Centos使用let's encrypt免费https证书(certbot)

最近做一个新加坡的项目,要用到亚马逊云服务AWS,遇到一个https的证书问题。以前国内项目都用的阿里云提供的免费https证书,直接推送到SLB完事。AWS也有同样的功能,使用ELB(负载均衡)提供免费的证书管理服务ACM,但存在一个问题,项目需要将https证书下载下来用于APP端验证,而AWS不提供证书下载。于是有人提出使用let’s encrypt的免费证书,然后我这边负责调研。

其实在2017年IOS强制使用https时就看过关于let’s encrypt的小道新闻,但没有真正关注,毕竟阿里云提供了相关服务。

首先感谢这些提供免费产品/服务的组织,let’s encrypt免费提供使用90天的证书,支持续期。看起来很完美,但实际使用过程中坑很多:

  1. let’s encrypt的证书制作过程是依赖应用服务器的,比如nginx、apache,需要能够访问到服务器的80端口。以nginx为例,需要提前安装好,并且装了SSL模块,还需要配置到环境变量中。
  2. 操作系统版本不一样都会导致证书制作出现各种异常。
  3. 最关键的是续期,如果能够自动续期,那什么问题都不是问题。如果拥有服务器的命令行权限,那么可以增加定时任务实现自动续期;但是对于没有命令行权限的服务(比如AWS ELB),只能手动处理,定期上传新证书,这其实不现实。

由于let’s encrypt官方推荐使用certbot来生成证书,下面将使用certbot进行尝试。参考https://certbot.eff.org/lets-encrypt/centosrhel7-nginx

第一步:安装certbot

sudo yum install certbot python2-certbot-nginx

其中certbot用来生成证书,python2-certbot-nginx用来将证书自动配置到nginx

执行后提示:

Failed: python-urllib3.noarch 0:1.10.2-5.el7

也就是urllib3安装失败,暂时没处理

第二步:生成证书

sudo certbot certonly --nginx

也可以使用下面的命令,直接将证书自动配置到nginx

sudo certbot --nginx

我使用的certbot certonly --nginx,执行后提示

ImportError: No module named ‘requests.packages.urllib3’

于是将urllib3删除后重新安装,再次执行

pip uninstall urllib3
pip install urllib3
sudo certbot certonly --nginx

依然报错,提示:

pkg_resources.DistributionNotFound: The ‘urllib3<1.23,>=1.21.1’
distribution was not found and is required by requests

于是指定urllib3的版本安装后再次执行

easy_install urllib3==1.21.1
sudo certbot certonly --nginx

报了其他错,提示

ImportError: ‘pyOpenSSL’ module missing required functionality. Try
upgrading to v0.14 or newer.

通过以下命令发现requests版本不一致

#	pip list 2>/dev/null | grep requests	

2.18.4

#	rpm -q python2-requests --queryformat '%{VERSION}\n'

2.6.0

于是将requests版本设置为一致后再次执行:

pip install --upgrade --force-reinstall 'requests==2.6.0'
sudo certbot certonly --nginx

提示错误:(也就是找不到nginx)

Could not choose appropriate plugin: The nginx plugin is not working; there may be problems with your existing configuration.
The error was: NoInstallationError("Could not find a usable 'nginx' binary. Ensure nginx exists, the binary is executable, and your PATH is set correctly.",)

由于没有将nginx放到环境变量中,设置nginx软连接

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/nginx/conf/ /etc/nginx
sudo certbot certonly --nginx

又提示新的错误:

Could not choose appropriate plugin: The nginx plugin is not working; there may be problems with your existing configuration.
The error was: PluginError('Nginx build is missing SSL module (--with-http_ssl_module).',)

通过nginx -V查看nginxconfigure arguments没有安装ssl模板,在nginx目录中重新构建

cd /opt/nginx-1.14.0
./configure --with-http_ssl_module
make && make install

再次检查nginx -V已经加上ssl模块。

使用sudo certbot certonly --nginx生成证书,中间需要填写email和域名,生成成功后会提示证书存放路径:

/etc/letsencrypt/live/kevin.xxx.com/fullchain.pem
/etc/letsencrypt/live/kevin.xxx.com/privkey.pem

第三步:使用证书
配置nginx:vim /opt/nginx-1.14.0/conf/nginx.conf

server {
        listen       443;

        ssl on;
        server_name  kevin.xxx.com;

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

        location / {
            root   /opt/www/test;
            index  index.html index.htm;
        }
}

重启后即可访问,记得要开放服务器的443端口
在这里插入图片描述

第四步:配置自动续签

每月1号、8号和20号零点自动检查更新证书,如果证书有效期还比较长,就不会更新。证书到期前20天、10天和1天都会发送通知邮件。

echo "0 0 1,8,20 * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew" | sudo tee -a /etc/crontab > /dev/null

意思就是在/etc/crontab中加入更新证书的定时任务

可以在/var/log/letsencrypt中查看日志,检查定时任务是否执行。

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值