参考文献

通过 Certbot 装置 Let's Encrypt 证书,来实现全站的 HTTPS 拜访

学院军 - 将博客利用从 HTTP 协定收费降级到 HTTPS

certbot官网地址

  • 关上首页先抉择本人的零碎版本 (我这里采纳的 web 服务器是 nginx,零碎是 centos7 )传输门

  • 能够依照官网提供的操作文档执行命令
# 装置 certbot 客户端工具sudo yum install certbot python2-certbot-nginx# 自动检测 nginx 配置以及确定哪些网站须要配置 ssl (会列出全副的 nginx 配置信息)sudo certbot --nginx# 设置 crontab 打算工作,自动更新 ssl 证书echo "0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew -q" | sudo tee -a /etc/crontab > /dev/null

以下记录自己装置 certbot 的整个过程

零碎环境

服务器:阿里云服务器
零碎: centos 7
web 服务器: nginx
装置过宝塔 (装置过宝塔后,nginx 的主配置文件位于 /www/server/nginx/conf/

整体流程是依照官网的流程来操作的,然而其中会遇到各种问题,呈现的问题如下:

  1. 装置 certbot 客户端工具 (此流程失常),装置过程中,该间接回车的就回车,该间接选 Yes 的输出 Y 而后回车
sudo yum install certbot python2-certbot-nginx
  1. 配置 ssl 时,一路出错,如下:
sudo certbot --nginx

重要错误信息如下:

pkg_resources.ContextualVersionConflict: (cryptography 2.1 (/usr/lib64/python2.7/site-packages), Requirement.parse('cryptography>=2.3'), set(['PyOpenSSL']))

查看 PyOpenSSL 版本信息,发现的确是版本过低的起因

pip show PyOpenSSL

解决方案:更新相应的 python 包

pip install -U PyOpenSSLpip install -U cryptography

再次执行以下命令

sudo certbot --nginx

再次发现报错,错误信息如下:

ImportError: cannot import name UnrewindableBodyError

解决方案:装置相应的 python 包

# 更新 pippip install --upgrade pip# 卸载 urllib3pip uninstall urllib3# 从新再次下载pip install urllib3

再次执行以下命令

sudo certbot --nginx

再次发现报错,错误信息如下:

ImportError: No module named urllib3.exceptions  

解决方案:间接暴力下载相应的 pyOpenSSL 包

yum -y install http://cbs.centos.org/kojifiles/packages/pyOpenSSL/16.2.0/3.el7/noarch/python2-pyOpenSSL-16.2.0-3.el7.noarch.rpm

再次执行以下命令

sudo certbot --nginx

持续报错,如下:

Error while running nginx -c /etc/nginx/nginx.conf -t.nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)nginx: configuration file /etc/nginx/nginx.conf test failedThe nginx plugin is not working; there may be problems with your existing configuration.The error was: MisconfigurationError('Error while running nginx -c /etc/nginx/nginx.conf -t.\n\nnginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)\nnginx: configuration file /etc/nginx/nginx.conf test failed\n',)

起因是:宝塔将 nginx 的主配置文件装置在 /www/server/nginx/conf/ 目录下,然而 certbot 默认在扫描 /etc/nginx/nginx.conf 文件,故而找不到 nginx 的配置文件

解决方案:指定 nginx 的配置目录,执行以下命令

sudo certbot --nginx --nginx-server-root=/www/server/nginx/conf/

然而可怜的是,仍然还是报错,报错信息如下:

An unexpected error occurred:TypeError: from_buffer() cannot return the address of the raw string within a str or unicode or bytearray object

解决方案:更新 cffi 包,执行以下命令

pip install --upgrade cffi

装置结束之后,再次执行以下命令

sudo certbot --nginx --nginx-server-root=/www/server/nginx/conf/

发现还是报错,报错信息如下:

An unexpected error occurred:UnicodeEncodeError: 'ascii' codec can't encode characters in position 310-328: ordinal not in range(128)

对于 ascii 报错,这篇文章有具体的介绍 通过 Certbot 装置 Let's Encrypt 证书,来实现全站的 HTTPS 拜访

解决方案是:查看你抉择的须要配置的 nginx 配置文件中是否含有中文,将所有的中文去掉就好了

去掉之后,再次执行以下命令

sudo certbot --nginx --nginx-server-root=/www/server/nginx/conf/

功败垂成!

比照一下没有配置 ssl 之前的 nginx 配置

没有配置 ssl 之前的 nginx 信息

server{    server_name www.pudongping.com;    index index.php index.html index.htm default.php default.htm default.html;    root /www/wwwroot/www.pudongping.com;    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)    {        return 404;    }}

配置 ssl 之后的配置信息 (当初配置文件中,默认强制性重定向了 https)

server{    server_name www.pudongping.com;    index index.php index.html index.htm default.php default.htm default.html;    root /www/wwwroot/www.pudongping.com;    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)    {        return 404;    }    listen 443 ssl; # managed by Certbot    ssl_certificate /etc/letsencrypt/live/www.pudongping.com/fullchain.pem; # managed by Certbot    ssl_certificate_key /etc/letsencrypt/live/www.pudongping.com/privkey.pem; # managed by Certbot    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot}server{    if ($host = www.pudongping.com) {        return 301 https://$host$request_uri;    } # managed by Certbot    listen 80;    server_name www.pudongping.com;    return 404; # managed by Certbot}

自动更新证书

因为 Let's Encrypt 默认的有效期是 90 天,所以如果你的利用须要在生产环境长期提供服务,还要在证书到期之后更新证书,咱们能够通过 certbot renew 命令来更新证书,你能够通过如下命令来测试该命令是否失效:

sudo certbot renew --dry-run

如果在输入中看到如下字样,则示意失效:

当然,实在环境中通过手动保护是不事实的,咱们能够借助 Crontab 来编写一个定时工作,每个月都强制更新一个这个证书,而后重启 Nginx:

0 0 1 * * certbot renew5 0 1 * * service nginx restart

或者间接执行官网提供的命令

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

对于目录

  • Certbot 的配置文件目录在 /etc/letsencrypt
  • Certbot 的 log 预设门路在 /var/log/letsencrypt
  • 网站对应的 .pem 文件门路在 /etc/letsencrypt/live/网站名称/privkey.pem