SSL一直是公司架設網站的安全重點,可是頂級的單位發憑證超貴,又害怕隨便找的機構發出的憑證瀏覽器不支援,花了大錢卻還是一直跑出警告頁面嚇壞使用者,原本一些無涉用戶登入及金流的網站並沒有升HTTPS的急迫性,但是Google公開表態支援HTTPS的網站可得到較好的搜尋排名,又吹皺了一池春水。
本篇文章要教大家設定open source的免費服務 – Let’s Encrypt,它是許多大廠發起的計畫,2016年3月剛發出第一百萬個憑證,大家以後不用再為了SSL憑證煩惱啦。
操作步驟
- 直接把code clone到home目錄
cd ~ ; sudo git clone https://github.com/letsencrypt/letsencrypt
- 建立symlink並且進入 /opt/letsencrypt 目錄
sudo ln -s ~/letsencrypt /opt/letsencrypt && cd /opt/letsencrypt
- Let’s Encrypt提供了一些好用的CLI,但是基本上 letsencrypt-auto 就夠用了
以下的例子都是用yourdomain.com,讀者可以換成自己的網域名稱,一定要符合FQDN的規範,不然就會跳出錯誤訊息,詳見let’s Encrypt官方論壇
–webroot-path參數要帶入document root,我的例子是/usr/share/nginx/html/sudo ./letsencrypt-auto certonly -a webroot --webroot-path=/usr/share/nginx/html/ -d yourdomain.com -d www.yourdomain.com
若機器是AWS EC2則必須加上–debug參數
sudo ./letsencrypt-auto certonly -a webroot --debug --webroot-path=/usr/share/nginx/html/ -d yourdomain.com -d www.yourdomain.com
- 一切順利的話會先安裝pyhton,然後進入藍底UI,這時候輸入EMAIL以及同意服務條款(Term of Service)就完成申請了
- 設定nginx.conf
申請完成的SSL憑證會放在/etc/letsencrypt/live/yourdomain.com下server { listen 80; server_name yourdomain.com www.yourdomain.com; root /usr/share/nginx/html; listen 443 ssl; ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/yourdomain.com/privkey.pem; }
- 重啟nginx
sudo service nginx restart
- 用瀏覽器連到 https://yourdomain.com 即可
小提醒
- 改用https以後所有網頁裡面的asset,包含圖片、影片、javascripts等等,都要一起改為https為首的URL,避免瀏覽器顯示錯誤訊息
- 如果你使用AWS EC2,記得要去Security Group把Port 443加入Inbound規則,以免怎麼連都連不上囉
- 記得用 /opt/letsencrypt/letsencrypt-auto renew 來更新 SSL憑證,在瀏覽器就可以看到憑證到期日
$ sudo crontab -e # Renew SSL certification per day 30 2 * * 1 /opt/letsencrypt/letsencrypt-auto renew >> /var/log/le-renew.log 35 2 * * 1 /etc/init.d/nginx reload
參考資料
- Let’s Encrypt 官網 | Github | 討論區
- Amazon launches Certificate Manager, offering free SSL/TLS certificates for AWS resources
Facebook Comments