生成 HTTPS 证书并配置到 Nginx 的完整步骤

步骤 1: 安装 acme.sh

如果你还没有安装 acme.sh,可以通过以下命令进行安装:

curl https://get.acme.sh | sh

步骤 2: 生成 HTTPS 证书

使用 acme.sh 生成 forum.selectious.fun 的证书。你可以使用 standalone 模式,这意味着 acme.sh 会在生成证书时临时启动一个 Web 服务器来完成域名验证。

acme.sh --issue -d forum.selectious.fun --standalone --httpport 80

步骤 3: 安装证书

安装证书并指定证书和密钥的存放路径。你可以将证书安装到 Nginx 的证书目录(例如 /etc/nginx/cert/)。

acme.sh --install-cert -d forum.selectious.fun \
--key-file /etc/nginx/cert/forum.selectious.fun.key \
--fullchain-file /etc/nginx/cert/forum.selectious.fun.cer \
--reloadcmd "systemctl reload nginx"

步骤 4: 配置 Nginx

编辑 Nginx 配置文件(例如 /etc/nginx/sites-available/forum.selectious.fun.conf),以包含 SSL 配置。

以下是一个示例配置文件:

server {
    listen 80;
    server_name forum.selectious.fun;

    # Redirect HTTP to HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name forum.selectious.fun;

    ssl_certificate /etc/nginx/cert/forum.selectious.fun.cer;
    ssl_certificate_key /etc/nginx/cert/forum.selectious.fun.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;

    location / {
        proxy_pass http://localhost:XXXX;  # 替换为你的应用运行的端口
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

请根据你的实际情况调整路径和端口。

步骤 5: 测试和重载 Nginx

在修改完 Nginx 配置文件后,测试配置以确保没有语法错误:

sudo nginx -t

如果测试通过,重载 Nginx 以应用更改:

sudo systemctl reload nginx

步骤 6: 确保证书自动续订

acme.sh 通常会自动设置证书续订任务。你可以手动测试自动续订功能:

acme.sh --renew -d forum.selectious.fun --force

确保你的 acme.sh 配置中包含自动重载 Nginx 的命令,这样每次证书续订后,Nginx 都会自动重载。

完整的步骤回顾

  1. 安装 acme.sh

    curl https://get.acme.sh | sh
    
  2. 生成 HTTPS 证书:

    acme.sh --issue -d forum.selectious.fun --standalone --httpport 80
    
  3. 安装证书:

    acme.sh --install-cert -d forum.selectious.fun \
    --key-file /etc/nginx/cert/forum.selectious.fun.key \
    --fullchain-file /etc/nginx/cert/forum.selectious.fun.cer \
    --reloadcmd "systemctl reload nginx"
    
  4. 配置 Nginx:
    编辑 /etc/nginx/sites-available/forum.selectious.fun.conf,添加如下内容:

    server {
        listen 80;
        server_name forum.selectious.fun;
    
        # Redirect HTTP to HTTPS
        return 301 https://$host$request_uri;
    }
    
    server {
        listen 443 ssl;
        server_name forum.selectious.fun;
    
        ssl_certificate /etc/nginx/cert/forum.selectious.fun.cer;
        ssl_certificate_key /etc/nginx/cert/forum.selectious.fun.key;
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
    
        location / {
            proxy_pass http://localhost:XXXX;  # 替换为你的应用运行的端口
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
    
  5. 测试和重载 Nginx:

    sudo nginx -t
    sudo systemctl reload nginx
    
  6. 手动测试自动续订功能:

    acme.sh --renew -d forum.selectious.fun --force
    

通过以上步骤,你应该能够成功为 forum.selectious.fun 启用 HTTPS,并保证证书的自动续订和 Nginx 的自动重载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值