如何使用Certbot独立模式检索让我们在Debian 10上加密SSL证书

介绍 (Introduction)

Let’s Encrypt is a service that offers free SSL certificates through an automated API. The most popular Let’s Encrypt client is EFF’s Certbot client.

让我们加密是一项通过自动API提供免费SSL证书的服务。 最受欢迎的Let's Encrypt客户端是EFFCertbot客户端。

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提供了多种方法来验证您的域,获取证书以及自动配置ApacheNginx 。 在本教程中,我们将讨论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:

在开始本教程之前,您需要:

  • A Debian 10 server, a non-root user with sudo privileges, and a basic firewall, as detailed in this Debian 10 server setup tutorial.

    Debian 10服务器,具有sudo特权的非root用户以及基本防火墙,如本Debian 10服务器设置教程中所述

  • A domain name pointed at your server, which you can accomplish by following this documentation on creating DNS records on DigitalOcean.

    指向服务器的域名,您可以通过遵循此文档在DigitalOcean上创建DNS记录来完成。

  • 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 or DNS-based challenge mode.

    服务器上必须未使用端口80 443 。 如果您要保护的服务位于具有同时占用这两个端口的Web服务器的计算机上,则需要使用其他模式,例如Certbot的webroot模式或基于DNS的质询模式。

第1步-安装Certbot (Step 1 — Installing Certbot)

Debian 10 includes the Certbot client in their default repository, and it should be up-to-date enough for basic use. If you need to do DNS-based challenges or use other newer Certbot features, you should instead install from the buster-backports repo as instructed by the official Certbot documentation.

Debian 10在其默认存储库中包括了Certbot客户端,并且它应该已经足够最新,可以基本使用。 如果您需要进行基于DNS的挑战或使用其他新的Certbot功能,则应按照Certbot官方文档的说明,从buster-backports库中进行安装。

Update your package list:

更新您的包裹清单:

  • sudo apt update

    sudo apt更新

Use apt to install the certbot package:

使用apt安装certbot软件包:

  • sudo apt install certbot

    sudo apt安装certbot

You can test your installation by asking certbot to output its version number:

您可以通过要求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. Open up the appropriate port in your firewall:

Certbot需要回答Let's Encrypt API发出的加密挑战,以证明我们控制了我们的域。 它使用端口80 (HTTP)或443 (HTTPS)来完成此操作。 在防火墙中打开相应的端口:

  • sudo ufw allow 80

    sudo ufw允许80

Substitute 443 above if that’s the port you’re using. ufw will output confirmation that your rule was added:

如果这是您正在使用的端口,请替换上面的443ufw将输出确认已添加您的规则:


   
   
Output
Rule added Rule added (v6)

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 will use the --preferred-challenges http option. For port 443, use --preferred-challenges tls-sni. Finally, we’ll use the -d flag to specify the domain we’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选项以在一个证书中覆盖多个域。

We will use the --preferred-challenges http option to demonstrate, but you should use the option that makes sense for your use case. Run the following command with your preferred options to get your certificate:

我们将使用--preferred-challenges http选项进行演示,但是您应该使用对您的用例有意义的选项。 使用首选选项运行以下命令以获取证书:

  • sudo certbot certonly --standalone --preferred-challenges http -d your_domain

    sudo certbot certonly --standalone --preferred-challenges http -d your_domain

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/your_domain/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/your_domain/privkey.pem Your cert will expire on 2019-08-28. 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 your keys and certificates:

为您的SSL配置应用程序超出了本文的范围,因为每个应用程序都有不同的要求和配置选项,但是让我们来看看Certbot为我们下载了什么。 使用ls列出包含密钥和证书的目录:

  • sudo ls /etc/letsencrypt/live/your_domain

    须藤ls / etc / letsencrypt / live / your_domain

You will see the following output:

您将看到以下输出:


   
   
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 ssl-certificate-key or ssl-certificate-key-file.

    privkey.pem :这是证书的私钥。 这需要保持安全和保密,这就是为什么大多数/etc/letsencrypt目录具有非常严格的权限并且只能由root用户访问的原因。 大多数软件配置将其称为ssl-certificate-keyssl-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 or locations, or with other user permissions. It is best to leave everything in the letsencrypt directory, and not change any permissions there (permissions will just be overwritten upon renewal anyway), but sometimes that’s 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 —处理Certbot自动续订 (Step 4 — Handling Certbot Automatic Renewals)

Let’s Encrypt certificates are only valid for ninety days. This is to encourage users to automate the certificate renewal process. The certbot package we installed takes care of this for us by adding a renew script to /etc/cron.d. This script runs twice a day and will renew any certificate that’s within thirty days of expiring.

让我们加密证书仅有效期为90天。 这是为了鼓励用户自动执行证书续订过程。 我们安装的certbot软件包通过在/etc/cron.d添加更新脚本来为我们解决这一问题。 该脚本每天运行两次,并将在到期后三十天内更新所有证书。

With our certificates renewing automatically, we still need a way to run other 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 need to 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 your favorite editor:

要添加renew_hook ,我们需要更新Certbot的续订配置文件。 Certbot会记住您如何首次获取证书的所有详细信息,并且在续订时将使用相同的选项运行。 我们只需要添加我们的钩子即可。 使用您喜欢的编辑器打开配置文件:

  • sudo nano /etc/letsencrypt/renewal/your_domain.conf

    须藤nano / etc / letsencrypt / renewal / your_domain .conf

A text file will open with some configuration options. Add your hook on the last line. In this case, we’re using an example that would reload a rabbitmq service:

将打开一个文本文件,其中包含一些配置选项。 将钩子添加到最后一行。 在这种情况下,我们使用的示例将重新加载rabbitmq服务:

/etc/letsencrypt/renewal/your_domain.conf
/etc/letsencrypt/renewal/your_domain.conf
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. On Debian, you’ll usually use systemctl to reload a service.

将上面的命令更新为您需要运行以重新加载服务器或运行自定义文件munging脚本的任何内容。 在Debian上,通常会使用systemctl重新加载服务。

Save and close the file, then run a Certbot dry run to make sure the syntax is ok:

保存并关闭文件,然后运行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的文档

翻译自: https://www.digitalocean.com/community/tutorials/how-to-use-certbot-standalone-mode-to-retrieve-let-s-encrypt-ssl-certificates-on-debian-10

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值