介绍
Let's Encrypt是一个证书授权机构(CA),它提供了一个简单的方法来获取和安装免费的TLS/SSL证书,从而在Web服务器上启用加密HTTPS。它通过提供软件客户端Certbot来简化这个过程,这将自动化大多数步骤。 在本教程中,我们将向您展示如何使用Certbot获得免费SSL证书,并在FreeBSD服务器上使用它。我们还将向您展示如何自动更新您的SSL证书。 我们将在本教程中使用默认的Nginx配置文件,而不是单独的服务器封锁文件。
<$>[注] 注: 截至 2022 年 7 月 1 日,DigitalOcean 不再支持通过控制面板或 API 创建新的 FreeBSD Droplets。
前提条件
为了完成这个教程,你需要:
- 一个 FreeBSD 服务器. 如果你是新手使用 FreeBSD 11,你可以遵循 此指南来帮助你开始。
- Nginx 已安装并配置在你的服务器上。 有关如何设置此项指南,请参阅我们在 How to Install Nginx on FreeBSD 11.2上的指南。
- 一个注册域名,你拥有和控制. 如果你还没有注册域名,你可以注册一个在那里许多域名注册商之一(例如 Namecheap, GoDaddy 等)。
- A DNS **A 记录,指示你的域名到你的服务器的公共 IP 地址。 您可以遵循 这个主机名指南
一旦您完成了这些前提条件,让我们继续安装Certbot,Let’s Encrypt客户端软件。
步骤1:安装Certbot
使用 Let’s Encrypt 获取 SSL 证书的第一步是将certbot
客户端软件安装到您的服务器上。Certbot 的最新版本可以通过 FreeBSD 的 ports system 来从源头安装。
首先,请获取一个压缩的端口树的截图:
1sudo portsnap fetch
此命令可能需要几分钟才能完成. 完成后,请提取快照:
1sudo portsnap extract
此命令也可能需要一段时间才能完成,一旦完成,请导航到端口树中的py-certbot
目录:
1cd /usr/ports/security/py-certbot
然后使用做
命令与sudo
特权下载并编译Certbot源代码:
1sudo make install clean
接下来,导航到端口树内的「py-certbot-nginx」目录:
1cd /usr/ports/security/py-certbot-nginx
从此目录中再次运行make
命令. 这将安装Certbot的nginx
插件,我们将使用它来获取SSL证书:
1sudo make install clean
During this plugin’s installation, you will see a couple of blue dialog windows pop up that look like this:
These give you the option to install documentation for the plugin and its dependencies. For the purposes of this tutorial, you can just press
ENTER
to accept the default options in these windows which will install this documentation.
The certbot
Let's Encrypt client is now ready to use. Before obtaining your certificates, though, it’s important to set up a firewall and allow HTTPS traffic through it, if you haven’t already done so.
步骤 2 – 设置防火墙并允许 HTTPS 访问
如果你已经在你的服务器上设置了防火墙,你应该确保它允许HTTPS访问(通过端口 443
)。 如果你还没有设置防火墙,你可以这样做,按照本步骤中描述的指示。
打开你的 rc.conf
文件,位于 /etc/
目录中,使用你的偏好编辑器。
1sudo ee /etc/rc.conf
此文件用于告知 FreeBSD 每当机器启动时应该启动哪些服务。
1[label /etc/rc.conf]
2. . .
3nginx_enable="YES"
4firewall_enable="YES"
5firewall_type="workstation"
6firewall_myservices="22/tcp 80/tcp 443/tcp"
7firewall_allowservices="any"
以下是这些指令和它们的设置所做的事情:
- `firewall_enable="YES"' - 这允许防火墙在服务器启动时启动。
firewall_type="workstation" - FreeBSD提供几种默认类型的防火墙,每种都具有稍微不同的配置。 通过声明
workstation`类型,防火墙只会使用状态规则来保护该服务器。firewall_myservices="22/tcp 80/tcp 443/tcp" - 该 'firewall_myservices' 指令是您可以列出您希望通过防火墙允许的 TCP 端口。 在此示例中,我们正在指定端口
22和
443` 以允许 SSH, HTTP 和 HTT
添加这些行后,保存文件并关闭编辑器,按CTRL + C
,键入退出
,然后按ENTER
。
然后,启动ipfw
防火墙服务. 因为这是你第一次在这个服务器上启动防火墙,有可能这样做会导致你的服务器停滞,使其无法通过SSH访问。
1sudo nohup service ipfw start >/tmp/ipfw.log 2>&1
但是,如果您使用csh
或tcsh
,此重定向将导致`模糊的输出重定向。
1sudo nohup service ipfw start >&/tmp/ipfw.log
在未来,您可以像任何其他服务一样管理ipfw
防火墙,例如,要停止、启动,然后重新启动服务,您将执行以下命令:
1sudo service ipfw stop
2sudo service ipfw start
3sudo service ipfw restart
有了配置的防火墙,您现在已经准备好运行Certbot并获取您的证书。
步骤三:获得SSL证书
Certbot 提供了通过各种插件获取 SSL 证书的多种方式. nginx
插件将负责重新配置 Nginx 并重新加载 config 文件:
1sudo certbot --nginx -d example.com -d www.example.com
如果这是您第一次在这个服务器上运行certbot
,客户端将提示您输入电子邮件地址并同意让我们加密
服务条款。 这样做后,certbot
将与让我们加密
服务器进行通信,然后运行一个挑战,以验证您控制的域名,您正在寻求证书。
如果挑战成功,Certbot将询问您如何配置您的HTTPS设置:
1[secondary_label Output]
2. . .
3Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
4-------------------------------------------------------------------------------
51: No redirect - Make no further changes to the webserver configuration.
62: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
7new sites, or if you're confident your site works on HTTPS. You can undo this
8change by editing your web server's configuration.
9-------------------------------------------------------------------------------
10Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
选择您的选择,然后点击ENTER
。这将更新配置并重新加载 Nginx 以获取新的设置。certbot
将包装一个消息,告诉您流程成功,您的证书存储在哪里:
1[secondary_label Output]
2IMPORTANT NOTES:
3- Congratulations! Your certificate and chain have been saved at:
4 /usr/local/etc/letsencrypt/live/example.com/fullchain.pem
5 Your key file has been saved at:
6 /usr/local/etc/letsencrypt/live/example.com/privkey.pem
7 Your cert will expire on 2018-09-24. To obtain a new or tweaked
8 version of this certificate in the future, simply run certbot
9 again. To non-interactively renew *all* of your certificates, run
10 "certbot renew"
11- Your account credentials have been saved in your Certbot
12 configuration directory at /usr/local/etc/letsencrypt. You should
13 make a secure backup of this folder now. This configuration
14 directory will also contain certificates and private keys obtained
15 by Certbot so making regular backups of this folder is ideal.
16- If you like Certbot, please consider supporting our work by:
17 Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
18 Donating to EFF: https://eff.org/donate-le
您的证书现在已下载,安装和配置。尝试使用https://
重新加载您的网站,并注意到您的浏览器的安全指标。它应该表示该网站是正确的安全,通常是绿色锁定图标。如果您使用SSL Labs Server Test(https://www.ssllabs.com/ssltest/)来测试您的服务器,它将获得 A等级。
确认您可以通过HTTPS访问您的网站后,您可以进入本教程的最后一步,您将确认您可以更新您的证书,然后配置一个程序来自动更新它们。
步骤 4 – 验证 Certbot 自动更新
Let's Encrypt 的证书仅有效 90 天,这是为了鼓励用户自动化其证书更新过程。本步骤描述了如何通过设置一个cron
任务来自动化证书更新。
1sudo certbot renew --dry-run
如果您没有看到任何错误,您都已设置为创建一个新的 crontab:
1sudo crontab -e
将下列内容添加到新文件中,该文件将告诉cron
每天中午和深夜两次运行certbot renew
命令。
10 0,12 * * * /usr/local/bin/certbot renew
请注意,由于您在crontab -e
命令之前使用了sudo
,所以此操作将作为 root运行,这是必要的,因为 certbot 需要超级用户权限才能运行。
结论
在本教程中,我们安装了 Let’s Encrypt 客户端certbot
,下载了我们的域的 SSL 证书,配置了 Nginx 以使用这些证书,并设置了自动证书更新。