Let's Encrypt的证书签发与配置
in Tutorial with 13 comments
Let's Encrypt的证书签发与配置
in Tutorial with 13 comments

本教程没有用到任何脚本进行配置Let's Encrypt的证书,对于新手熟悉一下https的配置也是有帮助的。教程中的出现的linpx.com域名需要替换成你要签发的域名。另外教程是在阿里云ECS上实践出来后才写的,跟着教程走就可以签发成功。

教程最适合的CentOS 7.x,因为CentOS 6.x需要自行升级Python为2.7

签发证书

进入有root权限的终端,先停止nginx运行(必做)

CentOS 6.x:

service nginx stop

CentOS 7.x:

systemctl stop nginx

然后获取Let’s Encrypt的源代码

git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt

接着给你的网站签发证书

./letsencrypt-auto certonly --standalone --email i@linpx.com -d linpx.com -d www.linpx.com

!注!意!

(from https://pypi.python.org/simple/python2-pythondialog/) failed: <urlopen error [Errno -2] Name or service not known>

如果出现这个报错,则需要进行下面的配置

mv /etc/resolv.conf /etc/resolv.conf.backup && vim /etc/resolv.conf

打开resolv.conf文件之后里面应该是什么都没有的

i 进入编辑模式,复制下面的内容进去,保存退出

nameserver 223.5.5.5
nameserver 8.8.8.8

然后重新执行,给你的网站签发证书

./letsencrypt-auto certonly --standalone --email i@linpx.com -d linpx.com -d www.linpx.com

这个主要是修复服务器上的DNS解析失败的问题


完成上面的操作,Let’s Encrypt就已经给你的网站签发证书了

可以输入下面命令查看

ls /etc/letsencrypt/live/linpx.com(这里的域名是你的签发证书域名)

会发现里面有四个文件

cert.pem  chain.pem  fullchain.pem  privkey.pem

前两个是给Apache用的,后两个才是给Nginx用的

配置Nginx

这里默认你已经开启了https,只是替换证书而已···

打开网站所对应的Nginx的conf配置文件

假设我的配置文件是在 /usr/local/nginx/conf/vhost 的目录

vim /usr/local/nginx/conf/vhost/www.linpx.com.conf

打开后,按 i 进入编辑模式,找到

ssl_certificate .............;
ssl_certificate_key ............;

修改为

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

不要忘了启动Nginx

CentOS 6.x:

service nginx start

CentOS 7.x:

systemctl start nginx

至此,Let's Encrypt的证书签发与配置已经完成,刷新你的网站,就能看到绿绿的小锁

自动续期

一般地,Let's Encrypt的证书有效期只有90天,所以需要设置一个自动续期的任务

先安装crontabs,一般情况下是提示已经安装

yum -y install crontabs

然后

crontab -e

添加下面的内容在新的一行,其中目录/root/letsencrypt 为上一步操作中所下载letsencrypt配置文件的所在目录

CentOS 6.x:

0 0 1 * * service nginx stop && ./root/letsencrypt/letsencrypt-auto certonly --renew-by-default --email i@linpx.com -d linpx.com -d www.linpx.com && service nginx start

CentOS 7.x:

0 0 1 * * systemctl stop nginx && ./root/letsencrypt/letsencrypt-auto certonly --renew-by-default --email i@linpx.com -d linpx.com -d www.linpx.com && systemctl start nginx

添加完成后,保存退出,查看定时任务列表

crontab -l

查看显示成功追加了定时任务···

这样,服务器就会在每个月1号自动为你的Let's Encrypt证书续签了。

辅助教程如下:

参考

Responses