letsencrypt是一個免費ssl提供商。證書有效期3個月,每3個月須要renew證書一次。下面的教程介紹瞭如何安裝配置證書,及如何經過cron自動renew證書.html
命令行,鍵入:nginx
$ sudo yum install epel-releaseweb
$ wget https://dl.eff.org/certbot-auto瀏覽器
$ chmod a+x certbot-auto服務器
$ cp certbot-auto /usr/bin/certbot-autodom
這裏我不想使用CertBot的standalone模式,這個模式雖然能夠配置好服務器,可是之後Renew的時候,須要讓服務中止一下,再啓動。所以拋棄這個模式,咱們使用Webroot配置模式。ide
由於,CertBot在驗證服務器域名的時候,會生成一個隨機文件,而後CertBot的服務器會經過HTTP訪問你的這個文件,所以要確保你的Nginx配置好,以即可以訪問到這個文件。網站
修改你的服務器配置,在server模塊添加:ui
location ^~ /.well-known/acme-challenge/ {this
default_type "text/plain";
root /usr/share/nginx/html;
}
location = /.well-known/acme-challenge/ {
return 404;
}
能夠看到,上面的root,咱們讓他指向了/usr/share/nginx/html,由於個人應用是經過NodeJS的ExpressJS寫的,若是修改源代碼的話,比較麻煩。所以我就讓檢驗的連接指向了nginx默認的文件夾下。
接着從新加載Nginx配置:
sudo service nginx reload
而後在命令行輸入:
sudo certbot-auto certonly --webroot -w /usr/share/nginx/html/ -d your.domain.com
上面記得替換your.domain.com爲你本身的域名。
若是提示:
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/your.domain.com/fullchain.pem. Your cert
will expire on 20XX-09-23. 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
證書生成成功!
啓用443端口
一樣,修改Nginx的虛擬主機配置文件,新建一個443端口的server配置:
server {
listen 443 ssl;
listen [::]:443 ssl ipv6only=on;
ssl_certificate /etc/letsencrypt/live/your.domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your.domain.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/your.domain.com/chain.pem;
// ... other settings ...
}
上面記得替換your.domain.com爲你本身的域名。
接着從新加載Nginx配置:
sudo service nginx reload
如今經過瀏覽器訪問你的網站:https://your.domain.com試試,若是看到瀏覽器的綠色標誌,恭喜你設置成功!
不過因爲這個證書的時效只有90天,咱們須要設置自動更新的功能,幫咱們自動更新證書的時效。
先在命令行模擬證書更新:
sudo certbot-auto renew --dry-run
模擬更新成功的效果以下:
-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/your.domain.com.conf
-------------------------------------------------------------------------------
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates below have not been saved.)
Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/your.domain.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)
既然模擬成功,咱們就使用crontab -e的命令來啓用自動任務,命令行:
sudo crontab -e
添加配置:
30 2 * * 1 /usr/bin/certbot-auto renew --quiet >> /var/log/letsencrypt/renew.log
上面的執行時間爲:每週一半夜2點30分執行renew任務。
你能夠在命令行執行/usr/bin/certbot-auto renew >> /var/log/letsencrypt/renew.log看看是否執行正常,若是一切OK,那麼咱們的配置到此結束!