
在香港VPS服务器上启用 HTTPS 并配置免费的 SSL 证书,可以通过使用 Let's Encrypt 提供的免费证书来实现。
1. 前提条件
-
VPS服务器基本配置:
- 系统:CentOS 7/8、Ubuntu 18.04/20.04 或其他主流 Linux 发行版。
- 必须已安装 Web 服务器(如 Apache 或 Nginx)。
-
域名解析:
- 确保域名已解析到您香港 VPS 的公网 IP 地址(通过 A 记录解析)。
-
管理员权限:
- 登录 VPS 的用户需要拥有
sudo
权限。
- 登录 VPS 的用户需要拥有
2. 安装 Web 服务器
如果您的服务器尚未安装 Web 服务器,可以参考以下步骤:
2.1 安装 Nginx
CentOS:
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
Ubuntu:
sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
2.2 安装 Apache
CentOS:
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
Ubuntu:
sudo apt update
sudo apt install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
3. 安装 Certbot
Certbot 是 Let's Encrypt 提供的客户端工具,用于自动生成和管理 SSL 证书。
3.1 安装 Certbot
根据您的 Web 服务器选择对应的插件:
(1) Nginx 配置
CentOS:
sudo yum install epel-release -y
sudo yum install certbot python3-certbot-nginx -y
Ubuntu:
sudo apt update
sudo apt install certbot python3-certbot-nginx -y
(2) Apache 配置
CentOS:
sudo yum install epel-release -y
sudo yum install certbot python3-certbot-apache -y
Ubuntu:
sudo apt update
sudo apt install certbot python3-certbot-apache -y
4. 申请并安装免费的 SSL 证书
4.1 为 Nginx 配置 HTTPS
-
申请证书:
使用 Certbot 自动申请 SSL 证书并配置 Nginx:bashsudo certbot --nginx
- Certbot 会自动检测 Nginx 配置文件中的域名,并为其申请证书。
- 按提示选择需要为哪个域名启用 HTTPS。
-
验证 HTTPS:
Certbot 会自动修改 Nginx 配置文件并启用 HTTPS,您可以使用以下命令重新加载 Nginx:bashsudo systemctl reload nginx
-
测试 HTTPS:
在浏览器中访问您的域名(如https://yourdomain.com
),确保 HTTPS 正常工作。
4.2 为 Apache 配置 HTTPS
-
申请证书:
使用 Certbot 自动申请 SSL 证书并配置 Apache:bashsudo certbot --apache
- Certbot 会自动检测 Apache 虚拟主机配置文件,并为检测到的域名申请证书。
- 按提示选择需要为哪个域名启用 HTTPS。
-
验证 HTTPS:
Certbot 会自动修改 Apache 配置文件并启用 HTTPS,您可以使用以下命令重新加载 Apache:bashsudo systemctl reload httpd # CentOS sudo systemctl reload apache2 # Ubuntu
-
测试 HTTPS:
在浏览器中访问您的域名(如https://yourdomain.com
),确保 HTTPS 正常工作。
5. 自动续期 SSL 证书
Let's Encrypt 的证书有效期为 90 天,但可以通过 certbot
设置自动续期任务。
5.1 测试自动续期
运行以下命令,测试自动续期是否正常:
sudo certbot renew --dry-run
如果测试成功,说明续期机制正常。
5.2 添加自动续期任务
Certbot 默认会在 /etc/cron.d
中添加自动续期任务。如果没有,可以手动创建:
sudo crontab -e
添加以下内容,每天凌晨自动检查证书并续期:
0 3 * * * /usr/bin/certbot renew --quiet
6. 强制 HTTPS
强制将 HTTP 请求重定向到 HTTPS,可以在 Web 服务器配置中添加重定向规则。
6.1 在 Nginx 中强制 HTTPS
编辑 Nginx 配置文件(通常在 /etc/nginx/nginx.conf
或 /etc/nginx/conf.d/
中):
sudo nano /etc/nginx/conf.d/yourdomain.conf
在 server
块中添加以下内容:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
重新加载 Nginx:
sudo systemctl reload nginx
6.2 在 Apache 中强制 HTTPS
编辑 Apache 虚拟主机配置文件(通常在 /etc/httpd/conf.d/
或 /etc/apache2/sites-available/
中):
sudo nano /etc/httpd/conf.d/ssl.conf # CentOS
sudo nano /etc/apache2/sites-available/000-default.conf # Ubuntu
添加以下内容:
<VirtualHost *:80>
ServerName yourdomain.com
ServerAlias www.yourdomain.com
Redirect permanent / https://yourdomain.com/
</VirtualHost>
重新加载 Apache:
sudo systemctl reload httpd # CentOS
sudo systemctl reload apache2 # Ubuntu
7. 验证 HTTPS 配置
-
在线工具验证:
使用 SSL Labs 的在线工具检查 HTTPS 配置是否安全:
https://www.ssllabs.com/ssltest/ -
浏览器验证:
- 在浏览器中访问您的域名,确保地址栏显示 HTTPS 和绿色锁标志。
- 确保所有 HTTP 请求都被重定向到 HTTPS。
8. 常见问题与解决方法
8.1 证书申请失败
- 问题:域名解析错误或未生效。
- 解决:确保域名 A 记录正确指向 VPS 的公网 IP,并等待解析生效(通常 5-10 分钟)。
8.2 HTTPS 页面不显示绿锁
- 问题:页面中有 HTTP 的资源(如图片、JS、CSS 文件)。
- 解决:检查页面源代码,确保所有资源使用 HTTPS。
8.3 自动续期失败
- 问题:防火墙或 Web 服务器未正确配置。
- 解决:确保打开端口 80 和 443,并检查 Certbot 是否有权限修改配置文件。
通过以上步骤,您可以成功为香港VPS服务器启用 HTTPS,并配置免费的 SSL 证书,从而提升网站的安全性和用户信任度!
- Tags:
- 香港VPS服务器,VPS服务器,香港VPS