为已有的url配置ssl证书

个人针对 配置好nginx的 url。使用Certbot一键配置。

这里是针对 配置好nginx的 url。使用Certbot一键配置。个人这样整一下也够用,企业那是企业的方法。

前言

优点

  • 自动化程度高
    Certbot 能够自动申请、验证和配置 SSL/TLS 证书,大大简化了流程,尤其对不熟悉手动配置的人来说非常友好。

  • 自动续期
    一旦配置成功,Certbot 会自动安排续期任务,确保证书在到期前自动更新,减少了维护负担。

  • 减少人为错误
    自动化配置减少了手动编辑配置文件的需求,从而降低因配置错误而导致的安全风险或服务中断。

  • 快速部署
    通过一键配置,可以在短时间内使网站支持 HTTPS,提高网站的安全性和用户信任度。

  • 开源免费
    Certbot 是 Let’s Encrypt 官方推荐的工具,免费且开源,适用于各种规模的网站。

缺点

  • 灵活性较低
    一键配置适用于标准的 Nginx 或 Apache 环境,对于有特殊需求或复杂配置的场景,自动配置可能无法满足所有定制化要求。

  • 覆盖现有配置
    在自动配置过程中,Certbot 可能会修改或覆盖已有的 Web 服务器配置文件,因此在使用前建议备份原始配置文件。

  • 对环境要求严格
    自动配置依赖于端口(如 80/443)开放,并且服务器环境需符合 Certbot 的运行要求,某些非标准环境可能需要手动调整。

  • 调试难度
    如果自动配置过程中出现问题,调试可能相对复杂,特别是对于不熟悉 SSL/TLS 配置或 Web 服务器内部机制的用户。

个人快速配一个也够用,而且非常简单。

安装工具

安装 Certbot:

sudo apt update
sudo apt install certbot python3-certbot-nginx

申请证书

检查nginx配置。这里的文件名是你的配置文件名。

cat /etc/nginx/sites-available/<文件名>

我这里的配置如下。其中<域名>是你配置的域名:

server {
    listen 80;
    server_name <域名>;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

现在申请证书。<域名>替换为上文的域名:

sudo certbot --nginx -d <域名>

在运行命令时,Certbot 会自动尝试以下几个步骤:

  1. 使用 HTTP-01 挑战验证 <域名> 的所有权。Certbot 会在 Nginx 上设置一个临时的路径来验证所有权。

  2. 如果验证成功,Certbot 会为你配置 SSL 证书,并自动更新 Nginx 配置文件,启用 HTTPS。

第一次使用可能会出现要邮箱注册账号。输入邮箱之后一直Y就行。

root@iZ2vcbdukawlb87fl1d64tZ:/etc/nginx/sites-available# sudo certbot --nginx -d <域名>
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.5-February-24-2025.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 <域名>

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/<域名>fullchain.pem
Key is saved at:         /etc/letsencrypt/live/<域名>/privkey.pem
This certificate expires on 2025-06-13.
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 blog.tsminato.cn to /etc/nginx/sites-enabled/<文件名>
Congratulations! You have successfully enabled HTTPS on <域名>

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

现在重新执行上文检查配置文件:

cat /etc/nginx/sites-available/<文件名>
server {
    server_name <域名>;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/<域名>/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/<域名>/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

}
server {
    if ($host = <域名>) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name <域名>;
    return 404; # managed by Certbot


}

上面的<域名>是你自己的域名。这里我稍微隐藏了下。

检查无误之后重启nginx

sudo systemctl reload nginx

然后你可以通过https访问你的域名。

总结

对于大多数标准 Web 服务场景,使用 Certbot 一键配置证书能够大大简化部署过程,提高安全性;但在一些高度定制化的场景下,可能需要手动配置或结合自动化工具进行二次调整。

Comment