一、前言
隨著全民互聯網安全意識的增強,由于HTTPS具有比HTTP更高網絡安全等級,HTTPS
開始迅速普及
。但是HTTPS的證書每年的認證費
需要近千元
,除非是由于不可抗拒的原因,必須升級到HTTPS,否則一般選擇繼續使用HTTP。高額的認證費阻礙HTTPS快速推廣應用,于是let's encrypt一個開源的HTTPS證書授權應運而生,給大家提供了優質的免費的自動化
認證服務。
我在配置let's encrypt的路上遇到了不少的困難,走了很多的彎路,浪費了不少時間。所以將其總結下來進行分享。根據我分享的經驗進行多部署幾次的話,平均獲取一個HTTPS證書只需要10分鐘左右。
使用的官網推薦自動部署工具certbot,支持自動部署的軟件,系統環境分別如下:
支持的軟件
apache
nginx
haproxy
plesk
等等(詳情請查看certbot官網)
支持的系統
Web Hosting Service
Debian
Ubuntu
Gentoo
Arch Linux
Fedora 26+
CentOS
RHEL
FreeBSD
OpenBSD
macOS
Devuan
等等(詳情請查看certbot官網)
官網鏈接
下面是我在
CENTOS6
、NGINX
上部署
Certbot的經驗分享。如果系統和軟件不同,也可以作為一個解決問題的思路,以供參考。
二、環境
Centos6
- Centos6 已經安裝了
OpenSSL library
-
Nginx
已集成ssl模塊
- 你已經有了一點遠程操作
Linux
的入門知識和實操經驗
三、步驟
注意
:重要,請閱讀完(三、步驟)和(四、難點)后再進行操作
步驟一: ssh以root用戶登陸centos6服務器
// 打開終端輸入
# ssh root@服務器IP
步驟二: 分別執行如下命令
// 進入root目錄(可定義)
# cd ~
// 下載certbot腳本
# wget https://dl.eff.org/certbot-auto
// 給certbot添加可執行權限
# chmod a+x certbot-auto
// 運行certbot腳本
# sudo ~/certbot-auto --nginx
步驟三: 運行certbot后,正常的情況一路會出現如下的選項
是否需要安裝python34?輸入`Y`回車
輸入一個郵箱用于接受證書到期更新通知:填寫自己可用的郵箱,例如:`xxx@163.com`
是否同意lets encrypt的協議?輸入`A`回車
是否接收他們的新聞,通過這種方式支持他們?輸入`Y`回車
輸入要支持HTTPS的域名,空格分開:填寫已指向并可用的域名,
多個域名用`半角空格`分開,例如:`www.bbwhm.com kunshan.bbwhm.com`
是否HTTP自動跳轉到HTTPS?選擇`n`的場合,該網站同時支持`http`和`https`,
選擇`Y`的場合,該網站只支持`https`網站,所有`http`請求自動跳轉為`https`,
我的選擇是`Y`回車
步驟四:結果確認
生成成功,建議備份`/etc/letsencrypt`,直接訪問`https://你的域名`,
查看結果,網站前多了一個鎖,表示成功。
四、難點
難點1:python鏡像配置錯誤
描述
:一臺阿里云服務器,centos6
系統已經默認安裝了python2.6
,是否需要自動安裝python34?就算選擇自動安裝了python34,運行仍然失敗,為什么?
解答
:certbot需要安裝python34
,并自動安裝,只要選擇Yes即可。運行仍然失敗的原因是,阿里云服務器的系統,有個配置文件默認指向阿里云鏡像導致的,只要修為清華鏡像即可。
vi ~/.pip/pip.config
修改前
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
修改后
[global]
index-url=https://pypi.tuna.tsinghua.edu.cn/simple
難點2:nginx的配置路徑不滿足certbot要求
描述
:nginx不是通過yum install nginx安裝,是自己編譯安裝的,有些配置不滿足certbot的要求
解答
:certbot要求必須存在如下的文件和目錄
/usr/bin/nginx
/etc/nginx
操作方法,假設你的nginx位于/usr/local/nginx,你需要執行下面的兩個命令,創建快捷方式軟連接過去即可。
# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
# ln -s /usr/local/nginx/conf /etc/nginx
難點3:nginx缺少ssl模塊
描述
:提示nginx沒有添加ssl模塊
解答
:可以通過nginx -V,查看是否有ssl模塊。由于nginx是自己從官網下載的源碼手動編譯的,當時沒有添加ssl參數。只需要nginx重新編譯添加ssl模塊即可。
// 從nginx官網下載和已安裝的同版本的源文件(我的是1.12.2)
# cd ~
// 下載
# wget http://nginx.org/download/nginx-1.12.2.tar.gz
// 解壓
# tar -xzvf nginx-1.12.2.tar.gz
// 進入
# cd nginx-1.12.2
// 配置(請根據實際情況、添加或減少模塊。--with-http_ssl_module 必須)
# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
// 編譯
# make
// 備份之前的文件
# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
// 覆蓋替換
# cp objs/nginx /usr/local/nginx/sbin/nginx
// 關閉nginx服務
# /usr/local/nginx/sbin/nginx -s stop
// 啟動nginx服務
# /usr/local/nginx/sbin/nginx
// 確認結果(是否包含ssl)
# /usr/local/nginx/sbin/nginx -V
難點4:443端口未開放
描述
:certbot提示已經成功部署,但是通過https//域名,仍然無法訪問
解答
:由于使用了阿里云服務器,開放端口的控制需要在阿里云控制臺里面進行設置,啟用443端口即可。
難點5:域名未綁定
描述
:certbot配置https域名的時候提示域名指向檢測失敗,例如:kunshan.bbwhm.com 沒有找到
解答
:在域名服務商管理后臺中,將 kunshan.bbwhm.com 指向這臺服務器的IP,然后在Centos6中,重新運行sudo ~/certbot-auto --nginx 這樣就直接進入最后一步了,跳過了前面很多的步驟。(最好提前就將所有的域名都指向好,測試沒有問題的時候,再繼續操作)
難點6:續期
描述
:證書有效期只有3個月,到期了怎么辦?
解答
:方案一、可以在到期前1個月,手動執行如下命令即可刷新證書,再次獲取3個月的有效期;
# sudo ~/certbot-auto renew --dry-run
方案二、也可以通過定時任務,命令如下:
0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && ~/certbot-auto renew
或者
0 0 * * * /root/certbot-auto renew
最近cerbot-auto升級到了letsencrypt-auto.sh,當前我的服務器定時任務配置為
0 0 * * * /root/letsencrypt-auto.sh renew
附錄
certbot在centos6的上配置整個順利的配置流程輸出如下,僅供參考。大體流程如下
[root@CENTOS6 ~]# cd ~
[root@CENTOS6 ~]# wget https://dl.eff.org/certbot-auto
[root@CENTOS6 ~]# sudo ./certbot-auto --nginx
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Which names would you like to activate HTTPS for?
----------------------------
1: www.bbwhm.com
2: kunshan.bbwhm.com
----------------------------
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): www.bbwhm.com kunshan.bbwhm.com
----------------------------
requested (ref: /etc/letsencrypt/renewal/www.bbwhm.com.conf)
It contains these names: www.bbwhm.com, kunshan.bbwhm.com
You requested these names for the new certificate: www.bbwhm.com,
kunshan.bbwhm.com
Do you want to expand and replace this existing certificate with the new
certificate?
----------------------------
(E)xpand/(C)ancel: E
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for www.bbwhm.com
http-01 challenge for kunshan.bbwhm.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/vhost/www.bbwhm.com.conf
Deploying Certificate to VirtualHost /etc/nginx/vhost/kunshan.bbwhm.com.conf
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
----------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
----------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
----------------------------
Your existing certificate has been successfully renewed, and the new certificate
has been installed.
The new certificate covers the following domains: https://www.bbwhm.com,
https://kunshan.bbwhm.com
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=www.bbwhm.com
https://www.ssllabs.com/ssltest/analyze.html?d=kunshan.bbwhm.com
----------------------------
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/www.bbwhm.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/www.bbwhm.com/privkey.pem
Your cert will expire on 2018-08-27. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again with the "certonly" option. To non-interactively renew *all*
of your certificates, run "certbot-auto 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
[root@CENTOS6 ~]#
若本文幫您節省了時間和金錢,請不吝贊賞。若本文有不足之處,請不吝賜教。謝謝。