在 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 插件:

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

第 2 步:配置 Nginx

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

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

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

第 3 步:获取 SSL 证书

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

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

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

vbnet 复制代码
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

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

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

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

第 5 步:测试自动续期

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

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

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

diff 复制代码
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!

相关推荐
陈随易8 分钟前
MoonBit助力前端开发,加密&性能两不误,斐波那契测试提高3-4倍
前端·后端·程序员
wfsm9 分钟前
spring事件使用
java·后端·spring
微风粼粼27 分钟前
程序员在线接单
java·jvm·后端·python·eclipse·tomcat·dubbo
rebel1 小时前
若依框架整合 CXF 实现 WebService 改造流程(后端)
java·后端
极客悟道1 小时前
颠覆传统虚拟化:在Docker容器中运行Windows系统的开源黑科技
前端·后端
调试人生的显微镜2 小时前
WebView 中 Cookie 丢失怎么办?跨域状态不同步的调试与修复经验
后端
weixin_437398212 小时前
转Go学习笔记(2)进阶
服务器·笔记·后端·学习·架构·golang
极客悟道2 小时前
巧解 Docker 镜像拉取难题:无需梯子和服务器,拉取数量无限制
后端·github
aiopencode2 小时前
iOS 出海 App 安全加固指南:无源码环境下的 IPA 加固与防破解方法
后端
liangdabiao2 小时前
AI一人公司?先搞定聚合支付!一天搞定全能的聚合支付系统
后端