前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx配置使用certbot自动申请HTTPS证书

Nginx配置使用certbot自动申请HTTPS证书

作者头像
王先森sec
发布2024-06-28 10:20:34
870
发布2024-06-28 10:20:34
举报
文章被收录于专栏:王先森王先森

Nginx配置使用certbot自动申请HTTPS证书

王先森2024-06-272024-06-27

引言

在当今互联网环境中,网站的安全性越来越受到重视。HTTPS 作为一种能够提供加密通信和验证网站真实性的协议,已经成为网站安全的基本标准。然而,许多平台提供的 HTTPS 证书往往有有效期限制,比如腾讯云等平台目前提供的证书只有3个月的有效期。这不仅增加了证书管理的复杂性,还可能因为证书过期而导致网站访问中断。

为了解决这个问题,我们可以使用Certbot这个强大的工具来自动申请和管理 HTTPS 证书。Certbot是一个开源软件,它可以通过自动化的方式从Let's Encrypt等证书颁发机构(CA)获取和安装 HTTPS 证书。更重要的是,这些证书是免费的,并且可以通过自动续订来保持长期有效。Certbot支持多种服务器和平台,包括ApacheNginx等。

为什么选择Certbot?

  1. 自动化:Certbot 可以自动申请、安装和更新证书,无需手动干预。
  2. 免费:Let’s Encrypt 提供的证书完全免费。
  3. 安全:使用 HTTPS 加密网站流量,保护用户数据安全。
  4. 兼容性:广泛支持各种操作系统和Web服务器。
  5. 社区支持:拥有活跃的社区和丰富的文档资源。

如何使用Certbot申请HTTPS证书?

首先,你需要在服务器上安装 Certbot。具体安装方法取决于你使用的操作系统和Web服务器软件。你可以访问Certbot的官方网站 https://certbot.eff.org/ 获取详细的安装指南。

步骤1:安装snap

官方推荐使用snap客户端安装cerbot客户端,对于CentOS系统,可以使用以下命令:

代码语言:javascript
复制
yum -y install epel-release          	#安装依赖
yum -y install snapd					#安装snap
systemctl enable --now snapd.socket		#启动snap通信套接字
ln -s /var/lib/snapd/snap /snap			#创建如下软连接,为了启用classic snap的支持

如果少一步,安装cerbot会报错。如:没有创建软连接,在安装cerbob报以下错误; error: cannot install “certbot”: classic confinement requires snaps under /snap or symlink from /snap to /var/lib/snapd/snap

步骤2:安装Certbot

代码语言:javascript
复制
# 如果之前装过certbot的话要先卸载
sudo yum remove certbot

# 安装Certbo
snap install --classic certbot

# 配置Certbot命令行,执行如下命令以确保Certbot命令行可用
ln -s /snap/bin/certbot /usr/bin/certbot

注意:若出现error: cannot communicate with server: Post http://localhost/v2/snaps/snap-store: dial un,尝试重启snap

代码语言:javascript
复制
systemctl restart snapd.service

步骤3:选择插件

Certbot支持多种Web服务器插件。例如,如果您使用的是Nginx,可以使用nginx插件(二选一):

运行此命令获取证书,并让 Certbot 自动编辑 Nginx 配置以提供服务,只需一步即可打开HTTPS访问:

代码语言:javascript
复制
certbot --nginx

注:Certbot默认 nginx配置文件在 /etc/nginx/nginx.conf或 /usr/local/etc/nginx/nginx.conf,若你的nginx配置文件不在此处(以/usr/local/nginx/conf/nginx.conf为例),需在命令后加上 –nginx-server-root /usr/local/nginx/conf. 若出现The nginx plugin is not working; there may be problems with your existing configuration.,说明Nginx未安装,如已安装使用下面命令加入环境变量。由于没有将nginx放到环境变量中,设置nginx软连接

代码语言:javascript
复制
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/nginx/conf/ /etc/nginx

仅获得证书。如果你希望手动配置nginx,输入如下命令(推荐):

代码语言:javascript
复制
$ certbot certonly --nginx --nginx-ctl /application/nginx-1.24/sbin/nginx --nginx-server-root /application/nginx-1.24/conf/

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): [这里输入你的邮箱]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, 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: Y [选Y 继续]
Account registered.

Which names would you like to activate HTTPS for?
We recommend selecting either all domains, or all domains in a VirtualHost/server block.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: chat.boysec.cn
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):  [这里不需要输入,回车选所有]
Requesting a certificate for chat.boysec.cn

Successfully received certificate.
Certificate is saved at: 
# [这里告诉我们生成的文件路径和有效期]
/etc/letsencrypt/live/chat.boysec.cn/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/chat.boysec.cn/privkey.pem
This certificate expires on 2024-09-25.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

步骤4:修改Nginx配置

代码语言:javascript
复制
server {
    listen       80;
    server_name  chat.boysec.com;
    return 302 https://chat.boysec.com;
}
server {
    #监听443端口
    listen 443 ssl;
    #对应的域名,空格分隔域名就可以了
    server_name   chat.boysec.com;
    #证书相关配置
    ssl_ciphers           HIGH:!aHULL:!MD5:!DES:!DES3;
    ssl_certificate       /etc/letsencrypt/live/chat.boysec.com/fullchain.pem;
    ssl_certificate_key   /etc/letsencrypt/live/chat.boysec.com/privkey.pem;
    ssl_session_cache     shared:SSL:10m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ......
}

查看网站签发证书:

配置自动续签

虽然是免费的,但certbot只有90天的有效期,但它有自动续期的命令,无需再次运行certbot重新签证书。通过运行以下命令,可以测试证书的续订是否正常。

代码语言:javascript
复制
certbot renew --dry-run

如果没有报错,则自动续期命令是正常运行的。

最后所以我们设置个定时任务,让这个续期命令到期自动执行。 用certbot renew --force-renewal强制续签,否则没到期,无法续签的。

#进入编辑定时任务

代码语言:javascript
复制
crontab -e 
#添加以下,每隔三个月的,凌晨1点0分,强制续签
0 1 24 3,6,9,12 * /usr/bin/certbot renew --dry-run --force-renewal && /application/nginx-1.24/sbin/nginx -s reload

附加: 安装泛域名证书

代码语言:javascript
复制
certbot certonly --manual --preferred-challenges dns --nginx-ctl /application/nginx-1.24/sbin/nginx --nginx-server-root /application/nginx-1.24/conf/ -d *.boysec.com -d boysec.com

添加一条类型为text的域名解析记录 主机记录为:wangxiansen.boysec.com 值为certbot 提示生成的:arCjK1Xoi_xxxxxxxxxxxxx

验证DNS解析是否成功

代码语言:javascript
复制
nslookup -type=txt wangxiansen.boysec.com 8.8.8.8     
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-06-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 引言
    • 为什么选择Certbot?
      • 如何使用Certbot申请HTTPS证书?
        • 步骤1:安装snap
        • 步骤2:安装Certbot
        • 步骤3:选择插件
        • 步骤4:修改Nginx配置
      • 配置自动续签
        • 附加: 安装泛域名证书
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档