你還在讓你的網站裸奔在網絡上嗎?在這裏咱們將搭建免費版HTTPS,免費的,免費的,免費的,重要的事情說三遍,申請來源爲letsencrypt,php
超文本傳輸協議HTTP協議被用於在Web瀏覽器和網站服務器之間傳遞信息,HTTP協議以明文方式發送內容,若是攻擊者截取了客戶端與服務器之間的傳輸報文,將能直接獲取到其中信息,css
爲了解決HTTP協議的這一缺陷,須要使用另外一種協議:安全套接字層超文本傳輸協議HTTPS,爲了數據傳輸的安全,html
HTTPS在HTTP的基礎上加入了SSL協議,SSL依靠證書來驗證服務器的身份,併爲客戶端和服務器之間的通訊加密。nginx
這裏使用git下載,尚未安裝git的 查看如何安裝git
切換目錄github
cd /usr/local web
克隆git倉庫shell
git clone https://github.com/certbot/certbot.git api
克隆完成將會在/usr/loca/ 出現certbot目錄瀏覽器
切換到certbot目錄
cd /usr/local/certbot
若是目錄是這樣的說明安裝成功了
certbot經常使用命令 查看官方文檔
查看當前證書,第一次執行將會安裝一些依賴,中途有一次確認詢問,輸入Y便可
./certbot-auto certificates
配置泛域名
./certbot-auto --server https://acme-v02.api.letsencrypt.org/directory -d *.nl166.com --manual --preferred-challenges dns-01 certonly
配置制定域名
./certbot-auto --server https://acme-v02.api.letsencrypt.org/directory -d api.nl166.com --manual --preferred-challenges dns-01 certonly
配置二級泛域名
./certbot-auto --server https://acme-v02.api.letsencrypt.org/directory -d *.api.nl166.com --manual --preferred-challenges dns-01 certonly
如上圖所示,第一次會讓你輸入一個郵箱,按要求輸入就能夠了,到時候會發送一個郵件給你,須要點擊確認郵箱,因此務必填寫真實郵箱,而後按要求確認就可,不確認是執行不下去的。
接下來會讓你驗證域名,按要求解析個TXT類型的記錄
保存確認之後再回到服務器中確認
上面這兩個文件就是配置https用到的證書了
咱們先看看官方怎麼說的
這裏我使用的是nginx(申請完會自動幫你重啓nginx)這種方式配置不了泛域名,只能一個一個添加
./certbot-auto --nginx -d api2.nl166.com
解決上述報錯,請注意,/usr/local/nginx 請替換爲你的nginx實際安裝位置 安裝LNMP
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/nginx/conf/ /etc/nginx
再次執行申請
中途會詢問你請選擇是否將HTTP流量重定向到HTTPS,刪除HTTP訪問。可根據本身的需求選擇,我這裏是c取消選擇(實際上這一步能夠直接不理,經測試,這一步沒有選擇前,訪問網址https已經能夠訪問了。)
若是想省略詢問這一步,能夠添加certonly 該種方式只會生成證書,不作其他操做,證書配置須要本身手動添加 以下:
./certbot-auto certonly --nginx -d api2.nl166.com
如上圖所示,若是你配置了監聽443端口的server,他會幫你自動找到對應域名的配置文件,並添加下面兩行,還把我格式打亂了,}號與上面{的縮進不對應了,不過不影響功能,這裏不知道會不會根據nginx的版本選擇是否添加ssl on;
由於我這個nginx版本是不須要這個的,較低版本須要添加ssl on;才能開啓https訪問。
如沒有監聽該域名的443端口,則會在以下位置添加信息
查看nginx是否支持配置https
/usr/local/nginx/sbin/nginx -V
若是尚未--with-http_ssl_module模塊則須要添加模塊 查看如何給nginx升級或增長模塊
1 # HTTPS server 2 # 3 server { 4 listen 443 ssl; 5 server_name api2.nl166.com; 6 root /data/web/im.nl166.com; 7 8 location / { 9 index index.php; 10 } 11 12 #auth_basic "hello world"; 13 #auth_basic_user_file /usr/local/nginx/conf/auth/nl166.auth; 14 15 location ~ \.php$ { 16 include fastcgi_params; 17 fastcgi_pass unix:/tmp/php-fcgi.sock; 18 fastcgi_index index.php; 19 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 20 } 21 location ~ .*.(svn|git|cvs) { 22 deny all; 23 } 24 25 ssl_certificate /etc/letsencrypt/live/api2.nl166.com/fullchain.pem; # managed by Certbot 26 ssl_certificate_key /etc/letsencrypt/live/api2.nl166.com/privkey.pem; # managed by Certbot 27 }
nginx配置https很簡單,只須要複製上面代碼到監聽80端口的server同級。
ssl_certificate 跟 ssl_certificate 分別對應本身的證書文件,其餘配置跟普通http的配置同樣修改成本身對應信息。
較低版本須要增長ssl on;才能開啓https訪問。
其中域名爲/etc/letsencrypt/renewal/目錄下的****.conf ,****就是你要填寫的域名,好比我生成的時候是*.nl166.com,可是在實際生成的時候是沒有*號的
home/certbot-sh/au.sh 替換成你本身更新DNS的腳本
以下:
./certbot-auto renew --cert-name nl166.com --manual-auth-hook /data/shell/crontab/auto_update_httpscert.sh --dry-run
把更新命令放到一個文件,我這裏是放在了/data/shell/crontab/auto_update_httpscert.sh 內容以下 ,原來的auto_update_httpscert.sh 更改到/data/shell/cnl_update_httpscert.sh
增長系統定時任務
crontab -e
#每一個星期天凌晨5點執行更新https證書操做
0 5 * * 0 sh /data/shell/crontab/auto_update_httpscert.sh
以下:
./certbot-auto certonly --renew-by-default --nginx -d api2.nl166.com --dry-run
增長系統定時任務操做參考方式一