默认wordpress是http进行访问,http其实不安全,但是使用https访问,如果没申请证书会提示警告,这个时候就需要申请有效的证书并配置。默认在腾讯云控制台是可以申请域名证书的,但是有控制台申请的域名证书每次只有3个月,每次都需要手动下载替换。在linux中可以直接使用certbot机器人进行申请和自动替换,好处有2个。

  • 可以申请https的ssl证书
  • 可以自动续期
    申请域名的前提条件:
  • 域名:需要已备案并有效的域名,并且该域名指向服务器。
  • 服务器:1台VPS 或云服务器,并且上面安装了 Nginx 或 Apache。
  • 操作系统: Debian、Ubuntu、CentOS系统
    本次以ubuntu系统为例,安装的时nginx。

安装与申请

1. 更新软件源

1
sudo apt update

2. 安装Certbot

在ubuntu中,安装cerbot

1
sudo apt install certbot python3-certbot-nginx

3. 申请ssl证书

替换yourdomain为你的域名,比如我申请rrdsceo.cn,那么就一次性申请:rrdsceo.cn,www.rrdsceo.cn, blog.rrdsceo.cn;

1
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com -d blog.yourdomain.com

Certbot 会请求 Let’s Encrypt 验证域名的控制权,通常 Certbot 会自动处理这个过程。验证成功后,它会安装证书并更新你的 web 服务器配置,比如查看:

1
2
sudo vim /etc/nginx/sites-enabled/default
sudo vim /etc/nginx/sites-enabled/yourdomain.com

都能看到有对应的443的https,则说明已经自动添加了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen 443 ssl;
listen [::]:443 ssl;

server_name yourdomain.com;

# SSL configuration
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

root /var/www/html; # 确保这个目录存在并有可访问的文件
index index.html index.htm index.nginx-debian.html;

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

这个时候访问https://yourdomain.com,确保 HTTPS 正常工作:

1
https://yourdomain.com

4. 自动续期

Let’s Encrypt 的证书有效期为 90 天,因此建议设置一个 Cron 任务自动续期。
`注意:默认情况下,Certbot 会在安装时为你设置自动续期。
你可以运行以下命令来测试自动续期。

1
sudo certbot renew --dry-run

也可以查看是否正在进行后台任务运行

1
systemctl list-timers | grep certbot

能看到一条,则表明后台自动运行

1
2
Sun 2024-11-10 04:34:35 CST 2h 33min left Sat 2024-11-09 13:12:10 CST 12h ago      certbot.timer                certbot.service