手把手教你在Nginx上使用CertBot

前言

本身作了一个iOS App,须要访问本身的网站获取数据,可是系统默认只能直接访问https的网站。不想让应用改用http的服务。所以,研究如何启用https,本文便是介绍如何在CentOS上配合Nginx使用CertBot。html

环境nginx

  • CentOS(CentOS Linux release 7.2.1511git

  • Nginx(nginx version: nginx/1.6.3github

  • ExpressJS应用web

安装CertBot

命令行,键入:浏览器

sudo yum install epel-release
sudo yum install certbot

配置Nginx

这里我不想使用CertBot的standalone模式,这个模式虽然能够配置好服务器,可是之后Renew的时候,须要让服务中止一下,再启动。所以抛弃这个模式,咱们使用Webroot配置模式。bash

由于,CertBot在验证服务器域名的时候,会生成一个随机文件,而后CertBot的服务器会经过HTTP访问你的这个文件,所以要确保你的Nginx配置好,以即可以访问到这个文件。服务器

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

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,由于个人应用是经过NodeJSExpressJS写的,若是修改源代码的话,比较麻烦。所以我就让检验的连接指向了nginx默认的文件夹下。ide

接着从新加载Nginx配置:

sudo service nginx reload

而后在命令行输入:

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

上面记得替换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天,咱们须要设置自动更新的功能,帮咱们自动更新证书的时效。

自动更新证书

先在命令行模拟证书更新:

sudo certbot 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/bin/certbot renew  >> /var/log/le-renew.log

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

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

参考

相关文章
相关标签/搜索