在 Ubuntu 22.04 上使用 Let‘s Encrypt 配置 Nginx SSL 证书

最近,我在自己的服务器上部署了一个网站,并决定使用 SSL 证书来确保网站的安全性。经过一番研究,我选择了 Let's Encrypt 作为 SSL 证书的提供商,因为它免费、自动化且广受信任。在这篇博客中,我将与大家分享我在 Ubuntu 22.04 上使用 Let's Encrypt 配置 Nginx SSL 证书的过程。

前提条件

在开始之前,请确保满足以下条件:

  • 你有一个域名,并将其解析到你的服务器 IP。
  • 你已经在服务器上安装了 Nginx。
  • 你以 root 用户身份登录,或者可以使用 sudo 运行命令。

第 1 步:安装 Certbot

Certbot 是 Let's Encrypt 的官方客户端,用于自动获取和部署 SSL 证书。在 Ubuntu 22.04 上,可以使用以下命令安装 Certbot 及其 Nginx 插件:

复制代码
sudo apt update
sudo apt install certbot python3-certbot-nginx

第 2 步:配置 Nginx

在获取 SSL 证书之前,我们需要确保 Nginx 已经正确配置。编辑你网站的 Nginx 配置文件(一般在 /etc/nginx/sites-available/ 目录下),确保其中包含以下内容:

复制代码
server {
    listen 80;
    server_name example.com;
    # 其他配置...
}

example.com 替换为你自己的域名。

第 3 步:获取 SSL 证书

现在,我们可以使用 Certbot 来获取 SSL 证书。运行以下命令:

复制代码
sudo certbot --nginx -d example.com

同样,将 example.com 替换为你自己的域名。Certbot 会自动检测 Nginx 配置并为你的域名申请证书。按照提示完成操作,如果一切顺利,你应该会看到类似以下的输出:

复制代码
IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/example.com/privkey.pem
   Your certificate will expire on 2023-07-10. 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

第 4 步:配置自动续期

Let's Encrypt 的证书有效期为 90 天,因此我们需要设置自动续期。运行以下命令编辑 crontab:

复制代码
sudo crontab -e

选择你喜欢的编辑器,然后添加以下内容:

复制代码
0 0 1 * * /usr/bin/certbot renew --quiet

这将在每个月的第一天自动尝试续期证书。

第 5 步:测试自动续期

为了确保自动续期配置正确,我们可以手动触发一次续期:

复制代码
sudo certbot renew --dry-run

如果一切正常,你应该会看到类似以下的输出:

复制代码
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator nginx, Installer nginx
Simulating renewal of an existing certificate for example.com
Performing the following challenges:
http-01 challenge for example.com
Waiting for verification...
Cleaning up challenges

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed with reload of nginx server; fullchain is
/etc/letsencrypt/live/example.com/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

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

结论

通过以上步骤,我成功地在 Ubuntu 22.04 上使用 Let's Encrypt 为 Nginx 配置了 SSL 证书,并设置了自动续期。现在,我的网站已经启用了 HTTPS,为用户提供了更安全的浏览体验。

如果你也想为你的网站启用 HTTPS,不妨按照这个教程尝试一下。如果遇到任何问题,欢迎在评论区留言讨论。

希望这篇博客对你有所帮助。Happy coding!

相关推荐
绿箭柠檬茶1 天前
Ubuntu 服务器配置转发网络访问
服务器·网络·ubuntu
风_峰1 天前
Ubuntu Linux SD卡分区操作
嵌入式硬件·ubuntu·fpga开发
太空的旅行者1 天前
告别双系统——WSL2+UBUNTU在WIN上畅游LINUX
linux·运维·ubuntu
人工智能训练师1 天前
Ubuntu22.04如何安装新版本的Node.js和npm
linux·运维·前端·人工智能·ubuntu·npm·node.js
qq_264220891 天前
Nginx优化与 SSL/TLS配置
运维·nginx
matlab的学徒2 天前
Web与Nginx网站服务(改)
linux·运维·前端·nginx·tomcat
xqlily2 天前
Linux操作系统之Ubuntu
linux·运维·ubuntu
风_峰2 天前
Petalinux相关配置——ZYNQ通过eMMC启动
嵌入式硬件·ubuntu·fpga开发
风_峰2 天前
【ZYNQ开发篇】Petalinux和电脑端的静态ip地址配置
网络·嵌入式硬件·tcp/ip·ubuntu·fpga开发
邂逅星河浪漫2 天前
【Docker+Nginx+Ollama】前后端分离式项目部署(传统打包方式)
java·nginx·docker·部署