生成 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 的自动重载。

相关推荐

  1. 生成 HTTPS 证书配置 Nginx 完整步骤

    2024-07-18 22:36:02       22 阅读
  2. nignx配置https证书

    2024-07-18 22:36:02       39 阅读
  3. Nginx配置ssl证书(https)

    2024-07-18 22:36:02       22 阅读
  4. nginx配置ssl支持https详细步骤

    2024-07-18 22:36:02       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 22:36:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 22:36:02       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 22:36:02       58 阅读
  4. Python语言-面向对象

    2024-07-18 22:36:02       69 阅读

热门阅读

  1. tensorflow1基础函数学习

    2024-07-18 22:36:02       21 阅读
  2. cookies,sessionStorage和localStorage都有什么区别

    2024-07-18 22:36:02       18 阅读
  3. 力扣 541反转链表2

    2024-07-18 22:36:02       22 阅读
  4. Vue3+TypeScript项目目录结构及文件作用

    2024-07-18 22:36:02       24 阅读
  5. Scala之OOP讲解

    2024-07-18 22:36:02       19 阅读
  6. 自动驾驶目标检测常见Metrics

    2024-07-18 22:36:02       22 阅读