SSL 证书部署

acme

acme.sh 实现了 acme 协议, 可以从 letsencrypt 生成免费的证书.

官方安装

github

国内用户

中国大陆用户请参考

1.开始安装

进入到一个安装目录

通常 /root

1.安装依赖

安装 socat

使用 Standalone 模式:如果您在使用 acme.shstandalone 模式(即 acme.sh 自带的临时服务器),socat 将被用来监听端口 80 并处理 HTTP-01 挑战请求。

在Ubuntu/Debian
1
2
sudo apt-get update
sudo apt-get install socat
在 CentOS/RHEL 系统上
1
sudo yum install socat

2.安装acme

1
2
3
git clone https://gitee.com/neilpang/acme.sh.git
cd acme.sh
./acme.sh --install -m my@example.com

2.生成证书

前提条件

  1. 域名已经解析到服务器

  2. 可通过http://example.com 访问到80 端口

    nginx 配置如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    server {
    listen 80; # 监听端口 80
    server_name todo.wyy.icu; # 定义网站域名

    # 网站根目录
    root /www/wwwroot/todo.wyy.icu;
    index index.html index.htm index.php;

    # 日志文件路径
    access_log /www/wwwlogs/todo.wyy.icu.log;
    error_log /www/wwwlogs/todo.wyy.icu.error.log;

    # 处理 PHP 文件
    location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/tmp/php-cgi-74.sock; # 使用 PHP 7.4 版本
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include pathinfo.conf;
    }

    # 处理静态文件
    location / {
    try_files $uri $uri/ =404;
    }

    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
    allow all;
    }


    # 禁止访问隐藏文件(如 .htaccess)
    location ~ /\. {
    deny all;
    }

    # 禁止访问 Git 和 SVN 目录
    location ~* \.(git|svn|htaccess|htpasswd)$ {
    deny all;
    }
    }
    1. 重启 nginx

      检测 nginx 配置文件是否正确

      1
      nginx -t

      重启

      1
      systemctl restart nginx

      检测

      1
      systemctl status nginx

    准备就绪 开始生成证书

todo.wyy.icu 网站域名

*.wyy.icu 泛域名

/www/wwwroot/todo.wyy.icu/ 网站运行目录

1
acme.sh --issue -d todo.wyy.icu --webroot /www/wwwroot/todo.wyy.icu/

或者 nginx 自动解析 网站域名 运行目录

1
acme.sh --issue -d todo.wyy.icu --nginx

迁移文件(可选)

SSL证书文件放入指定位置

成功后生成证书文件位置如下:

1
2
3
4
Your cert is in: /root/.acme.sh/todo.wyy.icu/todo.wyy.icu.cer
Your cert key is in: /root/.acme.sh/todo.wyy.icu/todo.wyy.icu.key
The intermediate CA cert is in: /root/.acme.sh/todo.wyy.icu/ca.cer
And the full chain certs is there: /root/.acme.sh/todo.wyy.icu/fullchain.cer

迁移证书文件到自定义文件夹 /www/server/panel/vhost/cert/todo.wyy.icu/

1
2
3
4
5
sudo mkdir -p /www/server/panel/vhost/cert/todo.wyy.icu/
sudo cp /root/.acme.sh/todo.wyy.icu/todo.wyy.icu.cer /www/server/panel/vhost/cert/todo.wyy.icu/
sudo cp /root/.acme.sh/todo.wyy.icu/todo.wyy.icu.key /www/server/panel/vhost/cert/todo.wyy.icu/
sudo cp /root/.acme.sh/todo.wyy.icu/ca.cer /www/server/panel/vhost/cert/todo.wyy.icu/
sudo cp /root/.acme.sh/todo.wyy.icu/fullchain.cer /www/server/panel/vhost/cert/todo.wyy.icu/

3.NGINX 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
server {
listen 443; # 监听端口 443
server_name todo.wyy.icu; # 定义网站域名

# ssl证书地址
# ssl_certificate /usr/local/nginx/cert/ssl.pem; # pem文件的路径
# ssl_certificate_key /usr/local/nginx/cert/ssl.key; # key文件的路径

# SSL-START SSL相关配置
# error_page 404/404.html;
# HTTP_TO_HTTPS_START
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}
# HTTP_TO_HTTPS_END
ssl_certificate /www/server/panel/vhost/cert/todo.wyy.icu/fullchain.cer;
ssl_certificate_key /www/server/panel/vhost/cert/todo.wyy.icu/todo.wyy.icu.key;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000";
error_page 497 https://$host$request_uri;


# 网站根目录
root /www/wwwroot/todo.wyy.icu;
index index.html index.htm index.php;

# 日志文件路径
access_log /www/wwwlogs/todo.wyy.icu.log;
error_log /www/wwwlogs/todo.wyy.icu.error.log;

# 处理 PHP 文件
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/php-cgi-74.sock; # 使用 PHP 7.4 版本
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include pathinfo.conf;
}

# 处理静态文件
location / {
try_files $uri $uri/ =404;
}

#一键申请SSL证书验证目录相关设置
location ~ \.well-known{
allow all;
}


# 禁止访问隐藏文件(如 .htaccess)
location ~ /\. {
deny all;
}

# 禁止访问 Git 和 SVN 目录
location ~* \.(git|svn|htaccess|htpasswd)$ {
deny all;
}
}
server {
listen 80;
server_name todo.wyy.icu;
return 301 https://$server_name$request_uri;
}

再次执行 重启nginx 步骤即可