背景

项目以前用的是nginx,现在加上了openresty,有一个新的网站需要使用certbot生成,这个时候原先的生成命令不管用,研究了一下如何处理。

nginx时候的命令

执行获取 Let’s Encrypt https 证书命令

// certbot certonly --nginx --nginx-server-root [nginx配置目录] -d [域名] -m [邮箱]
certbot certonly --nginx --nginx-server-root /etc/nginx/ -d mt.xxx.cn -m xxx@qq.com

openresty时候的命令

从–nginx --nginx-server-root改为了–webroot -w,感觉nginx也可以用–webroot -w

// certbot certonly -webroot -w [项目路径] -d [域名] -m [邮箱]
certbot certonly --webroot -w /opt/projects/XXXXX/  -d mt.xxx.cn -m XXX@qq.com

注意

在执行certbot 命令之前,一定要确保http的请求能正确访问,openresty的配置和域名指向都正确,否则会抛错。

下图是我没有配置好http的访问导致的抛错:

使用certbot openresty执行获取 Let’s Encrypt https 免费证书_ci


先做一个临时的配置,能让certbot 访问的http网站

server {
    listen 80;
    server_name www.xxx.com;
    location / {
        try_files $uri $uri/ /index.html;
        root /opt/xxx/xxx;
        index index.html index.htm;
    }
}

配置正确以后的执行情况:

使用certbot openresty执行获取 Let’s Encrypt https 免费证书_nginx_02

启用443端口

并把http的指向https

server {
    listen 80;
    server_name www.xxx.com;
    rewrite  ^/(.*)$  https://www.xxx.com/$1 permanent;
}

server {
    listen 443 ssl;
    server_name www.xxx.com;

    ssl_prefer_server_ciphers on;
    ssl_ciphers HIGH:!ADH:!MD5:!aNULL:!eNULL:!MEDIUM:!LOW:!EXP:!kEDH;
    ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;

    ssl_certificate /etc/letsencrypt/live/www.xxx.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.xxx.com/privkey.pem;


    location / {
        try_files $uri $uri/ /index.html;
        root /opt/xxx/xxx;
        index index.html index.htm;
    }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   html;
        }
}

自动更新证书

Let’s Encrypt 免费 SSL 证书用起来非常方便,但每次申请只有三个月有效期,在每次到期之前都需要重新申请,Certbot 提供了一键续订的命令

# 1. 打开定时任务配置
 crontab -e
# 2. 增加定时刷新的配置
30 3 * */2 * /usr/bin/certbot renew --quiet >> /var/log/cerbot.log