如何在 Ubuntu 22.04/23.04 上使用 Let’s Encrypt 保护 Nginx

Let’s Encrypt 是一个免费、自动化和开放的证书颁发机构 (CA),它提供数字 SSL/TLS 证书以启用网站的安全加密连接。Let’s Encrypt 的目标是让每个人都能广泛使用加密技术,并帮助创建一个更安全、更尊重隐私的网络。

Let’s Encrypt 证书的有效期为 90 天,可以轻松续订。证书颁发和续订过程是完全自动化的,使网站所有者可以轻松地使用 HTTPS 保护他们的网站。

除了免费和易于使用之外,Let’s Encrypt 还是透明和协作的,拥有广泛的利益相关者社区,他们支持其使命并为其发展做出贡献。

步骤 1:更新操作系统

更新您的 Ubuntu 22.04 操作系统以确保所有现有软件包都是最新的:

$ sudo apt update && sudo apt upgrade -y

第二步:安装 Nginx 网络服务器

您可以apt 通过执行以下命令通过包管理器安装 Nginx。

$ sudo apt install nginx

Nginx使用以下命令验证服务的状态:

$ sudo systemctl status nginx

输出:

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running)
       Docs: man:nginx(8)
    Process: 30128 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 30129 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 30218 (nginx)
      Tasks: 2 (limit: 2196)
     Memory: 10.1M
        CPU: 77ms
     CGroup: /system.slice/nginx.service
             ├─30218 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             └─30221 "nginx: worker process"

第 3 步:在 Ubuntu 22.04 上安装 Certbot

现在我们安装用于创建 Let’s Encrypt 证书的 Certbot 客户端:

$ sudo apt install certbot python3-certbot-nginx

验证 Certbot 是否已安装并正常工作:

$ certbot --version

您应该会看到刚刚安装的 Certbot 的版本号:

certbot 1.21.0

现在您可以使用 Certbot 获取 SSL 证书并配置您的 Web 服务器以使用它们。

第 4 步:配置 Nginx Web 服务器

导航到 /etc/nginx/sites-available 目录并运行以下命令为您的安装创建配置文件:

$ sudo nano /etc/nginx/sites-available/your-domain.com.conf

将以下代码添加到文件中:

server {
        listen 80;

        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;

        server_name your-domain.com www.your-domain.com;

        location / {
                try_files $uri $uri/ =404;
        }

        error_log /var/log/nginx/your-domain.com.error;
        access_log /var/log/nginx/your-domain.com.access;

}

请记住替换 your-domain.com 为您的服务器的域名。

保存并退出配置文件。

启用新的配置文件。

$ sudo ln -s /etc/nginx/sites-available/your-domain.com.conf /etc/nginx/sites-enabled/your-domain.com.conf

检查 Nginx 语法:

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

要实施更改,请重新启动 Nginx 网络服务器:

$ sudo systemctl restart nginx

第 5 步:获取 Let’s Encrypt SSL 证书

要使用 Certbot 获取 SSL 证书,请键入以下命令:

$ sudo certbot --nginx

系统会要求您提供有效的电子邮件地址并接受服务条款:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): [email protected]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

接下来,系统会询问您是否要与电子前沿基金会共享您的电子邮件以接收新闻和其他信息。如果您不想订阅他们的内容,请写N

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N

接下来,系统会要求您选择要安装 Let’s Encrypt SSL 的域:

Account registered.

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: your-domain.com
2: www.your-domain.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):

如果成功获取SSL证书,certbot会提示配置成功:

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/your-domain.com.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/your-domain.com/privkey.pem
   Your cert will expire on 2023-04-22. 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

现在,您已经成功地在您的网站上安装了 SSL。

您现在可以使用 打开您的网站 https://,您会注意到一个绿色的锁图标。

第 6 步:验证 Certbot 自动续订

验证 Certbot 的自动续订是否正常工作是确保您的网站保持安全的重要步骤。您可以通过执行以下步骤来验证 Certbot 的自动续订:

检查 Certbot 日志

Certbot 日志是验证自动续订的最佳起点。日志将包含有关任何续订尝试和可能发生的任何错误的信息。您可以通过运行以下命令来访问 Certbot 日志:

$ sudo cat /var/log/letsencrypt/letsencrypt.log

测试更新过程

您可以通过手动运行 Certbot 续订命令来测试续订过程。为此,请运行以下命令:

$ sudo certbot renew --dry-run

这将模拟更新尝试,并会为您提供有关结果的信息。如果续订成功,您应该会看到一条消息,表明证书已成功续订。

检查证书到期日期

最后,您可以检查证书的到期日期以确保它已更新。您可以通过访问您的网站并检查浏览器安全设置中的证书信息来执行此操作。

如果您在自动续订过程中遇到任何问题,建议您联系 Let’s Encrypt 社区或查阅Certbot 文档以寻求帮助。

第 7 步:撤销 Let’s Encrypt 证书

要撤销 Let’s Encrypt SSL 证书,您可以使用命令certbot revoke

首先,停止您的 Nginx Web 服务器:

$ sudo systemctl stop nginx

运行 certbot revoke 命令,指定要吊销的证书:

$ sudo certbot revoke --cert-path /etc/letsencrypt/live/your-domain.com/fullchain.pem

注意:您需要替换/etc/letsencrypt/live/your-domain.com/fullchain.pem为证书文件的实际路径。

再次启动您的网络服务器:

$ sudo systemctl start nginx

吊销证书后,证书将不再受浏览器信任,也不再用于加密您的网站流量。

例如,如果您需要将域转让给其他所有者,或者您怀疑您的私钥已被泄露,这将很有用。

评论和结论

就是这样。今天,您学习了如何在 Ubuntu 22.04 上使用 Let’s Encrypt 保护 Nginx。

如果您有任何问题或反馈,请随时发表评论。

类似文章

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注