本文记录在 CentOS 系统上,为 Hexo 博客配置域名绑定和 Let’s Encrypt 免费 SSL 证书的完整过程。

前置条件

  • 已购买域名并完成备案
  • 拥有一台云服务器(本文以 CentOS 为例)
  • 服务器已安装 Nginx
  • 域名已解析到服务器 IP

检查服务器状态

1. 检查 Nginx 是否运行

1
systemctl status nginx

2. 检查 80 端口监听情况

1
netstat -tlnp | grep -E ':(80|443)\s'

如果看到 nginx 正在监听 80 端口,说明 Nginx 已正常运行。

第一步:配置 DNS 解析

在域名注册商(如阿里云、腾讯云)的 DNS 管理页面,添加以下解析记录:

记录类型 主机记录 记录值 TTL
A @ 你的服务器IP 600
A www 你的服务器IP 600

记录值填写你的服务器公网 IP 地址,例如 117.72.58.244

第二步:配置 Nginx 域名绑定

编辑 Nginx 站点配置文件:

1
vi /etc/nginx/conf.d/blog.conf

修改 server_name 为你的域名:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
server {
listen 80;
listen [::]:80;

server_name yliang.top www.yliang.top; # 修改为你的域名

root /var/www/blog;
index index.html;

access_log /var/log/nginx/blog_access.log;
error_log /var/log/nginx/blog_error.log;

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

# 静态资源缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}

# Gzip 压缩
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript
application/x-javascript application/xml+rss
application/javascript application/json;
}

检查配置并重载:

1
2
nginx -t
nginx -s reload

此时访问 http://你的域名 应该可以看到博客了。

第三步:安装 Certbot

1
yum install -y certbot

第四步:获取 SSL 证书

使用 standalone 模式获取证书(需要临时停止 Nginx):

1
2
3
4
5
# 停止 Nginx
systemctl stop nginx

# 获取证书
certbot certonly --standalone -d yliang.top -d www.yliang.top

按提示操作:

  1. 输入邮箱地址(用于证书到期提醒)
  2. 同意服务条款(输入 Y
  3. 选择是否分享邮箱(可选)

获取成功后会显示证书保存位置:

1
2
/etc/letsencrypt/live/yliang.top/fullchain.pem
/etc/letsencrypt/live/yliang.top/privkey.pem

启动 Nginx:

1
systemctl start nginx

第五步:配置 HTTPS

编辑 Nginx 配置文件,添加 HTTPS 配置:

1
vi /etc/nginx/conf.d/blog.conf

完整配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# HTTP 重定向到 HTTPS
server {
listen 80;
listen [::]:80;
server_name yliang.top www.yliang.top;
return 301 https://$server_name$request_uri;
}

# HTTPS 配置
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name yliang.top www.yliang.top;

root /var/www/blog;
index index.html;

# SSL 证书配置
ssl_certificate /etc/letsencrypt/live/yliang.top/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yliang.top/privkey.pem;

# SSL 优化配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

access_log /var/log/nginx/blog_access.log;
error_log /var/log/nginx/blog_error.log;

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

# 静态资源缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}

# Gzip 压缩
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript
application/x-javascript application/xml+rss
application/javascript application/json;
}

检查配置并重载:

1
2
nginx -t
nginx -s reload

现在访问 https://你的域名 应该可以看到带小绿锁的 HTTPS 网站了。

第六步:配置证书自动续期

Let’s Encrypt 证书有效期为 90 天,需要设置自动续期。

1. 切换为 webroot 模式

修改续期配置文件,使用 webroot 模式(续期时无需停止 Nginx):

1
vi /etc/letsencrypt/renewal/你的域名.conf

确保有以下配置:

1
2
3
[renewalparams]
authenticator = webroot
webroot_path = /var/www/blog

2. 测试续期

1
certbot renew --dry-run

显示 new certificate deployed 表示测试成功。

3. 添加定时任务

编辑 crontab:

1
crontab -e

添加以下内容(每天凌晨 3 点检查并自动续期):

1
0 3 * * * certbot renew --quiet --webroot -w /var/www/blog && nginx -s reload

查看已添加的任务:

1
crontab -l

总结

完成以上步骤后,你的博客已具备:

  • ✅ 域名访问(HTTP 自动跳转 HTTPS)
  • ✅ 免费 SSL 证书(90 天有效期)
  • ✅ 证书自动续期
  • ✅ HTTP/2 支持
  • ✅ 静态资源缓存
  • ✅ Gzip 压缩

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 查看 Nginx 状态
systemctl status nginx

# 启动/停止/重启 Nginx
systemctl start/stop/restart nginx

# 检查 Nginx 配置
nginx -t

# 重载 Nginx 配置
nginx -s reload

# 手动续期证书
certbot renew --force-renewal --webroot -w /var/www/blog

# 查看证书到期时间
certbot certificates

故障排查

证书续期失败

如果续期时提示端口占用,确保使用了 webroot 模式:

1
certbot renew --force-renewal --webroot -w /var/www/blog

HTTPS 无法访问

检查防火墙是否开放 443 端口:

1
2
3
4
5
6
# 查看开放的端口
firewall-cmd --list-ports

# 开放 443 端口
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --reload

混合内容警告

如果浏览器提示混合内容,检查网页中是否有使用 http:// 的资源,全部改为 https:// 或使用相对路径 //