通过Certbot安装Let'sEncrypt证书实现全站免费HTTPS访问首先选择自己的系统版本(我这里使用的web服务器是nginx,系统是centos7)。传送门可以根据官网提供的操作文档执行命令#安装certbot客户端工具sudoyuminstallcertbotpython2-certbot-nginx#自动检测nginx配置,判断哪些网站需要配置ssl(所有nginx配置信息都会被列出)sudocertbot--nginx#设置crontab定时任务,自动更新ssl证书echo"00,12***rootpython-c'importrandom;importtime;time.sleep(random.random()*3600)'&&certbotrenew-q"|sudotee-a/etc/crontab>/dev/null下面记录安装certbot的全过程。系统环境服务器:阿里云服务器系统:centos7web服务器:nginx安装宝塔(安装宝塔后,nginx的主要配置文件位于/www/server/nginx/conf/)整体流程按照官网流程操作,但是会遇到各种问题,问题如下如下:安装certbotclientTool(这个过程正常),在安装过程中,如果应该直接回车就回车,如果应该直接选择是就输入Y然后回车sudoyuminstallcertbotpython2-certbot-nginx配置ssl的时候,一路出错,如下:sudocertbot--nginx重要错误信息如下:pkg_resources.ContextualVersionConflict:(cryptography2.1(/usr/lib64/python2.7/site-packages),Requirement.parse('cryptography>=2.3'),set(['PyOpenSSL']))查看PyOpenSSL版本信息,发现版本太低pipshowPyOpenSSL解决办法:更新对应的python包pipinstall-UPyOpenSSLpipinstall-Ucryptography再次执行以下命令sudocertbot--nginx再次发现错误,错误信息如下:ImportError:cannotimportnameUnrewindableBodyError解决方法:安装对应的python包#updatepippipinstall--upgradepip#Uninstallurllib3pipuninstallurllib3#重新下载pipinstallurllib3,再次执行以下命令sudocertbot--nginxfounderroragain,报错信息如下:ImportError:Nomodulenamedurllib3.exceptions解决方法:直接下载对应的pyOpenSSL包yum-yinstallhttp://cbs.centos.org/kojifiles/packages/pyOpenSSL/16.2.0/3.el7/noarch/python2-pyOpenSSL-16.2.0-3。el7.noarch.rpm再次执行以下命令sudocertbot--nginx继续报错,如下:Errorwhilerunningnginx-c/etc/nginx/nginx.conf-t.nginx:[emerg]open()"/etc/nginx/nginx.conf”failed(2:Nosuchfileordirectory)nginx:配置文件/etc/nginx/nginx.conf测试失败nginx插件没有工作;您现有的配置可能存在问题。错误是:MisconfigurationError('Errorwhilerunningnginx-c/etc/nginx/nginx.conf-t.\n\nginx:[emerg]open()"/etc/nginx/nginx.conf"failed(2:Nosuchfileordirectory)\nnginx:configurationfile/etc/nginx/nginx.conftestfailed\n',)原因是:宝塔在/www/server/nginx/conf/目录下安装了主要的nginx配置文件,但是certbot默认扫描/etc/nginx/nginx.conf文件,所以找不到nginx配置文件。解决方法:指定nginx配置目录,执行如下命令sudocertbot--nginx--nginx-server-root=/www/server/nginx/conf/但是很遗憾,还是报错,报错信息如下:意外错误出现:TypeError:from_buffer()cannotreturntheaddressoftherawstringwithinastrorunicodeorbytearrayobject解决方法:更新cffi包,执行以下命令pipinstall--upgrade安装cffi后,再次执行以下命令sudocertbot--nginx--nginx-server-root=/www/server/nginx/conf/发现还是报错,报错信息如下:Anunexpectederroroccurred:UnicodeEncodeError:'ascii'codeccan'tencodecharactersinposition310-328:ordinalnotinrange(128)关于ascii错误,本文有详细介绍通过Certbot安装Let'sEncrypt证书实现全站HTTPS访问。解决方法是:查看你选择的nginx配置文件是否包含中文,把中文全部去掉即可。去掉之后,再次执行如下命令sudocertbot--nginx--nginx-server-root=/www/server/nginx/conf/就大功告成了!对比没有ssl的nginx配置和没有ssl配置的nginx信息server{server_namewww.pudongping.com;indexindex.phpindex.htmlindex.htmdefault.phpdefault.htmdefault.html;root/www/wwwroot/www.pudongping.com;位置~^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md){return404;}}配置完ssl配置信息(现在在配置文件中,HTTPS默认是必须的)server{server_namewww.pudongping.com;indexindex.phpindex.htmlindex.htmdefault.phpdefault.htmdefault.html;root/www/wwwroot/www.pudongping.com;位置~^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md){return404;}听443SSL;#由Certbot管理ssl_certificate/etc/letsencrypt/live/www.pudongping.com/fullchain.pem;#由Certbot管理ssl_certificate_key/etc/letsencrypt/live/www.pudongping.com/privkey.pem;#由Certbot管理包括/etc/letsencrypt/options-ssl-nginx.conf;#由Certbotssl_dhparam/etc/letsencrypt/ssl-dhparams.pem管理;#由Certbot管理}server{if($host=www.pudongping.com){return301https://$host$request_uri;}#由Certbot管理listen80;服务器名称www.浦东平网;返回404;#managedbyCertbot}自动续订证书由于Let'sEncrypt默认有效期为90天,所以如果您的应用需要长期在生产环境提供服务,证书过期后需要续订证书,我们可以使用certbotrenew命令更新证书。可以通过以下命令测试命令是否生效:sudocertbotrenew--dry-run如果在输出中看到如下字样,说明有效:当然,在真实环境中手动维护是不现实的。我们可以用crontab写一个定时任务,每个月强制更新一个证书,然后重启Nginx:001**certbotrenew501**servicenginxrestart或者直接执行官方命令echo"00,12***rootpython-c'importrandom;importtime;time.sleep(random.random()*0)'&&certbotrenew-q"|sudotee-a/etc/crontab>/dev/nullAbout目录Certbot的配置文件目录在/etc/letsencryptCertbot的日志预设路径在/var/log/letsencrypt网站对应的.pem文件路径在/etc/letsencrypt/live/站点名称/privkey.pem
