安装letsencrypt ssl证书(nginx centos6)

简介(https://letsencrypt.org)

letsencrypt是一个免费ssl提供商。证书有效期3个月,每3个月须要renew证书一次。下面的教程介绍了如何安装配置证书,及如何经过cron自动renew证书.html

安装CertBot(https://certbot.eff.org/)

命令行,键入:nginx

$ sudo yum install epel-releaseweb

$ wget https://dl.eff.org/certbot-auto浏览器

$ chmod a+x certbot-auto服务器

$ cp certbot-auto /usr/bin/certbot-autodom

配置Nginx

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

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

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

location ^~ /.well-known/acme-challenge/ {this

   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-auto 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-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/bin/certbot-auto renew --quiet >> /var/log/letsencrypt/renew.log

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

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

相关文章
相关标签/搜索