如何在 Ubuntu 16.04/18.04 上使用 Nginx 安装 Let’s Encrypt

介绍

为网站启用 SSL 对于数据保护、用户信任以及 SEO 排名都至关重要。Let’s Encrypt 提供了一个免费、自动化且开放的证书颁发机构(CA),它能让您轻松安装和配置 SSL 证书。

在本指南中,我们将一步步讲解如何在 Ubuntu 16.04/18.04 上通过 Nginx 设置 Let’s Encrypt SSL。

前提条件

在开始之前,请确保您已具备以下条件:

  • 一台运行 Ubuntu 16.04 或 Ubuntu 18.04 的服务器
  • 一个已注册并解析到服务器公网 IP 的域名
  • 已安装并运行的 Nginx
  • 拥有服务器的 sudo 或 root 权限

在 Ubuntu 16.04/18.04 上安装 Let’s Encrypt 的步骤(Nginx)

步骤 1:更新系统

通过 SSH 以 root 用户身份登录 Ubuntu 16.04/18.04 服务器

ssh root@IP_Address -p Port_Number

确保系统已完全更新。

apt-get update && apt-get upgrade

步骤 2:安装 Certbot 客户端

Certbot 是一个免费的开源 ACME 客户端,可自动从 Let’s Encrypt 获取、安装并续订 SSL/TLS 证书,使您能够轻松启用服务器的 HTTPS。

运行以下命令以添加 Certbot 软件源:

sudo apt install software-properties-common
sudo add-apt-repository ppa:certbot/certbot

确认安装后,更新软件包列表并安装适用于 Nginx 的 Certbot:

apt-get update
apt-get install python-certbot-nginx

完成此步骤后,Certbot 已安装完毕,您已准备好获取免费的 Let’s Encrypt SSL 证书。

步骤 3:获取 Let’s Encrypt 证书

Let’s Encrypt 证书可以通过不同的 Certbot 插件进行安装。在本教程中,我们将使用 Nginx 插件,它会自动更新配置并为您重新加载 Nginx。

运行以下命令,将 domain.com 替换为您的真实域名:

certbot --apache -d domain.com -d www.domain.com

首次生成证书时,系统会要求您输入电子邮件地址并同意 Certbot 的服务条款。

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):

如果您希望所有网站访问者都自动跳转至 HTTPS(推荐选项),请选择数字 2 并按回车键。

如果 Let’s Encrypt SSL 证书安装成功,您将看到如下输出:

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/domain.com/fullchain.pem. Your cert will
expire on 2017-10-23. 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"
- 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

步骤 4:验证 SSL 安装

此时,您应该已经成功在 domain.com 域名上安装并配置了 Let’s Encrypt SSL 证书。您可以通过访问以下链接来验证是否生效:
https://domain.com

Let’s Encrypt SSL 证书的有效期为 90 天,我们可以通过创建计划任务(cron job)来自动续订。Let’s Encrypt 建议自动续订任务每天运行两次。执行以下命令编辑 crontab:

crontab -e

然后添加以下行:

* */12 * * * /usr/bin/certbot renew >/dev/null 2>&1

这样系统将每 12 小时自动检测并续订证书,确保您的网站始终保持安全的 HTTPS 访问。