今天开始折腾使用Let’s Encrypt为网站签署HTTPS证书,记录过程如下。

我们的网站是使用CentOS 7上的Nginx接入的。 https://certbot.eff.org/#centosrhel7-nginx这个链接是关于CentOS 7上Nginx的配置说明。

安装certbot

Enable EPEL Repository:

1wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
2rpm -ivh epel-release-latest-7.noarch.rpm
1yum install certbot

生成HTTPS证书

手动生成证书:

1certbot certonly --standalone -d example.com
  • -d参数指定域名,是期望开启https的域名
  • --standalone表示申请证书时将启动certbot内置的webserver,因此需要先把网站的Nginx停掉
  • 在申请证书时letsencrypt会做认证,如果多次认证失败会启动Rate-limiting,因此在初期调试生成命令的时候,可以在加上--test-cert这个参数,表示先在letsencrypt的staging环境进行调试

上面命令生成的证书在/etc/letsencrypt/live/example.com中。

1ls /etc/letsencrypt/live/example.com
2certbot.log  cert.pem  chain.pem  fullchain.pem  privkey.pem  README

配置nginx

nginx中做如下的配置:

 1server {  
 2    listen 80;  
 3    server_name example.com;  
 4    rewrite ^(.*)$  https://$host$1 permanent;  
 5}
 6
 7server {
 8    listen       443 ssl;
 9    server_name  example.com;
10    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
11    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
12    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
13    location / {
14        root   html;
15        index  index.html index.htm;
16    }
17}

证书更新

前面生成的证书有效期为3个月,当快要到期时需要使用certbot renew命令更新证书。 在更新证书时需要先停止nginx,然后输入certbot renew --dry-run模拟更新,如果模拟更新没有问题使用certbot renew更新证书后,再启动nginx进行测试。

在证书到期前let’s encrypt为发提醒邮件,可使用下面的命令更新提醒邮件地址:

1 certbot register --update-registration --email [email protected]

证书查看和删除

查看:

1certbot certificates

删除:

1certbot delete

参考