certbot申请https证书
- 安装certbot
wget https://dl.eff.org/certbot-auto chmod a+x certbot-auto
-
./certbot-auto certonly --standalone -d abc.com -d www.abc.com
certonly: obtain or renew a certificate,but do not install it
run: (default) obtain & install a certificate in your current webserver
renew: renew all previously botained certificate that are near expiry
-d : domains, Comma-separated list of domains to obtain a certificate
--manual: obtain certificate -
./certbot-auto certificates
展示获取的证书信息
- 执行上面指令,按提示操作。
Certbot 会启动一个临时服务器来完成验证(会占用80端口或443端口,因此需要暂时关闭 Web 服务器),然后 Certbot 会把证书以文件的形式保存,包括完整的证书链文件和私钥文件。
文件保存在 /etc/letsencrypt/live/ 下面的域名目录下。
修改nginx配置
server { listen 80; server_name abc.com; rewrite ^(.*) https://abc.com permanent; } server { listen 443 ssl default_server; listen [::]:443 ssl default_server; ssl_certificate /etc/letsencrypt/live/abc.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/abc.com/privkey.pem; server_name www.abc.com; root /web/abc.com/; }
- 创建定时任务,自动续期
默认证书有效期3个月
crontab -e 0 3 1 * * ./certbot-auto renew --renew-hook "sudo nginx -s reload"
每月1日的凌晨3点就会执行一次所有域名的续期操作。