如何在 Ubuntu 22.04 LTS 上使用 Let’s Encrypt 安装 Nginx

[ad_1]

在本教程中,我们将向您展示如何在 Ubuntu 22.04 LTS 上安装 MySQL。 对于那些不知道的人,Nginx 是一个开源、免费的 HTTP 服务器软件。 除了 HTTP 服务器功能,Nginx 还可以充当代理服务器。 而 Let’s Encrypt 是一个为网站提供免费 SSL 证书的证书颁发机构。 Let’s Encrypt 支持自动安装证书 Apache、Nginx、Plex 等等。

本文假设您至少具备 Linux 的基本知识,知道如何使用 shell,最重要的是,您将网站托管在自己的 VPS 上。 安装非常简单,假设您在 root 帐户下运行,如果不是,您可能需要添加 ‘sudo‘ 到命令以获取 root 权限。 我将向您展示在 Ubuntu 22.04 (Jammy Jellyfish) 上使用 Nginx 加密 SSL 的分步安装。 对于 Ubuntu 22.04 和任何其他基于 Debian 的发行版,如 Linux Mint、Elementary OS、Pop!_OS 等,您可以按照相同的说明进行操作。

先决条件

  • 运行以下操作系统之一的服务器:Ubuntu 22.04、20.04 和任何其他基于 Debian 的发行版,如 Linux Mint。
  • 建议您使用全新的操作系统安装来防止任何潜在问题。
  • 对服务器的 SSH 访问(或者如果您在桌面上,则只需打开终端)。
  • 一种 non-root sudo user或访问 root user. 我们建议充当 non-root sudo user,但是,如果您在充当 root 时不小心,可能会损害您的系统。

在 Ubuntu 22.04 LTS Jammy Jellyfish 上使用 Let’s Encrypt 安装 Nginx

步骤 1. 首先,通过运行以下命令确保所有系统包都是最新的 apt 终端中的命令。

sudo apt update
sudo apt upgrade

步骤 2. 在 Ubuntu 22.04 上安装 Nginx。

默认情况下,Nginx 在 Ubuntu 22.04 基础存储库中可用。 现在运行以下命令将最新版本的 Nginx 安装到您的 Ubuntu 系统:

sudo apt install nginx

安装成功后,启用 Nginx(系统启动时自动启动),启动,并使用以下命令验证状态:

sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx

确认安装并检查已安装的 Nginx 构建版本:

nginx -v

步骤 3. 配置防火墙。

Ubuntu 22.04 有 ufw 默认运行的防火墙。 通过端口启用连接 80 HTTP 和 443 HTTPS:

sudo ufw allow 'Nginx FULL'
sudo ufw enable
sudo ufw status

步骤 4. 访问 Nginx Web 服务器。

成功安装后,打开系统上的 Web 浏览器并在地址栏中输入服务器的 IP。 您将获得默认的 Nginx 服务器页面:

步骤 5. 安装 Certbot。

现在我们安装 证书机器人 在 Ubuntu 22.04 上使用以下命令:

sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

步骤 6. 在 Ubuntu 22.04 上使用 Let’s Encrypt 保护 Nginx。

完成安装 Certbot 后,现在我们使用以下命令为 Nginx 设置 Certbot:

sudo apt install python3-certbot-nginx

接下来,运行以下命令开始创建证书:

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d www.domain.com

输出:

-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://domain.com and https://www.domain.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=domain.com
https://www.ssllabs.com/ssltest/analyze.html?d=www.domain.com
-------------------------------------------------------------------------------

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

步骤 7. 自动续订 SSL。

Let’s Encrypt 证书的有效期为 90 天,强烈建议在证书到期前更新证书。 要测试此更新过程是否正常工作,您可以运行:

sudo certbot renew --dry-run

输出:

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/domain.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Account registered.
Simulating renewal of an existing certificate for domain.com and www.domain.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded: 
  /etc/letsencrypt/live/domain.com/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

步骤 8. 测试 SSL。

使用 Let’s Encrypt SSL 成功完成安全 Nginx 后,现在转到 ssllabs.com/ssltest/,并在您的域上运行 SSL 测试:

在 Ubuntu 22.04 LTS 上使用 Let's Encrypt 安装 Nginx

恭喜! 您已使用 Let’s Encrypt 成功保护了 Nginx。 感谢您使用本教程在 Ubuntu 22.04 LTS Jammy Jellyfish 系统上使用 Let’s Encrypt TLS/SSL 安装 Nginx。 如需更多帮助或有用信息,我们建议您查看 Nginx 官方网站.

[ad_2]

Related Posts