手把手教你在Ngin上使用CertBot

前言

自己做了一个iOS App,需要访问自己的网站获取数据,但是系统默认只能直接访问https的网站。不想让应用改用http的服务。因此,研究如何启用https,本文即是介绍如何在CentOS上配合Nginx使用CertBot。

环境

  1. CentOS(CentOS Linux release 7.2.1511)

  2. Nginx(nginx version: nginx/1.6.3)

  3. ExpressJS应用

安装CertBot

命令行,键入:

sudo yum install epel-releasesudo yum install certbot

配置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,因为我的应用是通过NodeJS的ExpressJS写的,如果修改源代码的话,比较麻烦。因此我就让检验的链接指向了nginx默认的文件夹下。

接着重新加载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,那么我们的配置到此结束!

参考

  1. Certbot官网

  2. LetsEncrypt Nginx and Proxy Apps on Ubuntu

  3. nginx配置示例

关键字:Nginx, SSL, ssl证书, CentOS


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部