(轉)Certbot-免費的https證書
什麼是HTTPS?
HTTP:是網際網路上應用最為廣泛的一種網路協議,是一個客戶端和伺服器端請求和應答的標準(TCP),用於從WWW伺服器傳輸超文字到本地瀏覽器的傳輸協議,它可以使瀏覽器更加高效,使網路傳輸減少。
HTTPS:全稱:Hyper Text Transfer Protocol over Secure Socket Layer,則是以安全為目標的HTTP通道,簡單講是HTTP的安全版,即HTTP下加入SSL層,HTTPS的安全基礎是SSL,因此加密的詳細內容就需要SSL。
HTTPS協議的主要作用可以分為兩種:一種是建立一個資訊保安通道,來保證資料傳輸的安全;另一種就是確認網站的真實性。
如何使用HTTPS?
一般情況下,要使用HTTPS協議,需要有一張被信任的 CA ( Certificate Authority )也就是證書授權中心頒發的 SSL 安全證書,並且將它部署到你的網站伺服器上。一旦部署成功後,當用戶訪問你的網站時,瀏覽器會在顯示的網址前加一把小鎖,表明這個網站是安全的,當然同時你也會看到網址前的字首變成了 https ,不再是 http 了
https請求
獲取SSL證書
理論上,我們自己也可以手動製作一個 SSL 安全證書,但是我們自己簽發的安全證書瀏覽器信任,所以我們需要被信任的證書授權中心( CA )簽發的安全證書。而一般的 SSL 安全證書籤發服務都需要付費,且價格昂貴,不過為了加快推廣 https 的普及, EEF 電子前哨基金會、 Mozilla 基金會和美國密歇根大學成立了一個公益組織叫 ISRG ( Internet Security Research Group ),這個組織從 2015 年開始推出了 Let’s Encrypt 免費證書。這個免費證書不僅免費,而且還相當好用,所以我們就可以利用 Let’s Encrypt 提供的免費證書部署 https 了。
Let’s Encrypt
Let’s Encrypt提供了免費的證書申請服務,同時也提供了官方客戶端 Certbot,開啟首頁,就可以得到官方的安裝教程。官方教程給出了四種常用伺服器和不同的Linux、Unix的安裝使用方案,可以說是十分的貼心了。
Certbot首頁
下面我將會介紹一個通用的安裝方案:
- 獲取
certbot-auto
wget https://dl.eff.org/certbot-auto chmod a+x certbot-auto
- 生成證書
生成證書前需要關閉伺服器
service nginx stop ./certbot-auto certonly
根據提示,輸入相關資料後,如列印類似以下內容,即可在/etc/letsencrypt/archive目錄下得到證書檔案。
How would you like to authenticate with the ACME CA? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: Nginx Web Server plugin (nginx) 2: Spin up a temporary webserver (standalone)【使用臨時Web伺服器(獨立目錄)】 3: Place files in webroot directory (webroot)【將檔案放在webroot目錄】 【選擇3回車】 Plugins selected: Authenticator webroot, Installer None Enter email address (used for urgent renewal and security notices)[email protected]【輸入您的郵箱地址,用於緊急更新和安全通知】 Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (A)gree/(C)ancel: A【選擇A回車同意服務條款,C為拒絕】 Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y 您是否願意分享您的電子郵件地址,建議選擇Y回車】 Please enter in your domain name(s) (comma and/or space separated) (Enter 'c' to cancel): mp.wukuy.cn 【輸入域名回車】 Obtaining a new certificate Performing the following challenges: http-01 challenge for mp.wukuy.cn Input the webroot for mp.wukuy.cn: (Enter 'c' to cancel): /usr/local/www/wechat/mp 【輸入網站所在絕對路徑回車】
成功申請證書
如果不想一步一步走,也可以直接使用以下命令直接生成。注意xxx需要替換為自己的東西。
./certbot-auto certonly --standalone --email [email protected] --agree-tos -d xxx.com -d www.xxx.com
3.配置證書
Nginx中配置SSL證書的配置檔案參考如下:
server { listen 443 ssl; server_name xxx.com; location / { # .... } ssl_certificate /etc/letsencrypt/live/xxx.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/xxx.com/privkey.pem; } server { listen 80; server_name xxx.com; location / { # ... } #如果需要把http強制轉換為https,需要配置以下內容 if ($host = xxx.com) { return 301 https://$host$request_uri; } }
配置完成後,啟動Nginx,瀏覽器中檢視效果。
service nginx start
自動續訂
Certbot可以配置為在證書過期之前自動更新證書。由於Let’s Encrypt SSL證書有效期時間為90天,所以建議您利用此功能。您可以通過執行以下命令來測試證書的自動續訂:
./certbot-auto --nginx certonly
如果以上正常工作,你可以通過新增執行以下操作的cron或systemd定時任務安排自動更新:
./certbot-auto renew
我們寫一個自動執行指令碼,建議每小時執行一次:
sudo crontab -e
新增以下內容:
0 */6 * * * /root/certbot-auto renew --quiet && /bin/systemctl restart nginx
通過命令檢視是否新增成功:
$crontab -l 0 */6 * * * /root/certbot-auto renew --quiet && /bin/systemctl restart nginx
然後重啟crontab
systemctl status crond.service systemctl restart crond.service
通過命令觀察 crontab 是否執行:
tail -f /var/log/cron
證書是否續訂成功,可以通過以下命令管理檢視證書資訊:
certbot-auto certificates
轉自:https://blog.csdn.net/guangcaiwudong/article/details/98858337
轉自:https://blog.csdn.net/weixin_44520133/article/details/104215066