51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

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

Web服务 Nginx

Nginx配置使用certbot自动申请HTTPS证书 {#CrawlerTitle}

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

引言 {#引言}

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

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

为什么选择Certbot? {#为什么选择Certbot?}

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

如何使用Certbot申请HTTPS证书? {#如何使用Certbot申请HTTPS证书?}

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

步骤1:安装snap {#步骤1:安装snap}

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

|-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 | 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 {#步骤2:安装Certbot}

|-------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 | # 如果之前装过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:dialun ,尝试重启snap

|-----------|-----------------------------------------| | 1 | systemctl restart snapd.service |

步骤3:选择插件 {#步骤3:选择插件}

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

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

    |-----------|-------------------------| | 1 | 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软连接

    |-------------|--------------------------------------------------------------------------------------------------| | 1 2 | ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx ln -s /usr/local/nginx/conf/ /etc/nginx |

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

    |------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | $ 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配置 {#步骤4:修改Nginx配置}

|------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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重新签证书。通过运行以下命令,可以测试证书的续订是否正常。

|-----------|---------------------------------| | 1 | certbot renew --dry-run |

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

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

#进入编辑定时任务

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

附加: 安装泛域名证书 {#附加-安装泛域名证书}

|-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 | 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解析是否成功

|-----------|-----------------------------------------------------------| | 1 | nslookup -type=txt wangxiansen.boysec.com 8.8.8.8 |

赞(0)
未经允许不得转载:工具盒子 » Nginx配置使用certbot自动申请HTTPS证书