手把手教你在Nginx上使用CertBot

e10101發表於2016-06-25

前言

自己做了一個iOS App,需要訪問自己的網站獲取資料,但是系統預設只能直接訪問https的網站。不想讓應用改用http的服務。因此,研究如何啟用https,本文即是介紹如何在CentOS上配合Nginx使用CertBot。

環境

  • CentOS(CentOS Linux release 7.2.1511

  • Nginx(nginx version: nginx/1.6.3

  • ExpressJS應用

安裝CertBot

命令列,鍵入:

sudo yum install epel-release
sudo yum install certbot

配置Nginx

這裡我不想使用CertBot的standalone模式,這個模式雖然可以配置好伺服器,但是以後Renew的時候,需要讓服務停止一下,再啟動。因此拋棄這個模式,我們使用Webroot配置模式。

因為,CertBot在驗證伺服器域名的時候,會生成一個隨機檔案,然後CertBot的伺服器會通過HTTP訪問你的這個檔案,因此要確保你的Nginx配置好,以便可以訪問到這個檔案。

修改你的伺服器配置,在server模組新增:

location ^~ /.well-known/acme-challenge/ {
   default_type "text/plain";
   root     /usr/share/nginx/html;
}

location = /.well-known/acme-challenge/ {
   return 404;
}

可以看到,上面的root,我們讓他指向了/usr/share/nginx/html,因為我的應用是通過NodeJSExpressJS寫的,如果修改原始碼的話,比較麻煩。因此我就讓檢驗的連結指向了nginx預設的資料夾下。

接著重新載入Nginx配置:

sudo service nginx reload

然後在命令列輸入:

sudo certbot 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 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 renew  >> /var/log/le-renew.log

上面的執行時間為:每週一半夜2點30分執行renew任務。

你可以在命令列執行/usr/bin/certbot renew >> /var/log/le-renew.log看看是否執行正常,如果一切OK,那麼我們的配置到此結束!

參考

相關文章