Certbot 是 Let's Encrypt 官方推荐的获取证书的客户端,可以帮助我们获取免费的 Let's Encrypt 证书。本文详细介绍如何在 Ubuntu 18.04 上手动安装 certbot 并为网站添加 SSL 证书以启用 https 。注意,以下步骤基于 LNMP ,如果服务器未安装 LNMP ,请参考文章“Ubuntu 18.04 手动搭建LNMP环境”配置服务器环境。以下操作是在 root 账号下进行的,非 root 账号需提升到 root 权限。

安装 certbot

添加 certbot 软件源

终端中输入以下命令添加 certbot 软件源:

apt install software-properties-common -y
add-apt-repository ppa:certbot/certbot

以上命令执行后终端提示如下:

root@opKVM:~# add-apt-repository ppa:certbot/certbot
 This is the PPA for packages prepared by Debian Let's Encrypt Team and backported for Ubuntu(s).
 More info: https://launchpad.net/~certbot/+archive/ubuntu/certbot
Press [ENTER] to continue or Ctrl-c to cancel adding it.

按回车键继续,完成软件源的安装。

安装 certbot

使用如下命令安装 certbot:

apt-get update && apt-get install python-certbot-nginx -y

获取 SSL 证书

使用 certbot 自动获取 SSL 证书需要满足以下条件:
1、配置好 nginx 虚拟主机。
2、设置好域名 A 记录且 A 记录已生效,能够正常解析到虚拟主机。
满足以上两个条件后使用如下命令获取 SSL 证书:

certbot

命令执行后终端返回信息如下:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 

输入 email 地址,回车继续,终端中返回信息如下:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: 

输入 A 并回车接受 TOS 并继续,终端中返回信息如下:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: 

此处询问是否愿意与 Electronic Frontier Foundation 分享你的电子邮件地址,输入 N 回车并继续,终端返回信息如下:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: timelate.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 

此处列出了服务器上的所有虚拟主机,输入需要签发证书的域名序号,例如 1 ,回车并继续。不输入数字直接回车则会为所有域名获取 SSL 证书;输入字母 c 并回车则取消证书申请。输入数字 1 后开始获取证书并自动修改 nginx 虚拟主机配置以启用 https 。终端中返回信息如下:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 

此处询问是否将 http 重定向到 https ,输入数字 2 并回车,以启用重定向。终端中返回以下信息时即说明 SSL 证书获取成功且网站成功启用 https ,刷新网站即可看到效果:

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/timelate.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/timelate.com/privkey.pem
   Your cert will expire on 2019-03-25. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. 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

证书自动续签

Let's Encrypt 签发的 SSL 证书有效期只有 90 天,所以在过期之前,需要自动更新 SSL 证书。最新版 certbot 会自动添加更新脚本到 /etc/cron.d 里,此脚本每隔 12 小时运行一次,并将续订任何在30天内到期的证书。输入以下命令检测自动续签是否工作:

certbot renew --dry-run

命令运行后如果没有报错,即说明自动续签可以正常工作。

通过以上步骤便可在 Ubuntu 18.04 上手动安装 certbot 并为网站添加 SSL 证书以启用 https ,本文结束。

文章目录