如何在 Ubuntu20.04 上使用 Let’s Encrypt 给 Nginx 加密(SSL)
Let’s Encrypt 是Certificate Authority机构(CA)的一个加密证书。它可以帮助我们免费获得和安装TLS / SSL证书,获得这个证书,就可以在我们的服务器上启动HTTPS加密。Let’s Encrypt 提供一个软件Certbot来简化安装步骤,可以让大多数安装步骤已经全自动化进行。Certbot已经可以在 Apache 和 Nginx 上全自动安装了。
在本教程中,我们一起来学习如何使用 Certbot 在 Ubuntu 20.04 上获得 Nginx 的免费 SSL 证书,以及设置如何续订这个证书。
接下来,我们会使用单独的 Nginx 服务器配置文件,我建议大家也使用单独的配置文件,具体操作方法可以看我们之前的文章《如何在 Ubuntu 20.04 中安装 Nginx》
准备工作
要学习本教程,我们需要先准备:
- 有 sudo 的 root 用户权限,大家可根据《Ubuntu 120.04初始服务器设置》(撰写中,稍后上线)指南来配置自己的服务器。
- 注册域名。本教程使用
example.com
作为范例。大家可以在阿里云购买一个域名,.com 域名一年60多块钱。 -
为服务器设置了以下两个DNS记录。
example.com
的A记录指向服务器公网IPwww.example.com
的A记录指向服务器公网IP
- 请按照《如何在 Ubuntu 20.04 中安装 Nginx》教程来确保一个域对应一个server block。在本教程中,我们使用
/etc/nginx/sites-available/example.com
来作为操作对象。
第1步-安装Certbot
首先我们得现在服务器上安装 Certbot
安装Certbot 及 Nginx 插件 apt
:
sudo apt install certbot python3-certbot-nginx
选择 Y 后开始安装,安装完毕我们就可以使用 Certbot 了。
接下来我们要验证一下 Certbot 是否能为Nginx 自动配置 SSL。
第2步 确认 Nginx 的配置
Certbot 需要能够在 Nginx 配置中找到正确的 block 才能自动配置 SSL。 具体来说,Certbot 是通过对比 server_name
与我们请求证书的域相匹配的来实现的。
正如我们之前在 Nginx 安装教程中 关于服务器块( block )设置步骤中提到的方法。首先我们要确定server_name
要与/etc/nginx/sites-available/example.com
建立链接。
让我们来检查一下配置文件,我们可以使用nano
编辑器或其他任何编辑器打开下面的配置文件:
sudo nano /etc/nginx/sites-available/example.com
找到server_name
这一行,大概是如下的样子:
...
server_name example.com www.example.com;
...
如果你的配置文件和上述样例一样,那么我们退出编辑器继续跟着教程往后走。
如果不是,请根据教程进行修改,如果实在搞不懂,请先看我们之前学习的《Nginx 安装教程》,在其中有解答。
让我们来验证一下,配置文件是否正确。
sudo nginx -t
如果输出结果出现错误信息,请重新打开配置文件仔细检查。如果输出结果正确,那么让我们来重新加载新配置。
sudo systemctl reload nginx
赞,走到这一步。我们的 Certbot 已经可以正确找到我们的 server block
并且可以自动更新了。
接下来,让我们更新防火墙配置,让它允许HTTPS对外通信。
第3步 让我们的防火墙允许HTTPS
如果你的服务器启动了防火墙,那么我们需要告诉防火墙要允许HTTPS通过。首先我们来查看一下当前状态。
sudo ufw status
输出结果如果类似下面这样:
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
说明防火墙允许HTTPS通信。但如果你的服务器不允许,那么请执行以下命令。
sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'
然后再让我们查看一下状态的变化。
sudo ufw status
输出结果变为:
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)
好的,接下来,让我们运行 Certbot 来获取我们的安全证书吧。
第4步:获取 SSL 证书
Certbot 提供了获得 SSL 的方法。Nginx Plugins(插件) 帮助我们在必要的时候重新加载配置文件。让我们来开启这个插件。
sudo certbot --nginx -d example.com -d www.example.com
certbot
与--nginx
插件一起运行,-d
用于指定我们希望获得证书的域名。
如果我们是第一次运行,那么 Certbot
将提示我们输入邮箱并阅读服务条款。
Invalid email address: .
Enter email address (used for urgent renewal and security notices)
If you really want to skip this, you can run the client with
--register-unsafely-without-email but make sure you then backup your account key
from /etc/letsencrypt/accounts
(Enter 'c' to cancel): nginx@kalasearch.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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:
如果这是您第一次运行certbot
,将提示您输入电子邮件地址并同意服务条款。
完成此操作后,certbot
开始与 Let's Encrypt 服务器通信,然后开始验证我们是否是这个域名的真正拥有者。
如果成功,certbot
会继续询问我们如何配置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):
根据我们的需求来进行选择,然后回车。配置文件就会更新,Nginx 也会重新加载。Certbot
会显示一条消息,告诉我们整个过程已经完成,证书存储在服务器的什么位置上:
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Your cert will expire on 2020-11-01. 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
我们的证书已经下载并已经加载成功。我们可以使用 https://
来进行访问。如果访问成功并且浏览器地址栏前面已经出现带锁的标志,那么说明我们的网站已经获得A级保护。
接下来,我们学习如何续订这个安全证书。
第5步:验证 Certbot 自动续订
我们获得的加密证书只有 90 天有效期,但不用担心,Certbot
会帮我们解决续订问题,它会每天运行两次systemd
监测程序来检查域名证书是否快到期。如果域名证书在近 30 天到期,它会自动续订这些域名的证书。
我们可以输入以下命令来检查systemctl
的状态:
sudo systemctl status certbot.timer
kalasearch@chuan-server:~$ sudo systemctl status certbot.timer
● certbot.timer - Run certbot twice daily
Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: enabled)
Active: active (waiting) since Sat 2020-08-01 03:37:47 UTC; 46min ago
Trigger: Sat 2020-08-01 12:54:45 UTC; 8h left
Triggers: ● certbot.service
Aug 01 03:37:47 chuan-server systemd[1]: Started Run certbot twice daily.
要测试域名证书的续订过程,我们可以输入以下命令:
sudo certbot renew --dry-run
如果输出结果没有任何错误,则表明一切就绪。如果在未来,续订证书发生问题,那么也不用担心,Let's Encrypt 会通过前面几步中我们留的邮箱联系我们,通知我们域名证书即将过期。
总结
在本教程中,我们学习了如何安装 Let's Encrypt 的客户端 certbot。以及如何使用certbot 为我们的域名下载了 SSL 证书。以及学习了如何设置 certbot 让它自动帮我们更新证书。如果你对使用 Certbot 还有疑问,可以在本文下面留言,也可以查看它的官方文档。
扩展阅读
Docker系列教程
- Docker 入门指南:如何在 Ubuntu 上安装和使用 Docker
- Docker 删除命令手册:如何在 Docker 中删除镜像、容器和卷
- 如何在 Docker 容器和主机之间共享数据
- 如何安装和使用Docker Compose
- 如何在 Docker 容器之间共享数据
- 如何在 Centos 7 中安装和使用 Docker
- 如何在 debian 10 中安装和使用 Docker
Nginx系列教程