簡單記錄一下我整個設定的過程!
- 進到 SSL For Free → 點 Create Free SSL Certificate → 註冊 or 登入
- 點左側 Dashboard → 下面有個 Create SSL Certificate,點右邊的 New Certificate
- 在 Enter Domains 的地方填你 EC2 的 Elastic IP Address → Next Step
- 因為是免費方案,Validity 選 90-Day Certificate → Next Step
- 雖然不知道那是什麼,但感覺開啟 Auto-Generate CSR 即可 → Next Step
- 此時會看到 Free 被 Selected → Next Step
- Verification Method for [Elastic IP Address]選 HTTP File Upload → 有些步驟要跟著指示做
- Download Auth File → 連進你的 EC2 → 在專案 /public底下建立/.well-known/pki-validation/資料夾,指令:cd到/public以後,輸入mkdir -p .well-known/pki-validation
- cd .well-known/pki-validation/進到裡面之後,準備把剛剛 Download Auth File 載下來的東西傳到裡面
- 在本地端開新的終端機介面 → 輸入 scp -i [/path/to/EC2/XXX].pem [剛載下來的].txt ec2-user@[Elastic IP Address]:~即可將那個檔案傳進 EC2 裡面
- 回到 EC2,cd ~,用ls一看,還真的有欸 → 移動到剛剛新建的資料夾裡面吧mv [剛傳進來的].txt Campus-Program01/students/[path/to]/public/.well-known/pki-validation/
- 在 Express 主程式,可能叫做 app.js 的當中記得加上app.use(express.static(path.join(__dirname, 'public')));,
 前面或許還需要var path = require('path');,這樣才能讓對方讀到那個 txt 檔
- 回到 SSL Certificate 申請頁面,點 Make sure your file is available under the following link 旁邊的連結,新視窗有文字檔裡面的東西代表到目前為止一切順利 → Next Step
- Verify Domain → 會顯示 Congratulations, your domains have been verified. This means that our system is issuing your certificate at the moment. This page will refresh automatically every few seconds. → Your certificate has been issued and is ready for installation. To continue, please follow the steps below.
- 選你的 Server Type,通常不是 Apache 就是 NGINX,我是 NGINX → Download Certificate (.zip) → Next Step
- Follow the steps below to install your certificate → 照著指示做吧!接下來我以 NGINX 為例,Apache 的設定或許也是照著指示做就能順利完成
- Unzip 剛剛載下來的 zip → 裡面的東西都傳到 EC2scp -i [/path/to/EC2/XXX].pem ca_bundle.crt certificate.crt private.key ec2-user@[Elastic IP Address]:~
- 回到 EC2 → 進到你剛剛傳東西進去的目錄底下 → cat certificate.crt ca_bundle.crt >> certificate.crt→ 跳出cat: certificate.crt: input file is output file不用理它
- sudo mv certificate.crt private.key /etc/ssl/
- 輸入 history | grep nginx看你當初是去哪設定 nginx 的,用 vim 打開來編輯吧!(我的是sudo vim /etc/nginx/nginx.conf)
- 下面的東西加進去!(加在哪要稍微注意一下,我 port 是 3000 所以 proxy_pass http://localhost:3000;)
| 1 | server { | 
- sudo service nginx restart重啟 web server
- 回到最一開始 ZeroSSL 頁面 → Check Certificate → 完成!
- 想再確認是否真的成功?http://[Elastic IP Address]/你的路徑和https://[Elastic IP Address]/你的路徑都試試看吧!如果後者有個鎖頭在上面,就代表設定成功了!
 (如果後者進到的是錯誤頁面,可以看一下/var/log/nginx/nginx.vhost.error.log裡面寫什麼,再來一一 debug)