Description
这是一篇纯粹的流水账笔记,本文涉及到的 certbot/certbot-auto 版本是 0.21,如果你用的是 0.22 或以上版本,可参考官网步骤来配置通配符
环境
- CentOS: 7.4
- Nginx: 1.12.2
- Python: 2.7.5
步骤
通过 yum install certbot
或者按照官方步骤,安装 certbot
之后,运行如下命令开始生成证书:
certbot certonly --email xxx@gmail.com --cert-name xxx --webroot -w /www/xxx/ -d xxxx.com -w /www/xxx2/ -d xxxx2.com
email
:指定一个邮箱cert-name
:设定证书名字d
:对应的域名w
:域名对应的根目录
多个域名正式其实就是将 -w
和 -d
部分重复即可。
但实际情况并不如此顺利,可能会碰到如下错误:
ImportError: No module named 'requests.packages.urllib3'
尝试 issue#5104 给出的一个方式:
pip install requests urllib3 pyOpenSSL --upgrade
运行之后再次生成证书时,又出现如下错误:
ImportError: 'pyOpenSSL' module missing required functionality. Try upgrading to v0.14 or newer.
升级 pyOpenSSL
:
pip install pyOpenSSL==16.2.0
ImportError
依然存在。
最后的解决方案是顺序运行如下命令:
pip uninstall requests
pip uninstall urllib3
yum remove python-urllib3
yum remove python-requests
yum remove certbot
yum install python-urllib3
yum install python-requests
yum install certbot
# 最后运行 certbot xxx 生成证书
如果证书创建成功的话会在终端看到类似如下的输出:
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
- ...
# 会输出证书的保存位置
国内的域名需要先备案,不然最后生成证书的时候会失败
证书生成之后呢,需要再配置 Web Server,我使用的是 Nginx,如果你使用的是 Apache 或者别的服务器,需要自行更改对应的配置文件。
# 在 nginx.conf 的对应 server 进行配置
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name xxxx.com;
root /home/www;
ssl_certificate "/etc/letsencrypt/live/xxxx.com/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/xxxx.com/privkey.pem";
ssl_trusted_certificate /etc/letsencrypt/live/xxxx.com/chain.pem;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
证书更新
由于 certbot
所使用的 letsencrypt
证书只有 90
天的有效期,所以需要定期更新证书。
测试自动更新功能:
sudo certbot renew --dry-run
如果功能正常,可以在 crontab
里添加计划任务来完成。在 /etc/crontab
文件里添加以下定时任务:
30 5 1 * * root /usr/bin/certbot renew --renew-hook "/usr/sbin/nginx -s reload"
以上每月1号5点30分执行更新,这个频率可以自己设定,不能太频繁。--renew-hook
后面根据自己的 web server 来改成相应的命令,上面是 nginx 作为 web server,证书更新后重新加载配置文件。
后记
对于 Python 2.6/CentOS 6/Nginx 的环境,则需要通过 certbot-auto
脚本安装:
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
创建证书:
sudo ./path/to/certbot-auto --nginx certonly --webroot -w /www/xxx/ -d xxx.com
但是有可能会出现如下的错误:
Error: Command '['/opt/eff.org/certbot/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1
相关 issue:#5523
运行如下命令:
export LC_ALL=C
再运行生成证书的命令即可,然后再配置对应的 server
:
# 在 nginx.conf 的对应 server 进行配置
server {
server_name your_domain;
location / {
root /xxx/www/1111/;
index index.html index.htm;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/your_domain/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/your_domain/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
}
测试自动更新功能:
sudo /path/to/certbot-auto renew --dry-run
如果功能正常,可以在 crontab
里添加计划任务来完成。在 /etc/crontab
文件里添加以下定时任务:
30 5 1 * * root /path/to/certbot-auto renew --renew-hook "/usr/sbin/nginx -s reload"
修改配置之后,运行 nginx -s reload
重新加载配置文件,重新访问配置的域名地址,地址栏前则会有对应的 Secure 标记: