介绍 (Introduction)
Let’s Encrypt is a service offering free SSL certificates through an automated API. The most popular Let’s Encrypt client is EFF’s Certbot.
让我们加密是一项通过自动API提供免费SSL证书的服务。 最受欢迎的“让我们加密”客户端是EFF的Certbot 。
Certbot offers a variety of ways to validate your domain, fetch certificates, and automatically configure Apache and Nginx. In this tutorial, we’ll discuss Certbot’s standalone mode and how to use it to secure other types of services, such as a mail server or a message broker like RabbitMQ.
Certbot提供了多种方法来验证您的域,获取证书以及自动配置Apache和Nginx。 在本教程中,我们将讨论Certbot的独立模式以及如何使用它来保护其他类型的服务,例如邮件服务器或RabbitMQ之类的消息代理。
We won’t discuss the details of SSL configuration, but when you are done you will have a valid certificate that is automatically renewed. Additionally, you will be able to automate reloading your service to pick up the renewed certificate.
我们不会讨论SSL配置的详细信息,但是完成后,您将拥有一个有效的证书,该证书会自动更新。 此外,您将能够自动重新加载服务以获取续订的证书。
先决条件 (Prerequisites)
Before starting this tutorial, you will need:
在开始本教程之前,您需要:
An CentOS 7 server with a non-root, sudo-enabled user, as detailed in this CentOS 7 initial server setup tutorial.
具有非root用户且启用了sudo的CentOS 7服务器,如本CentOS 7初始服务器设置教程中所述 。
A domain name pointed at your server, which you can accomplish by following “How to Set Up a Host Name with DigitalOcean.” This tutorial will use
example.com
throughout.指向服务器的域名,您可以按照“ 如何使用DigitalOcean设置主机名 ”来完成。 本教程将始终使用
example.com
。Port 80 or 443 must be unused on your server. If the service you’re trying to secure is on a machine with a web server that occupies both of those ports, you’ll need to use a different mode such as Certbot’s webroot mode.
服务器上必须未使用端口80 或 443。 如果您要保护的服务位于具有同时占用这两个端口的Web服务器的计算机上,则需要使用其他模式,例如Certbot的webroot模式 。
第1步-安装Certbot (Step 1 — Installing Certbot)
Certbot is packaged in an extra repository called Extra Packages for Enterprise Linux (EPEL). To enable this repository on CentOS 7, run the following yum
command:
Certbot打包在一个名为“企业Linux额外软件包” (EPEL)的额外存储库中。 要在CentOS 7上启用此存储库,请运行以下yum
命令:
- sudo yum --enablerepo=extras install epel-release sudo yum --enablerepo = extras安装epel-release
Afterwards, the certbot
package can be installed with yum
:
之后,可以使用yum
安装certbot
软件包:
- sudo yum install certbot sudo yum安装certbot
You may confirm your install was successful by calling the certbot
command:
您可以通过调用certbot
命令来确认安装成功:
- certbot --version certbot-版本
Output
certbot 0.31.0
Now that we have Certbot installed, let’s run it to get our certificate.
现在我们已经安装了Certbot,让我们运行它来获取我们的证书。
第2步-运行Certbot (Step 2 — Running Certbot)
Certbot needs to answer a cryptographic challenge issued by the Let’s Encrypt API in order to prove we control our domain. It uses ports 80
(HTTP) or 443
(HTTPS) to accomplish this. If you’re using a firewall, open up the appropriate port now. For firewalld
this would be something like the following:
Certbot需要回答Let's Encrypt API发出的加密挑战,以证明我们控制了我们的域。 它使用端口80
(HTTP)或443
(HTTPS)来完成此操作。 如果您使用的是防火墙,请立即打开相应的端口。 对于firewalld
这将类似于以下内容:
- sudo firewall-cmd --add-service=http sudo firewall-cmd --add-service = http
- sudo firewall-cmd --runtime-to-permanent sudo firewall-cmd-永久运行
Substitute https
for http
above if you’re using port 443.
如果您使用的是端口443,请用https
替换上面的http
。
We can now run Certbot to get our certificate. We’ll use the --standalone
option to tell Certbot to handle the challenge using its own built-in web server. The --preferred-challenges
option instructs Certbot to use port 80 or port 443. If you’re using port 80, you want --preferred-challenges http
. For port 443 it would be --preferred-challenges tls-sni
. Finally, the -d
flag is used to specify the domain you’re requesting a certificate for. You can add multiple -d
options to cover multiple domains in one certificate.
现在,我们可以运行Certbot来获取我们的证书。 我们将使用--standalone
选项来告诉Certbot使用其自己的内置Web服务器来应对挑战。 --preferred-challenges
选项指示Certbot使用端口80或端口443。如果使用端口80,则需要--preferred-challenges http
。 对于端口443,它将是--preferred-challenges tls-sni
。 最后, -d
标志用于指定您要为其申请证书的域。 您可以添加多个-d
选项以在一个证书中覆盖多个域。
sudo certbot certonly --standalone --preferred-challenges http -d example.com
sudo certbot certonly --standalone --preferred-challenges http -d example.com
When running the command, you will be prompted to enter an email address and agree to the terms of service. After doing so, you should see a message telling you the process was successful and where your certificates are stored:
运行该命令时,系统将提示您输入电子邮件地址并同意服务条款。 这样做之后,您应该看到一条消息,告诉您该过程已成功完成,并且证书的存储位置:
Output
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 2018-10-09. 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"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- 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
We’ve got our certificates. Let’s take a look at what we downloaded and how to use the files with our software.
我们有我们的证书。 让我们看一下下载的内容以及如何在我们的软件中使用文件。
步骤3 —配置您的应用程序 (Step 3 — Configuring Your Application)
Configuring your application for SSL is beyond the scope of this article, as each application has different requirements and configuration options, but let’s take a look at what Certbot has downloaded for us. Use ls
to list out the directory that holds our keys and certificates:
为您的SSL配置应用程序超出了本文的范围,因为每个应用程序都有不同的要求和配置选项,但是让我们来看看Certbot为我们下载了什么。 使用ls
列出包含我们的密钥和证书的目录:
sudo ls /etc/letsencrypt/live/example.com
须藤ls / etc / letsencrypt / live / example.com
Output
cert.pem chain.pem fullchain.pem privkey.pem README
The README
file in this directory has more information about each of these files. Most often you’ll only need two of these files:
此目录中的README
文件具有有关每个文件的更多信息。 通常,您仅需要以下两个文件:
privkey.pem
: This is the private key for the certificate. This needs to be kept safe and secret, which is why most of the/etc/letsencrypt
directory has very restrictive permissions and is accessible by only the root user. Most software configuration will refer to this as something similar tossl-certificate-key
orssl-certificate-key-file
.privkey.pem
:这是证书的私钥。 这需要保持安全和保密,这就是为什么大多数/etc/letsencrypt
目录具有非常严格的权限并且只能由root用户访问的原因。 大多数软件配置都将其称为ssl-certificate-key
或ssl-certificate-key-file
。fullchain.pem
: This is our certificate, bundled with all intermediate certificates. Most software will use this file for the actual certificate, and will refer to it in their configuration with a name like ‘ssl-certificate’.fullchain.pem
:这是我们的证书,与所有中间证书捆绑在一起。 大多数软件都会将此文件用作实际的证书,并且会在其配置中使用“ ssl-certificate”之类的名称进行引用。
For more information on the other files present, refer to the “Where are my certificates” section of the Certbot docs.
有关存在的其他文件的更多信息,请参阅Certbot文档的“ 我的证书在哪里 ”部分。
Some software will need its certificates in other formats, in other locations, or with other user permissions. It is best to leave everything in the letsencrypt
directory, and not change any permissions in there (permissions will just be overwritten upon renewal anyway), but sometimes that’s just not an option. In that case, you’ll need to write a script to move files and change permissions as needed. This script will need to be run whenever Certbot renews the certificates, which we’ll talk about next.
某些软件将需要其他格式的证书,其他位置的证书或具有其他用户权限的证书。 最好将所有内容保留在letsencrypt
目录中,并且不要在其中更改任何权限(无论如何,权限都将在续订时被覆盖),但这有时不是一个选择。 在这种情况下,您需要编写脚本来移动文件并根据需要更改权限。 每当Certbot续订证书时,都将需要运行此脚本,我们将在后面讨论。
步骤4 —启用自动证书续订 (Step 4 — Enabling Automatic Certificate Renewal)
Let’s Encrypt’s certificates are only valid for ninety days. This is to encourage users to automate their certificate renewal process. The certbot
package we installed includes a systemd timer to check for renewals twice a day, but it is disabled by default. Enable the timer by running the following command:
让我们加密的证书仅有效九十天。 这是为了鼓励用户自动化其证书续订过程。 我们安装的certbot
软件包包括一个systemd计时器,用于每天两次检查续订,但默认情况下处于禁用状态。 通过运行以下命令来启用计时器:
- sudo systemctl enable --now certbot-renew.timer sudo systemctl enable --now certbot-renew.timer
Output
Created symlink from /etc/systemd/system/timers.target.wants/certbot-renew.timer to /usr/lib/systemd/system/certbot-renew.timer.
You may verify the status of the timer using systemctl
:
您可以使用systemctl
验证计时器的状态:
- sudo systemctl status certbot-renew.timer sudo systemctl状态certbot-renew.timer
Output
● certbot-renew.timer - This is the timer to set the schedule for automated renewals
Loaded: loaded (/usr/lib/systemd/system/certbot-renew.timer; enabled; vendor preset: disabled)
Active: active (waiting) since Fri 2019-05-31 15:10:10 UTC; 48s ago
The timer should be active. Certbot will now automatically renew any certificates on this server whenever necessary.
计时器应处于活动状态。 Certbot现在将在必要时自动续订此服务器上的所有证书。
步骤5 —续订证书时运行任务 (Step 5 — Running Tasks When Certificates are Renewed)
Now that our certificates are renewing automatically, we need a way to run certain tasks after a renewal. We need to at least restart or reload our server to pick up the new certificates, and as mentioned in Step 3 we may need to manipulate the certificate files in some way to make them work with the software we’re using. This is the purpose of Certbot’s renew_hook
option.
现在我们的证书正在自动更新,我们需要一种在更新后运行某些任务的方法。 我们至少需要重新启动或重新加载服务器以获取新证书,如步骤3所述,我们可能需要以某种方式处理证书文件,以使其与我们使用的软件一起使用。 这是Certbot的renew_hook
选项的目的。
To add a renew_hook
, we update Certbot’s renewal config file. Certbot remembers all the details of how you first fetched the certificate, and will run with the same options upon renewal. We just need to add in our hook. Open the config file with you favorite editor:
要添加renew_hook
,我们将更新Certbot的续订配置文件。 Certbot会记住您如何首次获取证书的所有详细信息,并且在续订时将使用相同的选项运行。 我们只需要添加我们的钩子即可。 使用喜欢的编辑器打开配置文件:
- sudo vi /etc/letsencrypt/renewal/example.com.conf 须藤vi /etc/letsencrypt/renewal/example.com.conf
A text file will open with some configuration options. Add your hook on the last line:
将打开一个文本文件,其中包含一些配置选项。 在最后一行添加您的钩子:
renew_hook = systemctl reload rabbitmq
Update the command above to whatever you need to run to reload your server or run your custom file munging script. Usually, on CentOS, you’ll mostly be using systemctl
to reload a service. Save and close the file, then run a Certbot dry run to make sure the syntax is ok:
将上面的命令更新为您需要运行以重新加载服务器或运行自定义文件munging脚本的任何内容。 通常,在CentOS上,您通常会使用systemctl
重新加载服务。 保存并关闭文件,然后运行Certbot空运行以确保语法正确:
- sudo certbot renew --dry-run sudo certbot更新-干运行
If you see no errors, you’re all set. Certbot is set to renew when necessary and run any commands needed to get your service using the new files.
如果您没有看到任何错误,则说明一切就绪。 Certbot设置为在必要时进行续订,并运行使用新文件获取服务所需的任何命令。
结论 (Conclusion)
In this tutorial, we’ve installed the Certbot Let’s Encrypt client, downloaded an SSL certificate using standalone mode, and enabled automatic renewals with renew hooks. This should give you a good start on using Let’s Encrypt certificates with services other than your typical web server.
在本教程中,我们安装了Certbot Let's Encrypt客户端,使用独立模式下载了SSL证书,并启用了具有续订挂钩的自动续订。 这应该为您将Let's Encrypt证书与典型Web服务器以外的服务一起使用提供了一个良好的开端。
For more information, please refer to Certbot’s documentation.
有关更多信息,请参阅Certbot的文档 。