使用 certbot 在centos7 搭建ssl证书自动并且续约

本文详细描述了如何在Linux服务器上安装Certbot并确保与Nginx兼容,包括编译安装Python2.7、处理ImportError问题、更新依赖和设置自动续签的步骤。
摘要由CSDN通过智能技术生成

第一步,确定服务器适合安装的certbot版本

sudo yum install python27

如果上述方法不起作用,你可以尝试编译安装。首先,你需要安装编译Python所需的依赖包。

sudo yum install gcc make openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel

接下来,下载Python 2.7.5的源代码,并进行编译安装。

cd /usr/src
sudo wget https://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz
sudo tar xzf Python-2.7.5.tgz
cd Python-2.7.5
sudo ./configure
sudo make altinstall
python --version
Python 2.7.5

查看web应用是nginx 还是apache (ps aux | grep -E ‘apache|httpd|nginx’)

nginx -v

发现:nginx version: nginx/1.24.0

httpd -v
nginx -v

安装certbot的python2-certbot-nginx这个版本

按照指定版本安装

第二步,按照指定版本安装以及遇到问题进行解决

yum install epel-release
yum install certbot python2-certbot-nginx

安装成功后可以查看certbot的版本

certbot --version

可能出现的错误

但是执行certbot失败,报错,如下问题
certbot --nginx -d xxx.com
问题1:使用certbot进行证书续期时报错ImportError: cannot import name UnrewindableBodyError
搜索问题有的说是certbot版本低有的说是需要更新python版本等等,按照如下操作后依旧还是报这个错

sudo yum update certbot 
sudo yum install epel-release
sudo yum install certbot python2-certbot-nginx
certbot --nginx -d shop.jjwer.com 
sudo yum update python
sudo yum update
certbot --nginx -d shop.jjwer.com 
sudo yum remove certbot
sudo yum install certbot python2-certbot-nginx
certbot --nginx -d shop.jjwer.com 
sudo certbot renew --cert-name shop.jjwer.com
python --version

后来再搜索:发现是python的软件包urlib3的问题,需要卸载重装
解决办法:重装 urllib3 库 问题解决

pip uninstall urllib3
pip install urllib3
pip install --upgrade pip

问题2:然后再次报错,依赖版本调整后ok
使用cerbot命令时报pkg_resources.DistributionNotFound: The ‘urllib3<1.23,>=1.21.1’ distribution was not found and is required by requests
easy_install urllib3==1.21.1

问题3:问题原因是openssl包的版本太低了,重新安并升级
使用cerbot命令时报ImportError: ‘pyOpenSSL’ module missing required functionality. Try upgrading to v0.14 or newer.

sudo pip2 install --upgrade pyOpenSSL
sudo pip2 install --force-reinstall pyOpenSSL
sudo pip install --upgrade pip
python2 -m pip show pyOpenSSL
sudo pip2 install /usr/lib64/python2.7/site-packages/pyOpenSSL.whl
sudo pip install --upgrade pip

上述操作还是未解决

于是卸载yum安装的certbot和pyOpenSSL
yum remove certbot pyOpenSSL;
升级pip(如果不是最新版本,pip会提示你升级后再使用)

pip install --upgrade pip

pip安装certbot
pip install certbot

在这里插入代码片

然后测试certbot是否可用: certbot certificates。输出正常,说明pip安装了最新版的certbot,并且能正确运行。

第三步,对指定域名安装证书

后面进行指定域名证书的安装,未指定邮箱报错
sudo certbot certonly --standalone -d xxx.com
指定邮箱后(不再报邮箱未指定错误,但是报下面这个错误,因为80端口被占用,于是查看80端口进行,然后关闭程序,安装证书,ok,然后再开启之前使用80端口的程序ngnix)

sudo certbot certonly --standalone -d 你的域名 --email 你的邮箱

报错:Problem binding to port 80: Could not bind to IPv4 or IPv6. ,是因为80端口被占用

netstat -tlnp | grep 80
service nginx stop

在服务器的配置文件 ,指向你的证书

例如 在你的域名的nignx配置中:

 	ssl_certificate    /www/server/panel/vhost/cert/xxxxx.com/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/xxxxx.com/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+D5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    add_header Strict-Transport-Security "max-age=31xxx0";
    error_page 497  https://$host$request_uri;

1 手动续签

sudo certbot certificates	//证书有效期查询
sudo systemctl stop nginx	//关闭nginx,解除占用端口
sudo certbot renew				//续签证书
sudo systemctl restart nginx	//重启nginx
sudo certbot certificates

2 自动续签

# /etc/cron.d/certbot: crontab entries for the certbot package
#
# Upstream recommends attempting renewal twice a day
#
# Eventually, this will be an opportunity to validate certificates
# haven't been revoked, etc.  Renewal will only occur if expiration
# is within 30 days.
#
# Important Note!  This cronjob will NOT be executed if you are
# running systemd as your init system.  If you are running systemd,
# the cronjob.timer function takes precedence over this cronjob.  For
# more details, see the systemd.timer manpage, or use systemctl show
# certbot.timer.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew

如果系统运行了systemd,这里crontab不会执行!

我选择自己添加crontab任务:
crontab -e
0 0  1 */3 * sudo systemctl stop nginx && certbot -q renew --renew-hook "systemctl restart nginx" //每隔3个月的当月第一天0分0秒执行一次
前5位表示分(0-60)、时(0-24)、天(1-30)、月(1-12)、周(0-6)。
- q表示不输出执行结果日志。
crontab -l //查看当前用户周期任务
crontab -l -u root //查看root用户周期任务
cat /etc/passwd | cut -f 1 -d : | xargs -I {} crontab -l -u {} //以root用户执行,查看所有周期任务
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值