title: "使用 Let’s Encrypt 搭建免费的 HTTPS" date: 2017-07-14T11:15:00+08:00 tags: ["经验分享"] draft: false
$ cd ~
$ git clone https://github.com/lukas2511/dehydrated
$ sudo mkdir -p /etc/dehydrated
$ sudo mkdir -p /var/www/dehydrated
$ sudo chown `whoami` -R /var/www/dehydrated
$ sudo chown `whoami` -R /etc/dehydrated
$ cp ~/dehydrated/docs/examples/config /etc/dehydrated/config
$ cp ~/dehydrated/docs/examples/domains.txt /etc/dehydrated/domains.txt
<!--more-->
$ vim /etc/dehydrated/config
# 添加如下代码
BASEDIR="/etc/dehydrated/"
WELLKNOWN="/var/www/dehydrated/"
修改域名
$ vim /etc/dehydrated/domains.txt
# 输入
blog.forecho.com www.forecho.com
server {
listen 80;
....
location /.well-known/acme-challenge {
allow all;
alias /var/www/dehydrated/;
}
...
}
重启 nginx
$ service nginx reload
$ ~/dehydrated/dehydrated --register --accept-terms
$ ~/dehydrated/dehydrated -c
$ sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
$ sudo vim /etc/nginx/sites-enabled/blog.forecho.com.conf
添加 ssl 相关信息
server {
listen 80;
listen 443 ssl;
## listen 443 ssl http2;
listen [::]:443 ssl;
## ssl
ssl on;
ssl_certificate /etc/dehydrated/certs/blog.forecho.com/fullchain.pem;
ssl_certificate_key /etc/dehydrated/certs/blog.forecho.com/privkey.pem;
## ssl pem
ssl_dhparam /etc/ssl/certs/dhparam.pem;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;## omit SSLv3 because of POODLE (CVE-2014-3566)
ssl_stapling on;
ssl_ciphers "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA";
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
}
$ sudo nginx -t
$ sudo service nginx reload
$ mv ~/dehydrated /etc/dehydrated/
$ vim /etc/dehydrated/auto-renew.sh
# 输入
/etc/dehydrated/dehydrated/dehydrated -c
sudo service nginx reload
把脚本改为可执行
$ chmod 777 /etc/dehydrated/auto-renew.sh
把默认的 nano 改成 vim,如果你喜欢 nano 的话跳过这一步。
$ vim ~/.selected_editor
# 输入
SELECTED_EDITOR="/usr/bin/vim.tiny"
$ mkdir -p /etc/dehydrated/log
$ crontab -e
# 添加以下代码
1 0 1 * * /etc/dehydrated/auto-renew.sh >> /etc/dehydrated/log/lets-encrypt.log 2>&1
重启 cron 的服务
$ sudo service cron restart
搞定!
$ vim /etc/dehydrated/domains.txt
location /.well-known/acme-challenge {
allow all;
alias /var/www/dehydrated/;
}
$ sudo service nginx reload
$ sudo /etc/dehydrated/dehydrated/dehydrated -c
$ sudo service nginx reload
参考链接: Let’s Encrypt免费的https证书