安裝 Let’s Encrypt 工具

1
2
3
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot

設定 nginx config

1
2
3
location /.well-known {
alias /var/www/example.org/.well-known;
}

建立會用到的目錄

1
sudo mkdir -p /var/www/example.org/.well-known

產生憑證

1
sudo certbot certonly --webroot -w /var/www/example.org/ -d example.org

結果

1
2
3
4
5
6
7
8
9
10
11
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/example.org/fullchain.pem. Your cert will
expire on 2017-08-30. To obtain a new or tweaked version of this
certificate in the future, simply run certbot again. To
non-interactively renew *all* of your certificates, run "certbot
renew"
- If you like Certbot, please consider supporting our work by:

Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

nginx with ssl and HTTP/2 範例

/etc/nginx/snippets/ssl-params.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# from https://cipherli.st/
# and https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_ecdh_curve secp384r1;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# disable HSTS header for now
#add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
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
# HTTP - redirect all requests to HTTPS:
server {
listen 80;
listen [::]:80;
return 301 https://$server_name$request_uri;
}

# HTTPS - proxy requests on to local Node.js app:
server {
# 啟用 ssl 與 http2
listen 443 ssl http2;

# 同時啟用 IPv6 的 ssl 與 http2
listen [::]:443 ssl http2;

server_name your_domain_name;

ssl_certificate /etc/letsencrypt/live/your_domain_name/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your_domain_name/privkey.pem;

include snippets/ssl-params.conf;

# Pass requests for / to localhost:8080:
location / {
proxy_pass http://localhost:8080/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}

編輯 crontab

1
sudo crontab -e

新增

1
45 8 * * * /usr/bin/certbot renew --renew-hook "/bin/systemctl reload nginx" >> /var/log/certbot-renew.log

參考連結

refs.