Skip to main content

如何在 VPS 上安装 SSL 证书

本指南介绍如何在您的 VPS 上使用 Let's Encrypt 和 Certbot 安装免费 SSL 证书,适用于 Nginx 或 Apache。

订购服务器

要托管您的安全网站,HostMyServers 提供多种选择:

前提条件

  • 以 root 或具有 sudo 权限的用户进行 SSH 访问
  • 指向服务器 IP 地址的域名(DNS A 或 AAAA 记录)
  • 防火墙中开放 80 (HTTP) 和 443 (HTTPS) 端口
  • 已安装并配置 Nginx 或 Apache
  • 已更新的 Debian/Ubuntu 系统

安装 Certbot

推荐方法:通过 Snap

此方法确保您拥有最新版本的 Certbot:

sudo apt update
sudo apt install snapd -y
sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

替代方法:通过 APT

对于 Nginx

sudo apt update
sudo apt install certbot python3-certbot-nginx -y

对于 Apache

sudo apt update
sudo apt install certbot python3-certbot-apache -y

验证 Web 服务器配置

在获取证书之前,确保您的域名配置正确。

对于 Nginx

验证服务器块包含正确的 server_name

sudo nano /etc/nginx/sites-available/您的域名

文件应包含:

server {
listen 80;
listen [::]:80;
server_name 您的域名.com www.您的域名.com;
root /var/www/您的域名;
index index.html index.htm index.php;

location / {
try_files $uri $uri/ =404;
}
}

测试并重新加载配置:

sudo nginx -t
sudo systemctl reload nginx

对于 Apache

验证您的 VirtualHost:

sudo nano /etc/apache2/sites-available/您的域名.conf

文件应包含:

<VirtualHost *:80>
ServerName 您的域名.com
ServerAlias www.您的域名.com
DocumentRoot /var/www/您的域名

<Directory /var/www/您的域名>
AllowOverride All
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/您的域名-error.log
CustomLog ${APACHE_LOG_DIR}/您的域名-access.log combined
</VirtualHost>

启用站点并重新加载:

sudo a2ensite 您的域名.conf
sudo apachectl configtest
sudo systemctl reload apache2

获取 SSL 证书

使用 Nginx

使用 Nginx 插件运行 Certbot:

sudo certbot --nginx -d 您的域名.com -d www.您的域名.com

Certbot 将:

  1. 验证域名所有权
  2. 获取证书
  3. 自动配置 Nginx 的 HTTPS
  4. 设置 HTTP → HTTPS 重定向

使用 Apache

使用 Apache 插件运行 Certbot:

sudo certbot --apache -d 您的域名.com -d www.您的域名.com

Certbot 将:

  1. 验证域名所有权
  2. 获取证书
  3. 自动配置 Apache 的 HTTPS
  4. 如有必要,启用 SSL 模块
提示

在执行过程中,Certbot 会询问您是否要自动将 HTTP 流量重定向到 HTTPS。选择以获得更好的安全性。

手动配置(可选)

如果您喜欢手动配置 SSL,请使用 certonly 模式:

sudo certbot certonly --webroot -w /var/www/您的域名 -d 您的域名.com -d www.您的域名.com

Nginx 手动配置

修改您的服务器块:

# HTTP 到 HTTPS 重定向
server {
listen 80;
listen [::]:80;
server_name 您的域名.com www.您的域名.com;
return 301 https://$host$request_uri;
}

# HTTPS 配置
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name 您的域名.com www.您的域名.com;

ssl_certificate /etc/letsencrypt/live/您的域名.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/您的域名.com/privkey.pem;

# 推荐的 SSL 参数
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers off;

root /var/www/您的域名;
index index.html index.htm index.php;

location / {
try_files $uri $uri/ =404;
}
}

Apache 手动配置

启用 SSL 模块:

sudo a2enmod ssl
sudo a2enmod rewrite

创建或修改 SSL VirtualHost:

# HTTP 到 HTTPS 重定向
<VirtualHost *:80>
ServerName 您的域名.com
ServerAlias www.您的域名.com
Redirect permanent / https://您的域名.com/
</VirtualHost>

# HTTPS 配置
<VirtualHost *:443>
ServerName 您的域名.com
ServerAlias www.您的域名.com
DocumentRoot /var/www/您的域名

SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/您的域名.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/您的域名.com/privkey.pem

# 推荐的 SSL 参数
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
SSLHonorCipherOrder off

<Directory /var/www/您的域名>
AllowOverride All
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/您的域名-ssl-error.log
CustomLog ${APACHE_LOG_DIR}/您的域名-ssl-access.log combined
</VirtualHost>

重新加载 Apache:

sudo systemctl restart apache2

自动续期

Let's Encrypt 证书有效期为 90 天。Certbot 自动配置续期。

验证自动续期

测试续期而不实际执行:

sudo certbot renew --dry-run

检查 systemd 定时器

sudo systemctl status certbot.timer

强制续期

如有必要,您可以强制续期:

sudo certbot renew

添加重新加载钩子

在续期后自动重新加载 Web 服务器:

对于 Nginx

sudo certbot renew --post-hook "systemctl reload nginx"

对于 Apache

sudo certbot renew --post-hook "systemctl reload apache2"

验证证书

通过浏览器

访问 https://您的域名.com 并点击地址栏中的锁图标查看证书详情。

通过命令行

sudo certbot certificates

测试 SSL 配置

使用 SSL Labs 测试您的配置: https://www.ssllabs.com/ssltest/analyze.html?d=您的域名.com

高级安全(可选)

启用 HSTS

对于 Nginx,在 HTTPS 服务器块中添加:

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

对于 Apache,在 HTTPS VirtualHost 中添加:

Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"

不要忘记为 Apache 启用 headers 模块:

sudo a2enmod headers
sudo systemctl restart apache2

故障排除

域名验证错误

  • 验证 DNS 指向您的服务器:dig 您的域名.com
  • 验证端口 80 和 443 已开放:sudo ufw status
  • 检查 Certbot 日志:sudo tail -f /var/log/letsencrypt/letsencrypt.log

网站无法通过 HTTPS 访问

  • 验证 Web 服务器配置
  • 验证端口 443 在防火墙中已开放
  • 查看 Web 服务器日志:
    • Nginx:sudo tail -f /var/log/nginx/error.log
    • Apache:sudo tail -f /var/log/apache2/error.log

吊销证书

如有必要,您可以吊销证书:

sudo certbot revoke --cert-path /etc/letsencrypt/live/您的域名.com/cert.pem