使用 Let's Encrypt 生成 SSL 证书

2016-09-12 4087点热度 1人点赞

由于 https 必须使用 SSL 证书,而免费的 Wosign 的 SSL 证书由于各种问题实在影响使用,所以我们使用开源项目 Let's Encrypt 来为我们生成证书,达到低成本的目的。

项目官网 https://letsencrypt.org/
项目地址 https://github.com/letsencrypt/

Let's Encrypt 在官网上提供了很多说明与文档,并且有各种版本的程序的链接。但实际上,我们可以直接使用官方推荐的 certbot 来自动创建与生成证书,certbot 官网链接为 https://certbot.eff.org/


操作流程

1 、下载并安装 certbot

由于官网很简洁的提供了多版本支持,所以只需要选择对应的组件与环境即可,本例使用 Nginx 与 CentOS7

选择好后,可以直接看到安装说明

yum install epel-release
yum install certbot

2 、生成所需要的证书

安装好后,我们可以使用图形化的方式生成,也可以使用命令的方式生成。但是无论哪种方式,都会通过 http://$server_name/.well-known/来访问并验证您的域名。本文主要讲解命令行的方式

临时开放防火墙 80 端口:firewall-cmd --add-service=http

图形化生成的命令:certbot certonly
命令行生成的命令:certbot certonly --email [email protected] --agree-tos --webroot -w /website/ -d example.com

PS:--email [email protected] 代表生成证书的所有者邮箱,--agree-tos 代表自动同意使用协议,--webroot 代表使用 website 的方式验证域名,-w /website/代表站点根目录,-d example.com 代表生成证书的域名

例 2:certbot certonly --email [email protected] --agree-tos --webroot -w /website/www/ -d hello.example.com -d www.example.com -w /website/blog/ -d blog.example.com

解释:创建一个证书,包含以下三个域名 example.com   www.example.com   blog.example.com,验证时对 hello.example.com www.example.com 两个域名在/website/www 里验证,对 blog.example.com 在/website/blog/里验证。同时创建一个站点配置文件,名为 hello.example.com,并且以上三个域名的 SSL 都在一个文件内

成功生成 SSL 证书时的提示如下

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
 /etc/letsencrypt/live/hello.example.com/fullchain.pem. Your cert
 will expire on 2016-12-11. 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 lose your account credentials, you can recover through
 e-mails sent to [email protected].
 - Your account credentials have been saved in your Certbot
 configuration directory at /etc/letsencrypt. You should make a
 secure backup of this folder now. This configuration directory will
 also contain certificates and private keys obtained by Certbot so
 making regular backups of this folder is ideal.
 - 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

3 、将来重新颁发 SSL 证书

命令为 certbot renew --dry-run,运行后会重新颁发所有证书(通过检查站点配置文件查看)。

如果想自动颁发,则可以使用 crontab 计划任务,对 certbot renew --quiet 周期性执行即可,它会检查当前证书是否过期,如果过期则重颁发,未过期则不操作(最快不能超过每天 2 次)

vi /etc/crontab
添加以下内容即可(每周一执行一次)
 0 0 * * 0 root certbot renew --quiet && systemctl reload nginx.service

4 、编辑站点配置文件

ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;

5 、关于部分站点可能使用 301 重定向 https 的问题

由于重定向,所以 http 可能出现问题,解决方法如下,新增一个 location /.well-known/

server {
  listen 80;
  listen [::]:80;
  server_name www.example.com;

  location /.well-known/ {
  add_header Content-Type 'text/plain;';
  root /website/www;
  }

  location / {
  return 301 https://$server_name$request_uri;
  }
}

StarryVoid

Have a good time