标签归档:Python 2.6

在CentOS 6 的LNMP服务器上部署Let’s Encrypt的SSL证书

Let's Encrypt官方推荐的客户端certbot自2016年2月3日#2344开始真正支持Python 2.6,而在此之前需要Python 2.7,盲目升级CentOS 6 自带的Python 2.6到2.7,可能导致yum不能使用等问题。certbot-auto作为certbot的自动化脚本可以自动安装依赖并自动配置运行环境,使得获取Let's Encrypt证书简便快捷。

系统要求:本文是在CentOS 6.8已经部署好LNMP的服务器上部署certbot-auto,需要拥有服务器的root权限,在certbot准备运行环境阶段需要的内存较大,实测至少需要240M以上的内存
主要参考:
Let’s Encrypt and Nginx
建立个人博客,推荐使用搬瓦工年付19.99美元KVM虚拟VPS,512M内存,10G硬盘,500G月流量。

详细步骤:

1 取得 certbot-auto

访问https://certbot.eff.org/,在

I'm using Software on System.

分别选择nginx,CentOS 6,将弹出安装使用的详细方法。
1.1 首先需要安装certbot所依赖的源

yum install epel-release

1.2 下载certbot,并赋予运行权限
假设将certbot放在 /etc/certbot/目录下,

mkdir /etc/certbot/ && cd /etc/certbot/
wget https://dl.eff.org/certbot-auto

赋予运行权限:

chmod +x ./certbot-auto

安装依赖并配置自动运行环境:

./certbot-auto

2 运行certbot-auto取得证书

对于正常运行的LNMP服务器来说,推荐使用–webroot插件来获取证书,可以不必停止nginx的服务器的正常运行,否则需要停止nginx。
2.1 取得证书的命令如下:

/etc/certbot/certbot-auto certonly --webroot -w /var/www/html/ -d co1dawn.com -d www.co1dawn.com --staple-ocsp --hsts --rsa-key-size 4096 -m youremail@youremail.com

上述命令详解如下:

certonly     生成证书,但不安装。对于nginx,安装模式目前支持尚不完善,我们自己在nginx中配置证书。

–webroot   借助正在运行nginx的网站所在目录来生成证书请求的临时文件,例:/var/www/html/.well-known/

-w               用来指定目前正常运行nginx的服务器的网站所在目录,假设为:/var/www/html/

-d                co1dawn.com,www.co1dawn.com是两个不同的域名,根据自己的实际情况填写。

–staple-ocsp   开启证书的OCSP验证。

–hsts             强制浏览器使用加密连接访问网站。

–rsa-key-size 4096  请求4096bit的证书,默认为2048位已经足够。

-m                 自己的电子邮件地址。

2.2 可能碰到的错误

If you're using the webroot plugin, you should also verify that you are serving files from the webroot path you provided.

需要在nginx的配置文件中SSL区块中指定certbot的临时目录

       location ~ /.well-known {
                   allow all; 
       }

2.3 生成的证书位置
顺利的话,会给出成功的提示,并显示证书所在位置。

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/co1dawn.com/fullchain.pem (success)

3 在nginx中配置证书

共获得一枚私钥,三枚证书,我们需要用到其中的三个,在nginx的配置文件中SSL区块配置如下L:

...
ssl on;
ssl_certificate /etc/letsencrypt/live/co1dawn.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/co1dawn.com/privkey.pem;

#Enable SSL stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/letsencrypt/live/co1dawn.com/chain.pem;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
...

然后重启nginx服务:

service nginx restart

4 自动更新证书

90天的时间还是太短,因此需要自动更新证书
4.1 测试是否能自动更新

/etc/certbot/certbot-auto renew --dry-run

成功后有提示,但不会真正生成新的证书。
4.2 安装定时任务crontab

yum install -y vixie-cron

或者

yum install -y cronie

启动定时任务

service crond start

设crontab为开机自动启动

chkconfig crond on

4.3 将certbot-auto renew 加入定时运行

crontab -e

写入下列任务并保存退出:

14 06 * * * /etc/certbot/certbot-auto renew --post-hook "sudo service nginx reload" --quiet >> /var/log/certbot.log

如果出现

Unable to find post-hook command sudo in the PATH.
(PATH is /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

说明是以root权限运行的,可以去掉sudo,或者建立非root用户再运行。
这样每天早上6点14分,自动运行证书更新请求,并将运行日志记录在 /var/log/certbot.log,方便查看。