文档中心
Linux绯荤粺瀹夎HTTPS璇佷功鍏ㄦ敾鐣ヤ粠鐢宠鍒伴儴缃茶瑙?txt
时间 : 2025-09-27 16:24:31浏览量 : 3
什么是HTTPS证书?

HTTPS证书(SSL/TLS证书)就像网站的"身份证",它能让你的网站从"http://"变成更安全的"https://"。当用户访问你的网站时,浏览器会先检查这个证书是否有效、是否可信,确认无误后才会建立加密连接。这就好比你去银行办理业务,工作人员要先核实你的身份证一样。
常见的HTTPS证书有几种类型:
- 域名验证型(DV):只验证域名所有权,适合个人网站
- 组织验证型(OV):会验证企业/组织信息,适合企业官网
- 扩展验证型(EV):最高级别验证,浏览器地址栏会显示公司名称
为什么需要HTTPS证书?
1. 数据加密:防止传输过程中被窃听。比如你登录网站输入密码时,没有HTTPS的话密码可能以明文传输。
2. 身份认证:确保用户访问的是真正的网站而非钓鱼网站。想象一下你以为是访问银行官网,结果是个冒牌货!
3. SEO优势:Google等搜索引擎会给HTTPS网站更高的排名权重。
4. 信任标识:浏览器地址栏会显示安全锁图标,增加用户信任度。
获取HTTPS证书的三种方式
1. 从CA机构购买商业证书
知名CA机构包括:
- DigiCert
- GlobalSign
- Sectigo(原Comodo)
- GeoTrust
价格从几十到几千元不等。优点是兼容性好、支持率高;缺点是成本较高。
2. 使用Let's Encrypt免费证书
Let's Encrypt是一个非盈利的CA机构,提供完全免费的DV证书。特点是:
- 免费!
- 有效期只有90天(商业证书通常1-2年)
- 需要定期续期
- 支持自动化部署
非常适合个人博客、测试环境等场景。
3. 自签名证书
自己用OpenSSL生成的证书。特点是:
- 完全免费
- 不受信任(浏览器会显示警告)
- 适合内部测试环境使用
Linux系统安装HTTPS证书详细步骤
下面我们以最常见的Nginx和Apache服务器为例,演示如何安装商业购买的SSL证书(假设你已经从CA获得了.crt和.key文件)。
Nginx服务器配置示例
1. 上传证书文件
将获得的.crt文件和.key文件上传到服务器,通常放在`/etc/nginx/ssl/`目录下:
```bash
mkdir -p /etc/nginx/ssl/
scp yourdomain.crt user@yourserver:/etc/nginx/ssl/
scp yourdomain.key user@yourserver:/etc/nginx/ssl/
```
2. 修改Nginx配置
编辑站点配置文件(通常在`/etc/nginx/sites-enabled/yourdomain.conf`):
```nginx
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/nginx/ssl/yourdomain.crt;
ssl_certificate_key /etc/nginx/ssl/yourdomain.key;
SSL优化配置
ssl_protocols TLSv1.2 TLSv1.3;
禁用不安全的旧协议
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256...';
HSTS头(增强安全)
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
...其他配置...
}
HTTP自动跳转到HTTPS
listen 80;
return 301 https://$host$request_uri;
3. 测试并重载配置
sudo nginx -t
测试配置是否正确
sudo systemctl reload nginx
重载配置不中断服务
Apache服务器配置示例
mkdir -p /etc/apache2/ssl/
scp yourdomain.crt user@yourserver:/etc/apache2/ssl/
scp yourdomain.key user@yourserver:/etc/apache2/ssl/
2. 启用SSL模块并修改配置
sudo a2enmod ssl
启用SSL模块
sudo systemctl restart apache2
重启Apache生效模块
编辑站点配置文件(如`/etc/apache2/sites-enabled/yoursite.conf`):
```apacheconf
ServerName yourdomain.com
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/youdomain.crt
如果是多个文件的组合:
SSLCertificateFile /path/to/signed_certificate.pem
SSLCertificateKeyFile /path/to/keyfile.key
SSLCACertificateFile /path/to/all_ca_certs.pem
或者单个文件包含所有内容:
SSLCertificateFile /path/to/fullchain.pem
SSLCertificateKeyFile /path/to/keyfile.key
HTTP跳转HTTPS (可选)
方法一:使用mod_rewrite模块(需先启用)
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
方法二:直接重定向(更简单)
Redirect permanent "/" "https://yourdomain.com/"
3. 重启Apache使生效
sudo apachectl configtest
测试配置语法是否正确
sudo systemctl restart apache2
重启服务生效新配置
Let's Encrypt免费证书自动安装方法
对于不想花钱购买商业证书的用户,Certbot工具可以一键获取和安装Let's Encrypt免费证书:
Nginx环境安装示例:
1. 安装Certbot工具
Ubuntu/Debian系统:
sudo apt update
sudo apt install certbot python3-certbot-nginx
CentOS/RHEL系统:
sudo yum install epel-release
sudo yum install certbot python-certbot-nginx
2. 运行Certbot获取并自动配置SSL
```bash
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
--nginx:自动检测并修改Nginx配置
-d:指定要保护的域名(可多个)
按照提示操作即可完成自动化部署!
3. 设置自动续期
Let's Encrypt每90天需要续期一次:
测试续期是否正常工作:
sudo certbot renew --dry-run
添加crontab任务自动续期:
echo "0 */12 * * * root test -x /usr/bin/certbot && perl -e 'sleep int(rand(3600))' && certbot renew -q" | sudo tee -a /etc/crontab > /dev/null
这样每12小时检查一次是否需要续期。
Apache环境类似:
只需将上面命令中的`--nginx`改为`--apache`即可:
sudo certbot --apache -d yourdomain.com -d www.yourdomian.com
HTTPS安全最佳实践
安装了SSL/TLS后还需要注意以下安全设置:
1. 禁用老旧协议和弱密码套件
在Nginx中应禁用不安全的TLSv1、TLSv1.1协议:
ssl_protocols TLSv1.2 TLSv1.3;
推荐的安全密码套件列表:
ECDHE
TAG:linux系统怎么安装https证书,linux怎么安装rz,linux安装rzsz,linux如何安装sh,linux安装ssl证书步骤,linux安装cer证书