使用 Certbot 向 Let's Encrypt 免费申请 HTTPS 证书,ssl证书
1.let's Encrypt
1.申请证书
登录 https://certbot.eff.org/,选择我们博客网站使用的服务器软件和操作系统。教程中以 Nginx 和 CentOS 7 为例:
1.进入ssh服务
已满足
2.开启EPEL已满足
3.certbot python2-certbot-nginx 是 Let’s Encrypt 提供的 HTTPS 证书申请的工具,python2-certbot-nginx 是专门针对 Nginx 的插件,使得 Nginx 运行的服务申请证书更加简单方便。
centos7
sudo yum install certbot python2-certbot-nginx
cetntos 8 安装方法 https://certbot.eff.org/instructions?ws=nginx&os=centosrhel8
在 CentOS/RHEL 8 上安装 Certbot 以获取 HTTPS 证书是相当简单的。首先,您需要安装 snapd 并确保按照任何指示启用经典 snap 支持。接着,如果您的系统上安装了任何 Certbot 包,请先移除它们,以确保安装 Certbot snap 后使用的是 snap 而不是系统包管理器中的安装。下面是详细步骤:
-
安装 snapd:您需要安装 snapd,并确保按照任何指示启用经典 snap 支持。您可以在 snapcraft 网站上找到安装 snapd 的说明。
sudo yum install snapd
sudo systemctl enable --now snapd.socket
sudo ln -s /var/lib/snapd/snap /snap
-
移除 certbot-auto 和任何 Certbot OS 包:如果您使用像 apt、dnf 或 yum 这样的操作系统包管理器安装了任何 Certbot 包,请先移除它们。确保在安装 Certbot snap 之前,将这些包移除,以确保运行 certbot 命令时使用的是 snap 而不是操作系统包管理器中的安装。
例如,您可以使用以下命令移除 Certbot 包:
sudo apt-get remove certbot sudo dnf remove certbot sudo yum remove certbot
-
安装 Certbot:在命令行上运行以下命令来安装 Certbot:
sudo snap install --classic certbot
-
准备 Certbot 命令:执行以下命令以确保可以运行 certbot 命令:
sudo ln -s /snap/bin/certbot /usr/bin/certbot
通过执行上述步骤,您就可以在 CentOS/RHEL 8 上成功安装和配置 Certbot,以便轻松获取 HTTPS 证书。
4.然后运行证书申请命令:
1.(第一次远行用)
sudo certbot certonly --standalone -d xyz.com -d www.xyz.com
上述命令的standalone选项表示使用certbot内置的webserver(需要关闭在80端口上运行的webserver,如果有的话)为域名xyz.com, www.xyz.com生成证书;certonly表示不做任何自动安装的操作。接着进入一系列的交互,最终在提示的目录下生成:
cert.pem: 服务端证书
chain.pem: 浏览器需要的所有证书但不包括服务端证书,比如根证书和中间证书
fullchain.pem: 包括了cert.pem和chain.pem的内容
privkey.pem: 证书的私钥
我们游戏上Facebook后出现Android机器连接失败的问题,使用SSL证书检测网站检查了下,中间证书缺失。重新生成证书后,在cowboy中启动SSL时需要注意按以下项配置:
[{cacertfile, "path/fullchain.pem"},
{certfile, "path/cert.pem"},
{keyfile, "path/privkey.pem"}]
此时再检测时成功通过,Android也正常连接,问题解决
2.:
sudo certbot --nginx
注意(我没有遇到过)
经测试,运行上述命令后有可能报 ImportError: No module named 'requests.packages.urllib3' 的错误,这是由于 requests 和 urlib3 版本过低所致(可以参考这个 issue 的讨论),解决办法是重装它们,运行下面的命令:
pip uninstall requests
pip uninstall urllib3
yum remove python-urllib3
yum remove python-requests
然后重新安装 certbot,由于它依赖上面两个包,所以重装时会一并装上:
sudo yum install -y certbot python2-certbot-nginx
重新执行证书申请命令:sudo certbot --nginx
会有一系列交互式的提示,首先会让你输入邮箱,用于订阅。然后输入 a 同意他们的政策。
接着 certbot 会自动扫描出来域名,根据提示输入想开启 HTTPS 的域名标号:输入1
Which names would you like to activate HTTPS for?
1: yys534640040.club
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1
然后 certbot 会做一个域名校验,证明你对这个域名有控制权限。验证通过后,Let's Encrypt 就会把证书颁发给你。
最后会提示你是否把 HTTP 重定向到 HTTPS,当然选择是,这样 certbot 会自动帮我们修改 Nginx 的配置,将 HTTP 重定向到 HTTPS,如果用户使用 HTTP 协议访问我们的博客网站,就会重定向到 HTTPS 协议访问,确保安全性。
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
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/django-blog-tutorial-v2.conf
最后访问自己网站就变成了https了
由于全站开启了 HTTPS,因此需要把网站中非 HTTPS 的内容(比如通过 HTTP 协议请求的外部资源)改为 HTTPS,我们的博客中目前有一处引入外部图标库的样式文件是以 HTTP 协议引入的,需要改为 HTTPS:
blog\templates\detail.html
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
Nginx配置文件
vim /etc/nginx/conf.d/yys.conf
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/yys534640040.club/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/yys534640040.club/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
#手动测试,查看证书过期时间
certbot renew
#忽略证书过期时间,直接重置证书时间
certbot renew --force-renewal
#定时任务 ,如果没打开访问
crontab -e
#编辑文件
0 0 1 * * /usr/bin/certbot renew --force-renewal
/usr/bin/certbot renew --force-renewal报错
停止nginx
lsof -i:80
kill -9 pid关闭可以授权了
若打不开网页,重启nginx
sudo systemctl restart nginx
2.acme.sh(推荐)
支持ipv6,申请时候不用停nginx
发表评论
评论列表 (0 条评论)
暂无评论,快来抢沙发吧!