使用 certbot 自动化生成 https 证书

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

基本操作

0 准备工作

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

1 登录服务器

my-pc$ ssh user@you.domain.com

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): you@gmail.com  --------> 输入你的邮箱创建账号(只有第一次使用时会出现)

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 you@gmail.com --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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,829评论 1 331
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,603评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,846评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,600评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,780评论 3 272
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,695评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,136评论 2 293
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,862评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,453评论 0 229
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,942评论 2 233
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,347评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,790评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,293评论 3 221
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,839评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,448评论 0 181
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,564评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,623评论 2 249

推荐阅读更多精彩内容