龙空技术网

实操帖--linux下申请Let’s Encrypt免费SSL证书

五四疯哥 221

前言:

此刻小伙伴们对“letsencryptcentos6”大约比较着重,各位老铁们都想要学习一些“letsencryptcentos6”的相关知识。那么小编在网上网罗了一些有关“letsencryptcentos6””的相关知识,希望各位老铁们能喜欢,小伙伴们一起来学习一下吧!

上篇文章说了怎么在阿里云下申请免费的SSL证书,这是我第一推荐的免费证书,还有一种使用比较广泛的是Let's Encrypt免费证书,Let's Encrypt在各种浏览器的支持也非常好,缺点是一次只有3个月有效期,每3个月需要续签一次,安装起来也比较麻烦,下面具体介绍一下Let's Encrypt证书的申请步骤。

环境

我用的是阿里云的ECS服务器,系统是CentOS7,提前安装好了nginx,服务器的443端口要开放,nginx要停止运行。方便演示,我这里直接用的root用户。

官方文档地址

安装步骤

1.下载certbot-auto,certbot-auto是certbot提供的官方脚本,可以简化安装和操作步骤。下载脚本,owner设为root,赋予可执行权限,就不赘述了。

user@webserver:~$ wget  sudo mv certbot-auto /usr/local/bin/certbot-autouser@webserver:~$ sudo chown root /usr/local/bin/certbot-autouser@webserver:~$ chmod 0755 /usr/local/bin/certbot-autouser@webserver:~$ /usr/local/bin/certbot-auto --help

2.用certbot-auto安装certbot,命令/usr/local/bin/certbot-auto --install-only

[root@iZ28pgeqfooZ ~]# /usr/local/bin/certbot-auto --install-only

出现如下提示说明安装成功

Complete!Creating virtual environment...Installing Python packages...Installation succeeded.Certbot is installed.

这时候再看certbot-auto --help给的提示就多了

3.运行命令生成证书,换上你自己的邮箱和域名

/usr/local/bin/certbot-auto certonly --standalone --email 'xxxx@xx.com' -d 'xx.xxxxxxxx.com'

如果报错Problem binding to port 80: Could not bind to IPv4 or IPv6,说明你80端口被占了,我这里是因为忘了关nginx,关掉再来一次,看到下面提示说明证书申请成功,红色是证书在本地的保存地址。

4.配置nginx.conf,xx.xxxx.com换成你自己的域名

server { listen 443 ssl; server_name xx.xxxx.com; ssl on; ssl_certificate /etc/letsencrypt/live/xx.xxxx.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/xx.xxxx.com/privkey.pem; ssl_session_timeout 5m; ssl_protocols SSLv3 TLSv1; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP; ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; }}

5.看下效果

6.证书更新。

Let's Encrypt的证书有效期只有3个月,需要自己去更新,官方提供更新命令,更新的时候要关闭nginx。

/usr/local/bin/certbot-auto renew 

看上面的提示,因为我的证书没到期所以不让更新,可以加上参数 --force-renew强制更新

/usr/local/bin/certbot-auto renew --force-renew

一般的做法是写一个定时更新的脚本,然后用cron定时任务自动执行,免去了手工执行的麻烦,脚本里就是3步,关nginx,更新,开nginx,这里就不举例了。官方也提供了一步到位的命令。

certbot renew --pre-hook "service nginx stop" --post-hook "service nginx start"
结语

Let's Encrypt在开发中用的也比较多,它还有更多复杂的命令可以去看官方的文档。这里还要安利一下我上篇文章中的阿里云免费SSL证书,自己觉得比这个好用,当然大家见仁见智。

欢迎关注我的头条账号五四疯哥,查看更多实操攻略。

标签: #letsencryptcentos6