在 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!

相关推荐
吃海鲜的骆驼6 分钟前
服务异步通讯与RabbitMQ
java·分布式·后端·rabbitmq
m0_748233647 分钟前
RabbitMQ 进阶
android·前端·后端
m0_7482386317 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
桦说编程18 分钟前
【硬核总结】如何轻松实现只计算一次、惰性求值?良性竞争条件的广泛使用可能超过你的想象!String实际上是可变的?
后端·函数式编程
菜鸟阿达1 小时前
spring boot 2.7 + seata +微服务 降级失败问题修复
spring boot·后端·微服务
清河__1 小时前
【Go】十七、grpc 服务的具体功能编写
开发语言·后端·golang
张声录12 小时前
国密算法Sm2工具类--golang实现版
开发语言·后端·golang
一小路一10 小时前
从0-1学习Mysql第五章: 索引与优化
数据库·后端·学习·mysql·面试
mah66610 小时前
SpringBoot项目连接Oracle视图报错整理
spring boot·后端·oracle
IT学长编程11 小时前
计算机毕业设计 基于SpringBoot的智慧社区管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·后端·毕业设计·课程设计·论文笔记·1024程序员节