使用 certbot 自动化生成 https 证书

Let’s Encrypt 是一个自动签发 https 证书的免费项目
Certbot 是 Let’s Encrypt 官方推荐的证书生成客户端工具

基本操作

0 准备工作

将要签发证书的域名(如 you.domain.com) 解析到一台可在外网正常访问的服务器

1 登录服务器

my-pc$ ssh [email protected]

2 安装 certbot

这里以 Ubuntu 16.04 为例
各个系统的安装方式不一样,可以在官网首页查阅

my-server$ cat /etc/issue
Ubuntu 16.04.2 LTS \n \l

my-server$ sudo apt-get update
my-server$ sudo apt-get install software-properties-common
my-server$ sudo add-apt-repository ppa:certbot/certbot
my-server$ sudo apt-get update
my-server$ sudo apt-get install certbot 
my-server$ certbot --version
certbot 0.19.0

3 检查端口占用

如果服务器上的 443 端口正在被占用,请先关闭对应的服务进程
否则可能个导致 certbot 运行出错

4 以命令交互方式开始制作证书

my-server$ certbot certonly    --------> 开启命令

Saving debug log to /var/log/letsencrypt/letsencrypt.log

How would you like to authenticate with the ACME CA?
1: Spin up a temporary webserver (standalone)
2: Place files in webroot directory (webroot)

Select the appropriate number [1-2] then [enter] 
(press 'c' to cancel): 1  --------> 这里我们选择 1 standalone 模式
Plugins selected: Authenticator standalone, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]  --------> 输入你的邮箱创建账号(只有第一次使用时会出现)

Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree
in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
(A)gree/(C)ancel: a  --------> 选择 A 同意 (只有第一次使用时会出现)

Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
(Y)es/(N)o: y  --------> 选择 Y 或 N (只有第一次使用时会出现)
Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel): you.domain.com   --------> 这里输入要制作证书的域名

Obtaining a new certificate
Performing the following challenges:
tls-sni-01 challenge for you.domain.com
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/you.domain.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/you.domain.com/privkey.pem
   Your cert will expire on 2018-01-26. 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


my-server$ ls /etc/letsencrypt/live/you.domain.com/  --------> 证书文件目录
cert.pem  chain.pem  fullchain.pem  privkey.pem  README   

看到 IMPORTANT NOTES 下方出现 Congratulations 就表明证书已经成功签发
可以进入 /etc/letsencrypt/live/you.domain.com/ 目录查看相关生成的文件

进阶操作

使用 80 端口

默认 standalone 是使用 443 端口,也就是说要停止服务器现在占用 443 端口的进程
我们也可以将其改为使用 80 端口,同样道理,这时需要停止 80 端口的占用

my-server$ certbot certonly --standalone -n --agree-tos --email [email protected] --preferred-challenges http -d you.domain.com

-n 非交互式
--email 指定账户
--agree-tos 同意服务协议

使用 nginx 模式

安装 nginx 和插件

my-server$ apt-get install nginx
my-server$ apt-get install python-certbot-nginx

增加 nginx 配置

server {
        server_name you.domain.com;
}

制作证书

my-server$ certbot --nginx -d you.domain.com

使用 webroot 模式

standalone 需要停止服务器,nginx 模式需要修改配置,如果需要在不影响服务器正常运行的情况下制作证书,可以选择 webroot 模式
有个前提,需要你的服务的 80 端口上运行一个能处理静态文件的 web 服务,我们就以 nginx 为例 (注意,webroot + nginxnginx 是完全不同的两种方式)

先确定一个 webroot 目录,这里以 /var/www/html/ 为例

server {
        listen 80 default_server;
        location / {
            alias /var/www/html/;
        }
}

重启 nginx ,制作证书

my-server$ nginx -s reload
my-server$ certbot certonly --webroot -w /var/www/html/ -d you.domain.com

在执行命令后,certbot 会向指定的 webroot 目录中添加一个随机文件,随后 letsencrypt 会通过 http 访问那个文件,比如 http://you.domain.com/.well-known/acme-challenge/gdEMr7ZXOiZE51he9QwuvnbrrTnkwlpFhNAcArBt2uE, 如果能返回正确的数据则通过认证,否则认证失败
所以,即使不使用 nginx,只要能保证你的服务器能正确的返回 webroot 下的文件就行

使用 manual 模式

manual 模式其实和 webroot 类似,主要的区别就是 certbot 自动向 webroot 添加文件的过程,变为你自己手动完成

my-server$ certbot certonly --manual -d you.domain.com

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for you.domain.com

NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
(Y)es/(N)o: y    --------> 选 Y

Create a file containing just this data:

_Pamo3hOMx37O-KHIioWO1P0hbTLZusEZ2a2iCf7PDQ.8CyQk-SfDMR10nk7DuGBFA4IEpVhm1OySU-nSItD1BE

And make it available on your web server at this URL:

http://you.domain.com/.well-known/acme-challenge/_Pamo3hOMx37O-KHIioWO1P0hbTLZusEZ2a2iCf7PDQ

Press Enter to Continue  
--------> 你需要保证 http://you.domain.com/.well-known/acme-challenge/_Pamo3hOMx37O-KHIioWO1P0hbTLZusEZ2a2iCf7PDQ 
这个 url 返回的结果为 _Pamo3hOMx37O-KHIioWO1P0hbTLZusEZ2a2iCf7PDQ.8CyQk-SfDMR10nk7DuGBFA4IEpVhm1OySU-nSItD1BE (这个数据是随机的,每次都不一样) 
当你做到后,按下回车键继续

Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/you.domain.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/you.domain.com/privkey.pem
   Your cert will expire on 2018-01-27. 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


查看和管理当前服务器上的所有证书

my-server$ certbot certificates

使用 https 证书

以 nginx 为例

server {
    listen 80;
    listen 443 ssl;
    server_name you.domain.com;
    ssl_certificate /etc/letsencrypt/live/you.domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/you.domain.com/privkey.pem; 

    # ...
}

letsencrypt 有什么限制

标准限制

  • 同一个顶级域名下的二级域名,一周做多申请 20 个
  • 一个域名一周最多申请 5 次
  • 1 小时最多允许失败 5 次
  • 请求频率需要小于 20 次/s
  • 一个 ip 3 小时内最多创建 10 个账户
  • 一个账户最多同时存在 300 个 pending 的审核

测试模式限制

在 certbot 命令后加上 --staging 参数,限制更松一些,但不是正式证书

  • 同一个顶级域名下的二级域名,一周做多申请 30000 个
  • 一个域名一周最多申请 30000 次
  • 1 小时最多允许失败 60 次
  • 一个 ip 3小时内最多创建 50 个账户

签发泛域名证书

先升级到最新版本的 certbot,然后

my-server$ certbot certonly -d *.aabbcc.com --manual --server https://acme-v02.api.letsencrypt.org/directory --preferred-challenges dns

你可能感兴趣的:(使用 certbot 自动化生成 https 证书)