• 使用 Certbot 为 Nginx 自动配置 SSL 证书


    发布于 2023-07-13 on https://chenhaotian.top/linux/certbot-nginx/

    使用 Certbot 为 Nginx 自动配置 SSL 证书

    配置步骤

    Debian 11 为例

    1. 安装Certbot和Nginx插件

    sudo apt-get update
    sudo apt-get install certbot python3-certbot-nginx
    
    • 1
    • 2

    2. 获取和安装证书

    运行 Certbot 自动安装 SSL 证书。注意替换your_domain

    sudo certbot --nginx -d your_domain
    
    • 1

    Certbot 将自动与 Let’s Encrypt 的服务器通信,验证域名,请求SSL证书。

    3. 测试自动更新

    Let’s Encrypt 的证书每 90 天需要更新一次。运行以下命令来测试自动更新是否正常工作:

    sudo certbot renew --dry-run
    
    • 1

    若测试成功,证书将自动更新,无需任何其他操作。

    配置过程示例

    若是第一次配置,会要求输入一个合法邮箱提供给 Let’s Encrypt。若自动更新失效,Let’s Encrypt 会在证书失效前给你发邮件。

    root@VM-PV30VGNA7611:~# sudo certbot --nginx -d storage.opennet.top
    Saving debug log to /var/log/letsencrypt/letsencrypt.log
    Plugins selected: Authenticator nginx, Installer nginx
    Obtaining a new certificate
    Performing the following challenges:
    http-01 challenge for storage.opennet.top
    Waiting for verification...
    Cleaning up challenges
    Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/default
    
    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): 1
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Congratulations! You have successfully enabled https://storage.opennet.top
    
    You should test your configuration at:
    https://www.ssllabs.com/ssltest/analyze.html?d=storage.opennet.top
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    IMPORTANT NOTES:
     - Congratulations! Your certificate and chain have been saved at:
       /etc/letsencrypt/live/storage.opennet.top/fullchain.pem
       Your key file has been saved at:
       /etc/letsencrypt/live/storage.opennet.top/privkey.pem
       Your cert will expire on 2023-10-11. To obtain a new or tweaked
       version of this certificate in the future, simply run certbot again
       with the "certonly" option. To non-interactively renew *all* of
       your certificates, run "certbot renew"
     - 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
    
    • 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

    自动更新测试

    root@VM-PV30VGNA7611:~# sudo certbot renew --dry-run
    Saving debug log to /var/log/letsencrypt/letsencrypt.log
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Processing /etc/letsencrypt/renewal/storage.opennet.top.conf
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Cert not due for renewal, but simulating renewal for dry run
    Plugins selected: Authenticator nginx, Installer nginx
    Renewing an existing certificate
    Performing the following challenges:
    http-01 challenge for storage.opennet.top
    Waiting for verification...
    Cleaning up challenges
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    new certificate deployed with reload of nginx server; fullchain is
    /etc/letsencrypt/live/storage.opennet.top/fullchain.pem
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ** DRY RUN: simulating 'certbot renew' close to cert expiry
    **          (The test certificates below have not been saved.)
    
    Congratulations, all renewals succeeded. The following certs have been renewed:
      /etc/letsencrypt/live/storage.opennet.top/fullchain.pem (success)
    ** DRY RUN: simulating 'certbot renew' close to cert expiry
    **          (The test certificates above have not been saved.)
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    • 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

    Nginx 配置文件示例

    在写配置文件时只需写 80 端口,Certbot 会自动添加 443 端口的监听以及 SSL 证书的配置

    server {
        listen 80;
        server_name storage.opennet.top;
    
        location / {
            gzip off;
            proxy_pass http://localhost:8080;
            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;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_cache_bypass $http_upgrade;
        }
    
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/storage.opennet.top/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/storage.opennet.top/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
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    取消 Certbot 对网站的管理

    1. 删除证书:

    列出 Certbot 管理的所有证书

    sudo certbot certificates
    
    • 1

    删除证书,将 name_of_certificate 替换为证书名称,此时会自动取消续期

    sudo certbot delete --cert-name name_of_certificate
    
    • 1

    2. 删除 Nginx 的 SSL 配置:

    在 Nginx 配置中删除关于该证书的引用。编辑 Nginx 配置文件(通常位于 /etc/nginx/sites-available/),删除或注释掉所有 # managed by Certbot 的代码

    在这里插入图片描述

    重新加载 Nginx

    sudo systemctl reload nginx
    
    • 1
  • 相关阅读:
    天拓四方分享:智能装备制造厂商如何利用远程运维提升用户设备价值
    计算机视觉之三维重建——第八章:SLAM系统设计《深入浅出sfm和SLAM核心算法 (鲁鹏)》
    Android:RecyclerView的ChildHelper
    知网G4《基础教育论坛》期刊投稿邮箱
    unity搭建xlua和emmy_lua的debug环境
    路由器配置静态和默认路由实现VLAN之间的通信
    Web自动化测试进阶:网页中难点之等待机制 —— 强制等待,隐式等待
    八月七日 SpringBoot自动装配
    C#线程的参数传递、获取线程返回值以及处理多线程冲突
    POJ 3134:Power Calculus ← IDA*
  • 原文地址:https://blog.csdn.net/qq_34010538/article/details/133825978