如何在 Ubuntu 14.04 上使用 Let's Encrypt 加密 Nginx

介绍

Let's Encrypt是一个新的证书授权(CA),它提供了一个简单的方法来获取和安装免费的TLS/SSL证书,从而在Web服务器上启用加密的HTTPS。它通过提供一个软件客户端Certbot来简化这个过程,它试图自动化大多数(如果不是所有)所需的步骤。目前,获得和安装证书的整个过程在Apache和Nginx Web服务器上都是完全自动化的。

在本教程中,我们将向您展示如何使用Certbot获得免费的SSL证书,并在Ubuntu 14.04 LTS上使用NGINX。

在本教程中,我们将使用默认的 Nginx 配置文件,而不是单独的服务器封锁文件。 我们建议为每个域创建新的 Nginx 服务器封锁文件,因为它有助于避免一些常见的错误,并将默认文件作为故障配置作为预期。

前提条件

在遵循本教程之前,您将需要一些东西。

  • 一个 Ubuntu 14.04 服务器具有非根用户的sudo特权。你可以学习如何设置这样的用户帐户,通过遵循我们的 初始服务器设置为 Ubuntu 14.04 教程
  • Nginx 安装, 如何在 Ubuntu 14.04 LTS 上安装 Nginx
  • 你必须拥有或控制你想要使用证书的注册域名。 如果你还没有注册的域名,你可以注册其中一个域名注册商(例如,Namecheap,GoDaddy等)。
  • DNS **A 记录,指向你的域名到您的域名的公共 IP 地址。所以你可以遵循 这个域名教程详细信息,以便添加它们。

一旦你已经完成了所有前提条件,让我们继续安装Certbot,Let’s Encrypt客户端软件。

步骤1:安装Certbot

使用 Let's Encrypt 获得 SSL 证书的第一个步骤是将certbot软件安装在您的服务器上。Certbot 开发人员维护自己的 Ubuntu 软件存储库与软件的最新版本.因为 Certbot 处于如此积极的开发中,值得使用此存储库来安装比 Ubuntu 提供的更新的 Certbot。

首先,添加存储器:

1sudo add-apt-repository ppa:certbot/certbot

接下来,更新包列表以获取新库的包信息:

1sudo apt-get update

最后,用apt-get安装Certbot:

1sudo apt-get install python-certbot-nginx

certbot Let’s Encrypt 客户端现在已经准备好使用。

第2步:设置 Nginx

Certbot 可以自动为 Nginx 配置 SSL,但它需要能够在您的配置中找到正确的服务器块。它通过寻找与您要求证书的域相匹配的server_name指令来做到这一点。

1sudo nano /etc/nginx/sites-available/default

查找现有的server_name行:

1[label /etc/nginx/sites-available/default]
2server_name localhost;

用您的域名取代localhost:

1[label /etc/nginx/sites-available/default]
2server_name example.com www.example.com;

保存文件并停止编辑器. 检查您的配置编辑的语法:

1sudo nginx -t

如果没有错误,请重新加载 Nginx 来加载新配置:

1sudo service nginx reload

Certbot 现在将能够找到正确的服务器块并更新它,现在我们将更新我们的防火墙以允许 HTTPS 流量。

步骤三:获得SSL证书

Certbot 提供了通过各种插件获得 SSL 证书的多种方式。 Nginx 插件将负责重新配置 Nginx 并在必要时重新加载配置:

1sudo certbot --nginx -d example.com -d www.example.com

这将运行certbot--nginx插件,使用-d来指定我们希望证书有效的名称。

如果这是你第一次运行certbot,你将被要求输入电子邮件地址并同意服务条款. 这样做后,certbot将与 Let’s Encrypt 服务器进行通信,然后运行一个挑战,以验证你控制的域,你正在请求证书。

如果成功,certbot 会问你想如何配置你的 HTTPS 设置:

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

选择你的选择,然后点击ENTER。配置将被更新,Nginx将重新加载以获取新的设置。

 1[secondary_label Output]
 2IMPORTANT NOTES:
 3 - Congratulations! Your certificate and chain have been saved at
 4   /etc/letsencrypt/live/example.com/fullchain.pem. Your cert will
 5   expire on 2017-10-23. To obtain a new or tweaked version of this
 6   certificate in the future, simply run certbot again with the
 7   "certonly" option. To non-interactively renew *all* of your
 8   certificates, run "certbot renew"
 9 - Your account credentials have been saved in your Certbot
10   configuration directory at /etc/letsencrypt. You should make a
11   secure backup of this folder now. This configuration directory will
12   also contain certificates and private keys obtained by Certbot so
13   making regular backups of this folder is ideal.
14 - If you like Certbot, please consider supporting our work by:
15
16   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
17   Donating to EFF:                    https://eff.org/donate-le

您的证书现在已下载,安装和配置。 尝试使用https://重新加载您的网站,并注意您的浏览器的安全指标。 它应该表示该网站是正确保护的,通常用绿色锁定图标。 如果您使用 SSL Labs Server Test测试您的服务器,它将获得 A等级。

步骤 4 – 验证 Certbot 自动更新

Let's Encrypt 的证书仅有效 90 天,这是为了鼓励用户自动化其证书更新过程,我们安装的certbot包为我们负责,每天通过 systemd 计时器运行certbot renew 两次,在非 systemd 发行版中,此功能由放置在 `/etc/cron.d 中的脚本提供。

要测试更新过程,您可以使用certbot进行干跑:

1sudo certbot renew --dry-run

如果你看不到任何错误,你都已经设置了。当需要时,Certbot会更新你的证书并重新加载 Nginx 来接收更改。如果自动更新过程有时失败,Let’s Encrypt 会向你指定的电子邮件发送消息,提醒你你的证书即将到期。

结论

在本教程中,我们安装了 Let’s Encrypt 客户端certbot,下载了我们的域的 SSL 证书,配置了 Nginx 以使用这些证书,并设置了自动证书更新。

Published At
Categories with 技术
comments powered by Disqus