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

news/2024/9/23 10:23:06/

最近,我在自己的服务器上部署了一个网站,并决定使用 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/news/1437805.html

相关文章

Oracle expdp/impdp 及 exp/imp 命令详解

一、基础环境    操作系统:Windows 或 Linux 数据库版本:Oracle Database 11.2.0.1.0 及以上版本 二、命令简介    我们在使用Oracle 数据库的过程中会经常对数据进行导入导出。Oracle 数据库提供 expdp / impdp (Data Pump&#xff0c…

Oceanbase体验之(一)运维管理工具OCP部署(社区版4.2.2)

资源规划建议 ocp主机1台 内存:64G CPU1:2C及以上 硬盘大于500G observer服务器3台 内存32G CPU:4C以上 硬盘大于1T 建议存储硬盘与操作系统硬盘隔开实现IO隔离 一、OBD、OCP安装包准备 [rootobserver /]# chown -R admin:admin /software/ [rootobserver /]# …

基于emp的mysql查询

SQL命令 结构化查询语句:Structured Query Language 结构化查询语言是高级的非过程化变成语言,允许用户在高层数据结构上工作。是一种特殊目的的变成语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数…

231 基于matlab的北斗信号数据解析

基于matlab的北斗信号数据解析,多通道和单通道接收到的北斗信号数据,利用接收到的北斗数据(.dat .txt文件),进行解析,得到初始伪距,平滑伪距,载波相位,并计算其标准差&am…

jenkins自动化工具简介

Jenkins 是一个开源的自动化服务器,它允许您自动化各种任务,包括构建、测试和部署软件。它是一个用Java编写的应用程序,可以运行在任何支持Java的平台上。Jenkins 通过其插件系统提供了大量的功能,使其成为一个非常灵活和强大的工…

java POI解析Excel大文件,获取表头

目录 前言依赖代码StreamingReader的openWorkbookFactory的createCSV解析首行 前言 poi解析大文件可能出现oom,通样大小文件,xlsx会oom,xls不会,所以使用流式的方式改造解析xlsx文件的代码。 我的需求是提取每一页的表头&#xf…

C++ :设计模式实现

文章目录 原则单一职责原则开闭原则依赖倒置原则接口隔离原则里氏替换原则 设计模式单例模式观察者模式策略模式代理模式 原则 单一职责原则 定义: 即一个类只负责一项职责 问题: 类 T 负责两个不同的职责:职责 P1,职责 P2。当…

3.7设计模式——Observer 观察者模式(行为型)

意图 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于他的对象都得到通知并被自动更新。 结构 Subject(目标)知道它的观察者,可以有任意多个观察者观察同一个目标,提供注册和删…