【Nginx】使用certbot安装免费https证书使Nginx支持Https请求
2023-09-14 08:57:35 时间
certbot官网:https://certbot.eff.org/lets-encrypt/centosrhel7-nginx
一、安装步骤
1)安装certbot,执行
sudo yum install certbot python2-certbot-nginx
2)检查是否安装成功,执行
certbot --help
[root@iz2zeb4argxs74khdclp2dz ~]# certbot --help Traceback (most recent call last): File "/usr/bin/certbot", line 9, in <module> load_entry_point('certbot==0.38.0', 'console_scripts', 'certbot')() File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 561, in load_entry_point return get_distribution(dist).load_entry_point(group, name) File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2649, in load_entry_point return ep.load() File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2303, in load return self.resolve() File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2309, in resolve module = __import__(self.module_name, fromlist=['__name__'], level=0) File "/usr/lib/python2.7/site-packages/certbot/main.py", line 17, in <module> from certbot import account File "/usr/lib/python2.7/site-packages/certbot/account.py", line 17, in <module> from acme import messages File "/usr/lib/python2.7/site-packages/acme/messages.py", line 11, in <module> from acme import challenges File "/usr/lib/python2.7/site-packages/acme/challenges.py", line 12, in <module> import requests File "/usr/lib/python2.7/site-packages/requests/__init__.py", line 58, in <module> from . import utils File "/usr/lib/python2.7/site-packages/requests/utils.py", line 32, in <module> from .exceptions import InvalidURL File "/usr/lib/python2.7/site-packages/requests/exceptions.py", line 10, in <module> from .packages.urllib3.exceptions import HTTPError as BaseHTTPError File "/usr/lib/python2.7/site-packages/requests/packages/__init__.py", line 95, in load_module raise ImportError("No module named '%s'" % (name,)) ImportError: No module named 'requests.packages.urllib3'
3)解决上面没有requests.packages.urllib3的问题,执行
pip install --upgrade --force-reinstall 'requests==2.6.0' urllib3
4)安装证书,执行
sudo certbot --nginx
如:
[root@iz2zeb4argxs74khdclp2dz ~]# sudo certbot --nginx Saving debug log to /var/log/letsencrypt/letsencrypt.log 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放到环境变量中,设置nginx软连接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/nginx/conf/ /etc/nginx
再次执行就OK了
sudo certbot --nginx 安装证书
5)然后再一步一步的根据提示进行配置
如:
[root@iz2zeb4argxs74khdclp2dz sbin]# sudo certbot --nginx Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator nginx, Installer nginx Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): 756623607@qq.com // 1)设置邮箱,用于安全提示 Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (A)gree/(C)ancel: a // 2)同意协议 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: n // 3)不共享你的邮箱 Which names would you like to activate HTTPS for? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: admin.talkilla.jiushiyaokuaile.cn 2: consultant.talkilla.jiushiyaokuaile.cn 3: student.talkilla.jiushiyaokuaile.cn 4: teacher.talkilla.jiushiyaokuaile.cn 5: wechat.talkilla.jiushiyaokuaile.cn - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shown (Enter 'c' to cancel): 1 2 3 4 5 // 4)选择需要激活https的域名 Obtaining a new certificate Performing the following challenges: http-01 challenge for admin.talkilla.jiushiyaokuaile.cn http-01 challenge for consultant.talkilla.jiushiyaokuaile.cn http-01 challenge for student.talkilla.jiushiyaokuaile.cn http-01 challenge for teacher.talkilla.jiushiyaokuaile.cn http-01 challenge for wechat.talkilla.jiushiyaokuaile.cn Waiting for verification... Cleaning up challenges Deploying Certificate to VirtualHost /usr/local/nginx/conf/conf.d/admin-talkilla.conf Deploying Certificate to VirtualHost /usr/local/nginx/conf/conf.d/consultant-talkilla.conf Deploying Certificate to VirtualHost /usr/local/nginx/conf/conf.d/student-talkilla.conf Deploying Certificate to VirtualHost /usr/local/nginx/conf/conf.d/teacher-talkilla.conf Deploying Certificate to VirtualHost /usr/local/nginx/conf/conf.d/wechat-talkilla.conf Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2 // 5)设置是否将http自动重定向到https,1否2是 Redirecting all traffic on port 80 to ssl in /usr/local/nginx/conf/conf.d/admin-talkilla-http.conf Redirecting all traffic on port 80 to ssl in /usr/local/nginx/conf/conf.d/consultant-talkilla.conf Redirecting all traffic on port 80 to ssl in /usr/local/nginx/conf/conf.d/student-talkilla.conf Redirecting all traffic on port 80 to ssl in /usr/local/nginx/conf/conf.d/teacher-talkilla.conf Redirecting all traffic on port 80 to ssl in /usr/local/nginx/conf/conf.d/wechat-talkilla.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://admin.talkilla.jiushiyaokuaile.cn, https://consultant.talkilla.jiushiyaokuaile.cn, https://student.talkilla.jiushiyaokuaile.cn, https://teacher.talkilla.jiushiyaokuaile.cn, and https://wechat.talkilla.jiushiyaokuaile.cn You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=admin.talkilla.jiushiyaokuaile.cn https://www.ssllabs.com/ssltest/analyze.html?d=consultant.talkilla.jiushiyaokuaile.cn https://www.ssllabs.com/ssltest/analyze.html?d=student.talkilla.jiushiyaokuaile.cn https://www.ssllabs.com/ssltest/analyze.html?d=teacher.talkilla.jiushiyaokuaile.cn https://www.ssllabs.com/ssltest/analyze.html?d=wechat.talkilla.jiushiyaokuaile.cn - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/admin.talkilla.jiushiyaokuaile.cn/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/admin.talkilla.jiushiyaokuaile.cn/privkey.pem Your cert will expire on 2020-01-06. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
6) 配置自动更新证书
在证书到期之后更新证书,我们可以通过 certbot renew 命令来更新证书
借助 Crontab 来编写一个定时任务,定期强制更新一个这个证书,然后重启 Nginx:
Crontab 通过 crontab -e 命令编辑,通过 crontab -l 查看。
这样就完成了 SSL 安全证书更新了。
使用crontab -e 命令:
0 0 1 * * certbot renew 10 0 1 * * systemctl restart nginx
注意:
若执行certbot renew时,出现: 'ascii' codec can't decode byte 0xe8 in position 2: ordinal not in range(128) 错误,参考如下:
7)当需要安装新的证书
执行:
sudo certbot run --nginx
相关文章
- HTTP服务器 nginx for windows下载 详细安装与配置
- Nginx配置SSL证书时——nginx:[emerg]unknown directive ssl错误
- 通过yum安装nginx-mysql-php-fastcgi配置LNMP
- docker 安装tomcat 镜像
- yum nginx 安装时错误 Errno 14 Couldn't resolve host 解决办法
- php5.6源码安装及php-fpm配置与nginx集成
- Linux 快速安装脚本
- linux 安装mysql
- Centos7下yum安装配置nginx与php
- Nginx配置文件(nginx.conf)配置详解
- Centos 7.6配置nginx反向代理,直接yum安装
- linux ubuntu/deepin安装tengine(nginx增强版)2.3.2
- OpenResty安装--增强版的nginx
- Nginx----安装和配置文件参数详解
- Atitit 图像处理类库 halcon11 安装与环境搭建attilax总结
- 初始化创建安装 vue3工程项目的3种方法!超详细
- Cygwin安装教程
- Python之Pyforest:Pyforest的简介、安装、使用方法之详细攻略
- Python Django框架学习11:Django Nginx+uwsgi 安装配置
- 1.第一章 计算机基础和Linux安装 -- 计算机系统(一)
- Ubuntu 20.04 源码安装 nginx
- 【Deepin 20系统】Linux系统安装RTX 2070的NVIDIA卡驱动
- Nginx之echo-nginx-module模块安装
- python3安装scrapy及使用方法(爬虫框架)
- 【C++ 科学计算】机器学习算法 Dlib 编译安装(ubuntu)
- 【Nginx】入门看这一篇就够啦,nginx 简介、安装、工作原理、工作方式、详解配置文件
- SonarQube 01 代码质量平台简介与安装
- Ubuntu16.04安装搜狗输入法的正确姿势
- 部署安装Nginx服务实例
- Ubuntu 20.04 源码安装 nginx