Certbot生成HTTPS证书

技术教程 风为裳 3浏览 0评论 繁體

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

安装 certbot

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

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

使用 standalone 模式

certbot 还有另外一种模式 --standalone , 这种模式不需要指定网站根目录,他会自动启用服务器的443端口,来验证域名的归属。我们有其他服务(例如nginx)占用了443端口,就必须先停止这些服务,在证书生成完毕后,再启用。

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 + nginx 与 nginx 是完全不同的两种方式)
先确定一个 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生成HTTPS证书

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址