Let's encrypt 证书申请及安装配置
Let’s encrypt 证书申请及安装配置
为 Nginx 配置 Let’s Encrypt SSL 证书可以使用 Certbot,这是由 EFF 提供的一个非常受欢迎的 ACME 客户端。以下是详细的步骤来配置 Nginx 使用 Let’s Encrypt 的免费 SSL 证书:
1. 安装 Certbot 和 Nginx 插件
在大多数 Linux 发行版上,你可以通过包管理器安装 Certbot 及其 Nginx 插件。以下是基于 Ubuntu 24.04 的命令示例:
Ubuntu 24.04:
sudo apt update
sudo apt install certbot python3-certbot-nginx
2. 获取和安装 SSL 证书
确保你的域名解析已经指向了你想要配置 SSL 的服务器,并且 Nginx 已经正确配置并运行 (nginx 的安装配置此处省略, 改日可以单独一篇文章来说明)。
注意: 此处必须要在域名解析到服务器上运行命令才行, Certbot 会通过域名解析验证你的域名所有权。
运行以下命令以获取证书并自动配置 Nginx:
sudo certbot --nginx -d www.aicaiccxx.top
替换 www.aicaiccxx.top 为你的实际域名, 我这里使用了一个假的域名。如果你有多个域名或子域名,可以在 -d 参数后继续添加。
Certbot 会询问一些问题,比如联系人电子邮件等。
下面是一个示例输出:
sudo certbot --nginx -d www.aicaiccxx.top
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): 1951091234@fake-qq.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.4-April-3-2024.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
Account registered.
Requesting a certificate for www.aicaiccxx.top
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/www.aicaicc.top/fullchain.pem
Key is saved at: /etc/letsencrypt/live/www.aicaicc.top/privkey.pem
This certificate expires on 2025-03-17.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
Deploying certificate
Successfully deployed certificate for www.aicaiccxx.top to /etc/nginx/sites-enabled/default
Congratulations! You have successfully enabled HTTPS on https://www.aicaiccxx.top
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3. 验证配置
Certbot 通常会自动更新你的 Nginx 配置文件,添加必要的 SSL 指令,并重启 Nginx 服务以应用更改。你可以通过访问 https://yourdomain.com 来验证一切是否正常工作。
为了进一步测试你的 SSL 配置,可以使用 SSL Labs’ SSL Test 这样的在线工具。
4. 设置自动续期
Let’s Encrypt 证书的有效期是 90 天,但建议每 60 天或更早进行续期。Certbot 可以轻松设置自动续期,通常是通过 cron 或 systemd timer 完成的。
检查系统中是否已经设置了自动续期任务:
sudo systemctl list-timers | grep certbot
如果没有设置,可以手动创建一个 cron 作业来每天尝试续期证书。编辑 crontab 文件:
sudo crontab -e
添加以下行(如果不存在)以每天凌晨 2 点运行续期脚本:
0 2 * * * /usr/bin/certbot renew --quiet
5. 监控和维护
定期检查你的 SSL 证书状态,确保它们按时续期,并保持对 Nginx 和 Certbot 的更新。如果有任何配置变更或服务器迁移,记得重新验证和更新 SSL 证书。
通过以上步骤,你应该能够成功地为 Nginx 配置 Let’s Encrypt SSL 证书,并确保网站的安全性和性能。