amce-tiny制作免费https证书

下载 acme-tiny

1
2
git clone https://github.com/diafygi/acme-tiny.git
cd acme-tiny

创建账户私钥

创建一个 Let's Encrypt 账户私钥,以便让其识别你的身份

1
openssl genrsa 4096 > account.key

创建证书签名请求 (CSR) 的域名

1
openssl genrsa 4096 > domain.key
1
2
3
4
5
6
7
8
# For a single domain
openssl req -new -sha256 -key domain.key -subj "/CN=yoursite.com" > domain.csr

# For multiple domains (use this one if you want both www.yoursite.com and yoursite.com)
openssl req -new -sha256 -key domain.key -subj "/" -addext "subjectAltName = DNS:yoursite.com, DNS:www.yoursite.com" > domain.csr

# For multiple domains (same as above but works with openssl < 1.1.1)
openssl req -new -sha256 -key domain.key -subj "/" -reqexts SAN -config <(cat /etc/ssl/openssl.cnf <(printf "[SAN]\nsubjectAltName=DNS:yoursite.com,DNS:www.yoursite.com")) > domain.csr

配置验证域名所有权的服务

创建验证目录

1
2
# Make some challenge folder (modify to suit your needs)
mkdir -p /var/www/challenges/

配置一个 HTTP 服务让 LETSENCRYPT 能下载验证文件

1
2
3
4
5
6
7
8
9
10
11
12
# Example for nginx
server {
listen 80;
server_name yoursite.com www.yoursite.com;

location /.well-known/acme-challenge/ {
alias /var/www/challenges/;
try_files $uri =404;
}

...the rest of your config
}

获取签名证书

1
2
# Run the script on your server
python acme_tiny.py --account-key ./account.key --csr ./domain.csr --acme-dir /var/www/challenges/ > ./signed_chain.crt

安装证书

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
server {
listen 443 ssl;
server_name yoursite.com www.yoursite.com;

ssl_certificate /path/to/signed_chain.crt;
ssl_certificate_key /path/to/domain.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA;
ssl_session_cache shared:SSL:50m;
ssl_dhparam /path/to/server.dhparam;
ssl_prefer_server_ciphers on;

...the rest of your config
}

server {
listen 80;
server_name yoursite.com www.yoursite.com;

location /.well-known/acme-challenge/ {
alias /var/www/challenges/;
try_files $uri =404;
}

...the rest of your config
}

证书更新

renew_cert.sh

1
2
3
4
#!/usr/bin/sh
python /path/to/acme_tiny.py --account-key /path/to/account.key --csr /path/to/domain.csr --acme-dir /var/www/challenges/ > /path/to/signed_chain.crt.tmp || exit
mv /path/to/signed_chain.crt.tmp /path/to/signed_chain.crt
service nginx reload
1
2
# Example line in your crontab (runs once per month)
0 0 1 * * /path/to/renew_cert.sh 2>> /var/log/acme_tiny.log

重启nginx

问题

在安装 openssl 证书时提示如下报错:

Can't load /root/.rnd into RNG
140496635077056:error:2406F079:random number generator:RAND_load_file:Cannot open file:../crypto/rand/randfile.c:88:Filename=/root/.rnd

解决办法
cd /root
openssl rand -writerand .rnd