使用 ACME 生成自签发证书

·580 Views·

acme 说明文档:https://github.com/acmesh-official/acme.sh/wiki/说明

官方说的很详细。

这里就以本站域名为例,说下配置过程。当前域名 DNS 托管到 CloudFlare ,所以变量是 CF 开头。如果是 Godaday 是不一样的,注意区分。

1# Godaday
2# export GD_Key="key"
3# export GD_Secret="secret"
4# --dns dns_gd
5
6# CloudFlare
7export CF_KEY="key"
8export CF_Email="email"
9# --dns dns_cf
10
11# Aliyun
12# First you need to login to your Aliyun account to get your RAM API key. <https://ram.console.aliyun.com/users,并授予权限>
13# export Ali_Key="key"
14# export Ali_Secret="email"
15# --dns dns_ali


# 生成证书

1~/.acme.sh/acme.sh --issue -d xingbaifang.com -d *.xingbaifang.com --dns dns_cf


# 安装证书

1~/.acme.sh/acme.sh --installcert -d xingbaifang.com -d *.xingbaifang.com \\
2 --key-file /etc/nginx/ssl/xingbaifang.com.key \\
3 --fullchain-file /etc/nginx/ssl/xingbaifang.com.fullchain.cer


生成证书后,需要记录证书路径,然后修改 Nginx 配置。

一般 Nginx 安装目录在 /etc/nginx 中,先修改 nginx.conf ,添加 SSL 共有配置,这样每一个域名都避免重复添加,域名中,只配置证书路径。

1# nginx.conf
2# 在 http 节点中配置 SSL 共有路径
3ssl common config
4ssl_session_timeout 1d;
5ssl_session_cache shared:SSL:50m;
6ssl_session_tickets off;
7ssl_protocols TLSv1.1 TLSv1.2;
8ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4:!DH:!DHE';
9ssl_prefer_server_ciphers on;
10add_header Strict-Transport-Securit max-age=15768000;
11ssl_stapling on;
12ssl_stapling_verify on;


然后在具体域名配置文件中,配置证书路径,比如我的域名配置文件在 /etc/nginx/conf.d/ 目录中

1# xingbaifang.com.conf
2server {
3 listen 80;
4 server_name xingbaifang.com www.xingbaifang.com;
5 return 301 https://$host$request_uri;
6}
7server {
8 listen 12443 ssl http2;
9 server_name xingbaifang.com www.xingbaifang.com;
10 ssl_certificate /etc/nginx/ssl/xingbaifang.com.fullchain.cer;
11 ssl_certificate_key /etc/nginx/ssl/xingbaifang.com.key;
12 location / {
13 proxy_set_header Host $host;
14 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
15 proxy_redirect off;
16 proxy_buffering off;
17 proxy_pass <http://127.0.0.1:3000>;
18 }
19 client_max_body_size 50m;
20}


然后重启 nginx

1nginx -s reload