zoukankan      html  css  js  c++  java
  • 使用certbot 为nginx或openresty启用https

    certbot 使用letsencrypt 生成免费https证书

    以下内容网上整理,原文地址 https://segmentfault.com/a/1190000005797776

    ----------------------

    环境
    linux + nginx

    1.安装CertBot

    certbot官网地址
    https://certbot.eff.org

    下载

    wget https://dl.eff.org/certbot-auto
    sudo mv certbot-auto /usr/local/bin/certbot-auto
    sudo chown root /usr/local/bin/certbot-auto
    sudo chmod 0755 /usr/local/bin/certbot-auto

    2.配置Nginx
    这里我不想使用CertBot的standalone模式,这个模式虽然可以配置好服务器,但是以后Renew的时候,需要让服务停止一下,再启动。因此抛弃这个模式,我们使用Webroot配置模式。

    因为,CertBot在验证服务器域名的时候,会生成一个随机文件,然后CertBot的服务器会通过HTTP访问你的这个文件,因此要确保你的Nginx配置好,以便可以访问到这个文件。

    修改你的服务器配置,在server模块添加:

    location ^~ /.well-known/acme-challenge/ {
    default_type "text/plain";
    root /usr/share/nginx/html;
    }
    
    location = /.well-known/acme-challenge/ {
    return 404;
    }

    可以看到,上面的root,我们让他指向了/usr/share/nginx/html,让检验的链接指向了nginx默认的文件夹下。

    接着重新加载Nginx配置:

    sudo service nginx reload

    然后在命令行输入:

    sudo /usr/local/bin/certbot-auto certonly --webroot -w /usr/share/nginx/html/ -d your.domain.com

    -w 指定生成well-known验证目录
    -d 指向自己的域名
    上面记得替换your.domain.com为你自己的域名。

    如果提示:

    IMPORTANT NOTES:
    - Congratulations! Your certificate and chain have been saved at
    /etc/letsencrypt/live/your.domain.com/fullchain.pem. Your cert
    will expire on 20XX-09-23. To obtain a new or tweaked version of
    this certificate in the future, simply run certbot again. 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

    证书生成成功!

    启用443端口
    同样,修改Nginx的虚拟主机配置文件,新建一个443端口的server配置:

    server {
    listen 443 ssl;
    listen [::]:443 ssl ipv6only=on;
    
    ssl_certificate /etc/letsencrypt/live/your.domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your.domain.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/your.domain.com/chain.pem;
    
    // ... other settings ...
    }

    上面记得替换your.domain.com为你自己的域名。

    接着重新加载Nginx配置:

    sudo service nginx reload

    现在通过浏览器访问你的网站:https://your.domain.com试试,如果看到浏览器的绿色标志,恭喜你设置成功!

    不过由于这个证书的时效只有90天,我们需要设置自动更新的功能,帮我们自动更新证书的时效。

    3.自动更新证书
    先在命令行模拟证书更新:

    sudo /usr/local/bin/certbot-auto renew --dry-run

    模拟更新成功的效果如下:

    -------------------------------------------------------------------------------
    Processing /etc/letsencrypt/renewal/your.domain.com.conf
    -------------------------------------------------------------------------------
    ** 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/your.domain.com/fullchain.pem (success)
    ** DRY RUN: simulating 'certbot renew' close to cert expiry
    ** (The test certificates above have not been saved.)

    既然模拟成功,我们就使用crontab -e的命令来启用自动任务,命令行:

    sudo crontab -e

    添加配置:

    30 2 * * 1 /usr/local/bin/certbot-auto renew >> /var/log/le-renew.log


    上面的执行时间为:每周一半夜2点30分执行renew任务。

    你可以在命令行执行/usr/bin/certbot renew >> /var/log/le-renew.log看看是否执行正常,如果一切OK,那么我们的配置到此结束!

    重要说明:

    如果执行上面的命令后,提示 Cert not yet due for renewal ,那是因为 证书还没有过期

    如果证书没有过期,那么证书不会更新

  • 相关阅读:
    iOS开发-ScrollView图片缩放
    算法-随机不重复数列生成
    iOS开发-舒尔特表
    iOS开发-音乐播放
    iOS开发-简单的图片查看器
    iOS开发-Interface Builder的前世今生
    iOS开发-DatePicker控件
    iOS开发-UI基础Demo
    Objective-C-Category类别
    Objective-C面向对象之实现类
  • 原文地址:https://www.cnblogs.com/liuxm2017/p/10921512.html
Copyright © 2011-2022 走看看