how to install SSL certificate for a domain on your server

how to install SSL certificate for a domain on your server

前幾天申請了新的domain,大概更新後3個小時就可以就可以指到你指定的IP

今天來講如何設置SSL certificate,目的是為瀏覽器或電腦和伺服器或網路之間建立加密連結,讓一般人不容易擷取到你的重要資訊

之前介紹過一次,事隔一年多,各有優缺點,實作上差異不大,大家都可以參考看看

這次是直接使用Gandi購買域名時送的一年SSL certificate

Step 1.  登入gandi後,選SSL憑證,選頁面上方的購買,你就會看到:

這裡我是租用digital ocean的主機,所以選somewhere else

Step 2. 下一步後,一定要選”Standard”跟”Single address”,這樣才能免費獲得

Step 3.  到你的主機上輸入:

openssl req -nodes -newkey rsa:2048 -sha256 -keyout myserver.key -out server.csr

之後會詢問你一些問題,不想回答就按enter跳過就好

這是官方給的範例:

Country Name (2 letter code) [AU]: US
State or Province Name (full name) [Some-State]: Minnesota
Locality Name (eg, city) []: Moorhead
Organization Name (eg, company) [Internet Widgits Pty Ltd]: MyCompany Inc.
Organizational Unit Name (eg, section) []: IT
Common Name (eg, YOUR name) []: subdomain.example.com
Email Address []:
A challenge password []:
An optional company name []:

記得”Common Name”一定要填你的申請的域名(yourdomain.com),才能獲得一年免費的SSL certificate

然後

cat server.csr

複製連同begin跟end兩行的所有內容

-----BEGIN CERTIFICATE REQUEST-----
...text...
-----END CERTIFICATE REQUEST-----

Step 4. 在CSR框框中貼上你剛剛複製的內容,並確定Common name顯示的是yourdomain.com

Step 5. 之後你就會在你的購物車看到,價格為0的SSL certificate

之後會直接跳過Payment的步驟完成交易,接下來只要稍等一下確認信就行了,大概3分鐘內會收到

Step 6. 重新回到SSL certificate的頁面會看到我們要的domain狀態仍是Pending

點進去後會發現它需要認證,證明你真的擁有這個domain這個ip

有三種方式”Validation by DNS”, “Validation by email”, “Validation by file”

我個人是推薦第三種”Validation by file”,因為它更新的速度最快

Step 7. 如果你選”Validation by file”(記得要按submit),它會要你複製一段文字(或你也可以直接下載file),放到他指定的檔案位置,如果它能訪問到相同的檔案跟文字,你的認證就通過了,放好後就是時間的等待(這邊要注意你要讓那個檔案是可讀取的-rw-rw-r–)

大概30分鐘就能收到gandi的SSL certificate啟用信

Step 8.  登入Gandi,選SSL certificate,選擇你要設SSL certificate的domain,下載.crt憑證檔和.pem中繼憑證

Step 9. 用scp將.crt跟.pem傳到你的server上,windows系統可以用WinSCP

scp /path/file_you_want_to_send [email protected]:/path_you_want_to_put_your_file

Step 10. 把.pem中繼憑證內容放在.crt後面

cat GandiStandardSSLCA2.pem >> yourdomain.com.crt

Step 11. 在/etc/nginx/底下建一個ssl檔案夾,將一開始產生的myserver.key跟剛剛的.crt放在ssl/底下

Step 12. 編輯你的nginx 設定檔(記得用sudo)

sudo vim /etc/nginx/conf.d/default.conf

加上SSL設定

listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl_certificate /etc/nginx/ssl/yourdomain.crt;
ssl_certificate_key /etc/nginx/ssl/myserver.key;

儲存離開

Step 13. 檢查並reload你的nginx設定檔

sudo nginx -t
sudo service nginx reload

接著打開你的瀏覽器,試試”https://yourdomain.com”,如果成功載入網頁並出現鎖頭就代表完成了!

 

 


未分類

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *