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

ops/2024/10/18 16:55:10/

最近,我在自己的服务器上部署了一个网站,并决定使用 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.pemYour key file has been saved at:/etc/letsencrypt/live/example.com/privkey.pemYour certificate will expire on 2023-07-10. To obtain a new ortweaked version of this certificate in the future, simply runcertbot again with the "certonly" option. To non-interactivelyrenew *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/donateDonating 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!


http://www.ppmy.cn/ops/16300.html

相关文章

vscode 创建代码模版

在vscode中快捷创建代码模版 1.在VSCode中,按下Ctrl Shift P(Windows/Linux)或Cmd Shift P(Mac)打开命令面板。 2.然后输入"Preferences: Configure User Snippets"并选择该选项。打开一个json文件用户…

WPF之RadioButton单选框和checkbox多选框

RadioButton 单选框: 实现分组的单选框, checkbox 多选框: 表示用户可以选择和清除的控件。 常用属性 GroupName 获取或设置指定哪些 RadioButton 控件互相排斥的名称Content内容IsChecked是否选中 常用事件 checked 选中的事件unchecked 未选中的事件 RadioBu…

最新免费 ChatGPT、GPTs、AI换脸(Suno-AI音乐生成大模型)

🔥博客主页:只恨天高 ❤️感谢大家点赞👍收藏⭐评论✍️ ChatGPT3.5、GPT4.0、GPTs、AI绘画相信对大家应该不感到陌生吧?简单来说,GPT-4技术比之前的GPT-3.5相对来说更加智能,会根据用户的要求生成多种内容…

ios CI/CD 持续集成 组件化专题一 iOS 将图片打包成bundle

一、 创建 选择 macos 下的Bundle 二 、取名点击下一步 三、Base SDK 选择ios 四 、Build Active Architecture Only 五、Installation后面的内容删除 六、Skip Install 选择NO 七、Strip Debug Symbols During Copy 中"Release"项设置为 "YES" 八、COM…

HarmonyOS开发:【基于命令行(开发环境)】

准备开发环境 在嵌入式开发中,很多开发者习惯于使用Windows进行代码的编辑,比如使用Windows的Visual Studio Code进行OpenHarmony代码的开发。但当前阶段,大部分的开发板源码还不支持在Windows环境下进行编译,如Hi3861、Hi3516系…

java读取word里面的表格数据

Apache POI库读取Word文档中的表格数据 示例代码如下: import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFT…

ChatGPT API - 笔记

1 新手如何用Python玩转ChatGPT API,详细讲解 新手如何用Python玩转ChatGPT API,详细讲解-Django社区,Django中文网,django教程,Django! import time import requestsOPENAI_API_KEY = "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" proxy = "127.…

【PHP】sign加签方法示例

1.需求如下 设所有发送或者接收到的数据为集合M(注:集合中需要加上timestamp),将集合M内参数值的参数技照参数名ASCII码从小到大排序(字典序),使用URL键值对的格式 (key1vaNe1 & KeV2WaN2.)拼接成字符串stmgA在得到stingA后接stingA signkev times…