在宝塔面板中,为自己的云服务器安装SSL证书,为所搭建的网站启用https(主要部分攻略)

前提条件

My HTTP website is running Nginx on Debian 10(或者11)

时间:2024-3-28 16:25:52

你的网站部署在Debain 10(或者11)的 Nginx上

安装单域名证书(默认)(非泛域名)

1 进入自己的云服务器的宝塔面板(即云服务器控制台的外网连接)

2 进入终端标签页

安装snap

进入这个网址

https://snapcraft.io/docs/installing-snapd

选择自己的服务器环境

以 Debian 为例

在终端运行这些指令

snap can be installed directly from the command line:

sudo apt update
sudo apt install snapd

Either log out and back in again, or restart your system, to ensure snap’s paths are updated correctly.

After this, install the core snap in order to get the latest snapd.

sudo snap install core

To test your system, install the hello-world snap and make sure it runs correctly:

sudo snap install hello-world
hello-world

如果曾经安装过Certbot packages

则需要先卸载移除这些内容

例如

sudo apt-get remove certbot
sudo dnf remove certbot
sudo yum remove certbot

Install Certbot

sudo snap install --classic certbot

Prepare the Certbot command

Execute the following instruction on the command line on the machine to ensure that the certbot command can be run.

sudo ln -s /snap/bin/certbot /usr/bin/certbot

Run this command to get a certificate and have Certbot edit your nginx configuration automatically to serve it, turning on HTTPS access in a single step.(小白推荐)

sudo certbot --nginx

注意,如果你的nginx不在默认目录/etc/nginx/nginx.conf

则需要修改这个命令,修改后如下

以/www/server/nginx/conf/nginx.conf为例

sudo certbot --nginx-server-root /www/server/nginx/conf

启用自动续订SSL证书

Test automatic renewal

The Certbot packages on your system come with a cron job or systemd timer that will renew your certificates automatically before they expire. You will not need to run Certbot again, unless you change your configuration. You can test automatic renewal for your certificates by running this command:

sudo certbot renew --dry-run

The command to renew certbot is installed in one of the following locations:

  • /etc/crontab/
  • /etc/cron.*/*
  • systemctl list-timers

3 Confirm that Certbot worked,重新进入自己的网站,看看是否变成了https协议

        To confirm that your site is set up properly, visit https://yourwebsite.com/ in your browser and look for the lock icon in the URL bar.

        完成

4 去宝塔网站标签页(从这里开始建议不必理会,可以先去看看其他大神)

5 选择自己的网站项目

这里以go项目为例

6 粘贴自己的证书内容(内容哪里找?看第7步)

7 去宝塔文件系统的这个目录/etc/letsencrypt/live/www.xxxxx.com

分别对应privkey.pem 和 fullchain.pem

附录

指定nginx目录,且指定域名

指定nginx目录,且指定域名,示例

sudo certbot --nginx --nginx-server-root /www/server/nginx/conf -d www.xxxxx.com

sudo certbot --nginx --nginx-server-root /www/server/nginx/conf -d www.xxxxx.com

强制续订所有证书

sudo certbot renew --force-renewal

        续订完成之后回到步骤6 在宝塔面板更新一下证书即可

查看所有证书

sudo certbot certificates

        这里可以查看所有的证书都放在什么位置,都还有多少时间过期。

        比方说在这个路径:/etc/letsencrypt/live

删除不必要的证书

sudo rm /etc/letsencrypt/renewal/xxxxxxxx.conf

        其实就是直接删除一个配置文件

关于阿里云上传SSL证书

        上传SSL证书到阿里云时,要求同时上传证书文件、证书私钥和证书链,这是为了确保HTTPS连接的安全性和完整性。

文件对应关系

  1. 证书文件(Certificate):对应 cert.pem
  2. 证书私钥(Private Key):对应 privkey.pem
  3. 证书链(Certificate Chain):对应 chain.pem 或 fullchain.pem

详细解释

  1. 证书文件(cert.pem)

    • 内容:包含服务器的公钥和服务器的身份信息。直接使用 cert.pem 可能会破坏许多服务器配置,建议在阅读相关文档后再使用。
    • 作用:用于加密数据和验证服务器的身份。
  2. 证书私钥(privkey.pem)

    • 内容:包含与证书文件对应的私钥。
    • 作用:用于解密客户端发送的加密数据和签名数据,确保数据的完整性和来源的真实性。用于解密通过 SSL/TLS 加密的通信数据。私钥必须保密,不能泄露。
  3. 证书链(chain.pem 或 fullchain.pem)

    • 内容:包含中间证书和根证书,用于建立完整的证书链。
    • 作用:客户端在验证服务器证书时,需要通过证书链验证服务器证书的合法性。证书链确保客户端可以信任服务器证书。chain.pem用于 OCSP stapling(在线证书状态协议),特别是在 Nginx 1.3.7 及以上版本中使用。fullchain.pem是大多数服务器软件使用的证书文件,确保客户端可以验证服务器证书的完整链。

为什么需要这些文件

  1. 证书文件:客户端需要服务器的公钥来加密数据,并验证服务器的身份。
  2. 证书私钥:服务器需要私钥来解密客户端发送的加密数据,并签名数据以确保数据的完整性和来源的真实性。
  3. 证书链:客户端需要通过证书链验证服务器证书的合法性,确保服务器证书是由受信任的证书颁发机构(CA)签发的。

服务器配置示例

以下是如何在不同的服务器软件中使用这些文件的示例:

Nginx 配置示例

在 Nginx 中使用 fullchain.pem 和 privkey.pem

server {

    listen 443 ssl;

    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;

    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # 如果需要 OCSP stapling

    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    ssl_stapling on;

    ssl_stapling_verify on;

    # 其他配置...

}

Apache 配置示例

在 Apache 中使用 fullchain.pem 和 privkey.pem

<VirtualHost *:443>

    ServerName example.com

    SSLEngine on

    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem

    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

    # 其他配置...

</VirtualHost>

阿里云 DNS API 自动续签 Let's Encrypt SSL 证书(Certbot)

        使用 阿里云 RAM 访问控制 创建 API 访问凭据,并通过 certbot 自动获取和续签 Let's Encrypt SSL 证书,确保 HTTPS 站点长期安全运行。


第一部分:创建阿里云 API 访问凭据

为了让 Certbot 使用阿里云 DNS API,我们需要创建一个具有 AliyunDNSFullAccess 权限的用户。

1. 登录阿里云 RAM 访问控制

2. 创建 RAM 用户

  • 点击「创建用户」
  • 填写 用户名(例如 certbot
  • 选择 "OpenAPI 调用"(编程访问)
  • 点击 「确定」

3. 绑定 AliyunDNSFullAccess 权限

  • 选中刚刚创建的用户(certbot
  • 进入 权限管理 → 点击 「添加权限」
  • 搜索 AliyunDNSFullAccess 并勾选
  • 点击 「确定」

4. 获取 API 访问密钥

  • 在用户详情页面,进入 安全设置
  • 创建 AccessKey
  • 记下 AccessKey IDAccessKey Secret

    ⚠️ 注意: 只会显示一次,请务必保存!


第二部分:服务器安装 Certbot 和插件

1. 安装 Certbot

Ubuntu/Debian
sudo apt update && sudo apt install certbot python3-certbot-dns-aliyun -y
CentOS
sudo yum install epel-release -y
sudo yum install certbot python3-certbot-dns-aliyun -y
使用 Snap
sudo snap install certbot --classic
sudo snap refresh certbot

2. 检查 Certbot 版本

certbot --version

确保版本大于 1.10.0,否则更新 Certbot:

sudo apt update && sudo apt install certbot -y

第三部分:配置阿里云 API 访问凭据

1. 创建 API 认证文件

mkdir -p ~/.secrets/certbot
nano ~/.secrets/certbot/aliyun.ini

填写以下内容:

dns_aliyun_access_key = YOUR_ACCESS_KEY_ID
dns_aliyun_secret_key = YOUR_ACCESS_KEY_SECRET
dns_aliyun_access_key_id = YOUR_ACCESS_KEY_ID
dns_aliyun_access_key_secret = YOUR_ACCESS_KEY_SECRET

替换 YOUR_ACCESS_KEY_IDYOUR_ACCESS_KEY_SECRET 为你自己的值

2. 设置文件权限(重要!)

chmod 600 ~/.secrets/certbot/aliyun.ini

第四部分:获取 SSL 证书

使用 certbot 申请证书,示例中包含 XXX.XXX.comwww.XXX.com

sudo certbot certonly \
  --authenticator dns-aliyun \
  --dns-aliyun-credentials ~/.secrets/certbot/aliyun.ini \
  --dns-aliyun-propagation-seconds 60 \
  --force-renewal \
  -d XXX.XXX.com -d www.XXX.com

执行成功后,你会看到:

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/www.XXX.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/XXX.XXX.com/privkey.pem


第六部分:自动续签 SSL 证书

Let's Encrypt 证书有效期 90 天,所以我们需要配置 自动续签任务

1. 手动测试续签

sudo certbot renew --dry-run

如果没有错误,说明自动续签可用。

2. 配置定时任务(cron

打开 crontab

sudo crontab -e

添加:

0 3 * * * certbot renew --quiet --dns-aliyun --dns-aliyun-credentials ~/.secrets/certbot/aliyun.ini && systemctl reload nginx

每天凌晨 3 点 自动续签证书并重启 Nginx


### 使用宝塔面板服务器搭建网站教程 #### 准备工作 为了顺利使用宝塔面板搭建网站,需提前准备好必要的资源和工具: - **服务器**:推荐选用可靠的云服务提供商,例如纵横数据云服务器[^1]。 - **域名**:通过合法渠道购买并注册域名,建议选择阿里云等知名平台。 - **远程连接工具**:如Xshell可用于安全地连接Linux服务器;此款软件对个人用户免费开放。 #### 宝塔面板安装与配置 完成上述准备工作之后,按照如下指南操作来设置宝塔面板环境: ##### 下载并安装宝塔面板 访问官方文档获取最新版本的安装命令,在终端执行相应指令即可快速部署宝塔面板到目标机器上。通常情况下,只需一条简单的bash脚本就能实现自动化安装过程[^2]。 ```bash wget -O install.sh http://download.bt.cn/install/install_6.0.sh && bash install.sh ede360a5afaad9f4c8b7ccbf5aaaebd8 ``` ##### 登录管理界面 成功安装后,浏览器输入`http://<您的IP>:8888`进入图形化控制台,默认用户名为root,密码将在首次启动时显示于屏幕中央位置,请妥善保存以便后续登录验证所需。 #### 构建Web应用实例——WordPress博客站点 以创建一个基于PHP框架的内容管理系统为例说明具体步骤: ##### 创建数据库 导航至左侧菜单栏中的“数据库”,点击新增MySQL/MariaDB实例按钮,填写名称、字符集等相关参数提交申请等待初始化完毕。 ##### 配置虚拟主机 转到“网站”选项卡下新建站点条目,指定绑定域名以及根目录路径,勾选启用SSL加密传输协议(可选),最后确认保存更改生效。 ##### 上线前端页面 借助FTP客户端将事先打包好的主题模版文件夹上传至对应webapps子目录内,确保结构清晰有序利于后期维护升级。 ##### 应用程序对接 返回首页选取已就绪的应用商店模块,搜索框键入wordpress关键字定位插件包名,一键安装完成后依照向导提示逐步完善各项设定直至正式上线运行。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CCSBRIDGE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值