search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[Linux] 安裝 Let's Encrypt 所發行的 SSL 憑證,將網址加密變成 https

此篇文章瀏覽量: 696

Let’s Encrypt 官網的兩個較重要連結:

Getting Started

How Let’s Encrypt Works

一、進入到 Certbot 網站 按照指引安裝憑證並設定(以選擇 Apache、CentOS/RHEL 7 為例):

一、安裝 epel-release、python-certbot-apache:

$ sudo yum install epel-release
$ sudo yum install python-certbot-apache

二、作業系統還要安裝mod_ssl (若是安裝 Apache 的話),因為 Apache 若要支援 https ,就要需 mod_ssl 模組,同時也安裝openssl

$ yum install mod_ssl openssl

然後可再自己的本機端輸入以下指令,看 443 port 是否有通:(出現以下表示有通)

$ telnet 111.111.111.111(server的ip) 443

Trying 128.199.216.14...
Connected to 128.199.216.14.
Escape character is '^]'.

三、建立 virtualhost-443.conf

$ touch /etc/httpd/conf.d/virtualhost-443.conf

該檔的內容為(假設網址為 xxx.com):


  ServerName xxx.com
  DocumentRoot /var/www/html/xxx
  SSLEngine on
  SSLCertificateFile    /etc/letsencrypt/live/xxx.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/xxx.com/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/xxx.com/fullchain.pem
  
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
  

其中 SSLCertificateFile、SSLCertificateKeyFile、SSLCertificateChainFile 這三個所指定的檔案會在 以下指令之後產生:

$ certbot –apache

四、將原來的 80 port 轉到 443 port

產生好憑證之後,因為目前一般會設定 80 和 443 兩個 port ,所以要將80轉到443,
也就是若一般使用者輸入 http://xxx.com 會轉址到 https://xxx.com

$ vi /etc/httpd/conf.d/virtualhost.conf


  ServerName xxx.carlos-studio.com
  DocumentRoot /var/www/html/xxx
  Redirect permanent / https://xxx.com/
  
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
  

以上重啟 apache 後即完成。

五、憑證的 Automating renewal

因為 Let’s Encrypt 憑證預設只有 3 個月,所以需要設定自動 renew,輸入以下指令測試是否可以正確執行(此指令並不會真的renew,只是測試):

$ certbot renew –dry-run     (註記:dry 前面是兩個橫線)

以下這個指令才會真的renew,若確定執行成功,就可以設定在 cronjob 裡了,每天跑以下指令(官方建議每天跑兩次):

$ certbot renew –quiet         (註記:quiet 前面是兩個橫線,若有問題,可以 –quiet 改成用 –force-renew,force 前面是兩個橫線 )

六、如果要刪除某個憑證,不需要再用 https 了

先找到某個domain的相關設定檔及資料夾,例如 “sabinahuang.carlos-studio.com”

$ find /etc/letsencrypt/ -name “sabinahuang.carlos-studio

就會找出以下:

/etc/letsencrypt/renewal/sabinahuang.carlos-studio.com.conf
/etc/letsencrypt/live/sabinahuang.carlos-studio.com
/etc/letsencrypt/archive/sabinahuang.carlos-studio.com

全數刪除即可。

二、進入到 Certbot 網站 按照指引安裝憑證並設定(以選擇 Nginx、CentOS/RHEL 7 為例):

依序執行以下指令:

$ sudo yum install epel-release
$ sudo yum install python2-certbot-nginx

再執行以下指令,安裝憑證,並自動更新 nginx 的設定檔。

$ sudo certbot –nginx

安裝好後,會直接修改 nginx 的設定檔,正常的話,應該就可以看到 https 是可連線的狀態了。

可再執行以下語法,來檢查 nginx 的相關語法是否有錯誤:

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

因為憑證只有 90 天的效期,所以到期前,需要再依序執行以下指令來展延:

$ sudo certbot renew --dry-run
$ certbot renew
$ certbot renet --force-renew // 如果上面那個指令失效,可以改用這個試試

letsencrypt 的 log 檔:/var/log/letsencrypt/letsencrypt.log

測試成功的 cron renew 方式

先使用以下指令,觀看 nginx 的所在路徑:

$ which nginx
/usr/sbin/nginx
$ sudo which nginx
/sbin/nginx

然後檢查一下環境變數當中,是否有包含 /usr/sbin 及 /sbin 這兩個路徑,如果都沒有的話,就修改 ~/.bashrc 檔,加上最後一行,如下(假設是沒有 /sbin 路徑):

export PATH=”$PATH:/sbin”

然後再下指令 echo $PATH 檢查,將所輸出的完整路徑,放至排程檔,

crontab -e 編輯排程檔,如下:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/root/bin
10 0 * * * certbot renew –dry-run
30 0,12 * * * certbot renew –quiet

就可以自動 renew 憑證了。

其它參考資源(Other Reference)

想更瞭解加密原理嗎?請參考這篇文章( What Is Encryption And How Does It Work? )

若覺得文章有幫助,請多分享,讓其他同好也能吸收網站技能知識。



熱門推薦

本文由 carlos-studiocom 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦