HTTPS 簡介及使用官方工具 Certbot 配置 Let’s Encrypt SSL 安全證書詳細教程

本文內容較長,因為涵蓋背景知識、基本概念和完整的實操過程,如果一步步照著做的話,應該就可以一次配置成功。不想要了解背景知識的同學可以直接跳到實操過程,六步即可使用 Let's Encrypt 官方工具 Certbot 配置 SSL 安全證書。
♦背景知識
已經了解過的同學或計算機相關專業的同學直接略過,跳到第三節。
1、http 和 https 是什麼?
簡單來說,http 是一個傳輸網頁內容的協議,比如你看到的 http 開頭的網站 http://www.163.com,其網頁上的文字、圖片、 CSS 、 JS 等文件都是通過 http 協議傳輸到我們的瀏覽器,然後被我們看到。
而 https 可以理解為「 HTTP over SSL/TLS 」,好端端的 http 為什麼需要「 over SSL/TLS 」呢,因為 http 是明文傳輸的,通過 http 協議傳輸的內容很容易被偷看和篡改,為了安全(你肯定不想被人偷看或者篡改網頁內容吧,比如網站銀行密碼什麼的。)就為 http 協議再加上了一層 SSL/TLS 安全協議,所以就有了 https 。
2、SSL/TLS 是什麼?
「 HTTP over SSL/TLS 」字面意思就是帶「安全套接層」的 http 協議,內心純潔的同學也可以理解為「帶安全套的 http 」,因為帶了安全套,所以當然會比較安全(/(ㄒoㄒ)/~~)。其中 SSL 是「 Secure Sockets Layer 」 的縮寫,是「安全套接層」的意思。 TLS 是 "Transport Layer Security" 的縮寫,是 " 傳輸層安全協議 " 的意思。 SSL 和 TLS 是同一個東西的不同階段,理解為同一個東西也行,都是安全協議就對了。
3、為什麼要部署 https?
說到底,就是 https 更安全。甚至為了安全,一個專業可靠的網站, https 是必須的。 Firefox 和 Chrome 都計劃將沒有配置 SSL 加密的 http 網站標記為不安全(貌似 Firefox 50 已經這麼幹了),目前它們也正在聯合其他相關的基金會與公司推動整個互聯網 https 化,現在大家訪問的一些主要的網站。如 Google 多年前就已經全部啟用 https ,國內的淘寶、搜狗、知乎、百度等等也全面 https 了。甚至 Google 的搜索結果也正在給予 https 的網站更高的排名和優先收錄權。
4、怎麼部署 https 呢?
你只需要有一張被信任的 CA ( Certificate Authority )也就是證書授權中心頒發的 SSL 安全證書,並且將它部署到你的網站伺服器上。一旦部署成功後,當用戶訪問你的網站時,瀏覽器會在顯示的網址前加一把小綠鎖,表明這個網站是安全的,當然同時你也會看到網址前的前綴變成了 https ,不再是 http 了。
5、怎麼獲得 SSL 安全證書呢?
理論上,我們自己也可以簽發 SSL 安全證書,但是我們自己簽發的安全證書不會被主流的瀏覽器信任,所以我們需要被信任的證書授權中心( CA )簽發的安全證書。而一般的 SSL 安全證書籤發服務都比較貴,比如 Godaddy 、 GlobalSign 等機構簽發的證書一般都需要20美金一年甚至更貴,不過為了加快推廣 https 的普及, EEF 電子前哨基金會、 Mozilla 基金會和美國密歇根大學成立了一個公益組織叫 ISRG ( Internet Security Research Group ),這個組織從 2015 年開始推出了 Let's Encrypt 免費證書。這個免費證書不僅免費,而且還相當好用,所以我們就可以利用 Let's Encrypt 提供的免費證書部署 https 了。那麼怎麼獲得 Let's Encrypt 安全證書,並且將它部署在自己的網站伺服器上呢?這就是這篇文章要講的內容了。
註:對 https 、數字證書及 CA 的工作原理感興趣的同學可以閱讀編程隨想的系列文章。
♦ Let's Encrypt 及 Certbot 簡介
前面已經介紹過, Let's Encrypt 是 一個叫 ISRG ( Internet Security Research Group ,互聯網安全研究小組)的組織推出的免費安全證書計劃。參與這個計劃的組織和公司可以說是互聯網頂頂重要的先驅,除了前文提到的三個牛氣哄哄的發起單位外,後來又有思科(全球網路設備製造商執牛耳者)、 Akamai 加入,甚至連 Linux 基金會也加入了合作,這些大牌組織的加入保證了這個項目的可信度和可持續性。
儘管項目本身以及有該項目簽發的證書很可信,但一開始 Let's Encrypt 的安全證書配置起來比較麻煩,需要手動獲取及部署。存在一定的門檻,沒有一些技術底子可能比較難搞定。然後有一些網友就自己做了一些腳本來優化和簡化部署過程。其中有一個網友 xdtianyu 做了一個可以快速獲取及更新證書的 letsencrypt.sh 腳本被廣泛使用,地址如下:
https://raw.githubusercontent.com/xdtianyu/scripts/master/lets-encrypt/letsencrypt.sh
首先要感謝 xdtianyu 同學的努力付出。我之前用的也是這個腳本,不知道是不是姿勢不對,雖然順利地按照流程裝上了,但用 SSLlabs 檢測我配的網站還是不太安全,在很多瀏覽器上都被標註為「不安全」。當時因為忙也沒有太多時間折騰和細究,不過後來 ISRG 的發起者 EFF (電子前哨基金會)為 Let's Encrypt 項目發布了一個官方的客戶端 Certbot ,利用它可以完全自動化的獲取、部署和更新安全證書。這真是非常容易、方便呀,所以我們就可以直接使用官方客戶端,不需要再使用第三方的工具了。雖然第三方工具也可以使用,但是官方工具更權威,風險也更小,而且遇到問題也更容易解決,畢竟有官方的支持。何況 Certbot 確實非常方便,也比所有的第三方工具都更方便,何樂而不用呢?
♦官方客戶端 Certbot 使用方法
Certbot 的官方網站是 https://certbot.eff.org/ ,打開這個鏈接選擇自己使用的 web server 和操作系統,EFF 官方會給出詳細的使用方法,如下圖,不過我覺得這樣還是太複雜,太麻煩,所以建議讀者朋友可以不用看這個網站,按照我的方法走一遍即可。我自己的個人網站( https://wenqixiang.com )和本站( https://linuxstory.org )都是按此方法配置,以下以本網站域名( linuxstory.org )舉例。
假定運行環境:Debian 8.0 64 位(本教程同樣適用於 Debian 、 Ubuntu 、 CentOS 、 Arch Linux 、), Web 伺服器是 Nginx 1.10 ,當前工作目錄為 /home/linuxstory 家目錄。如果是從零開始配置伺服器,推薦使用 Debian 8 + lnmp 作為網站環境。
1、獲取 Certbot 客戶端
wget https://dl.eff.org/certbot-auto chmod a+x ./certbot-auto ./certbot-auto --help
2、配置 nginx 、驗證域名所有權
在虛擬主機配置文件( /usr/local/nginx/conf/vhost/linuxstory.org.conf )中添加如下內容,這一步是為了通過 Let's Encrypt 的驗證,驗證 linuxstory.org 這個域名是屬於我的管理之下。(具體解釋可見下一章「一些補充說明」的「 certbot 的兩種工作方式」)
location ^~ /.well-known/acme-challenge/ { default_type "text/plain"; root /home/wwwroot/linuxstory.org/; } location = /.well-known/acme-challenge/ { return 404; }
3、重載 nginx
配置好 Nginx 配置文件,重載使修改生效(如果是其他系統 nginx 重載方法可能不同)
sudo nginx -s reload
4、生成證書
./certbot-auto certonly --webroot -w /home/wwwroot/linuxstory.org -d linuxstory.org
中間會有一些自動運行及安裝的軟體,不用管,讓其自動運行就好,有一步要求輸入郵箱地址的提示,照著輸入自己的郵箱即可,順利完成的話,屏幕上會有提示信息。
此處有坑!如果順利執行請直接跳到第五步,我在自己的伺服器上執行多次都提示
connection :: The server could not connect to the client for DV :: DNS query timed out
發現問題出在 DNS 伺服器上,我用的是 DNSpod ,無法通過驗證,最後是將域名的 DNS 伺服器臨時換成 Godaddy 的才解決問題,通過驗證,然後再換回原來的 DNSpod 。
證書生成成功後,會有 Congratulations 的提示,並告訴我們證書放在 /etc/letsencrypt/live 這個位置
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/linuxstory.org/fullchain.pem. Your cert will expire on 2017-02-011. To obtain a new version of the certificate in the future, simply run Let's Encrypt again.
5、配置 Nginx(修改 /usr/local/nginx/conf/vhost/linuxstory.org.conf),使用 SSL 證書
listen 443 ssl; server_name linuxstory.org www.linuxstory.org; index index.html index.htm index.php; root /home/wwwroot/linuxstory.org; ssl_certificate /etc/letsencrypt/live/linuxstory.org/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/linuxstory.org/privkey.pem;
上面那一段是配置了 https 的訪問,我們再添加一段 http 的自動訪問跳轉,將所有通過 http://www.linuxstory.org 的訪問請求自動重定向到 https://linuxstory.org
server { listen 80; server_name linuxstory.org www.linuxstory.org; return 301 https://$server_name$request_uri; }
6、重載 nginx,大功告成,此時打開網站就可以顯示綠色小鎖了
sudo nginx -s reload
♦後續工作
出於安全策略, Let's Encrypt 簽發的證書有效期只有 90 天,所以需要每隔三個月就要更新一次安全證書,雖然有點麻煩,但是為了網路安全,這是值得的也是應該的。好在 Certbot 也提供了很方便的更新方法。
1、測試一下更新,這一步沒有在真的更新,只是在調用 Certbot 進行測試
./certbot-auto renew --dry-run
如果出現類似的結果,就說明測試成功了(總之有 Congratulations 的字眼)
Congratulations, all renewals succeeded. The following certs have been renewed: /etc/letsencrypt/live/linuxstory.org/fullchain.pem (success) ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates above have not been saved.)
2、手動更新的方法
./certbot-auto renew -v
3、自動更新的方法
./certbot-auto renew --quiet --no-self-upgrade
♦一些補充說明解釋
1、certbot-auto 和 certbot
certbot-auto 和 certbot 本質上是完全一樣的;不同之處在於運行 certbot-auto 會自動安裝它自己所需要的一些依賴,並且自動更新客戶端工具。因此在你使用 certbot-auto 情況下,只需運行在當前目錄執行即可
./certbot-auto
2、certbot的兩種工作方式
certbot (實際上是 certbot-auto ) 有兩種方式生成證書:
- standalone 方式: certbot 會自己運行一個 web server 來進行驗證。如果我們自己的伺服器上已經有 web server 正在運行 (比如 Nginx 或 Apache ),用 standalone 方式的話需要先關掉它,以免衝突。
- webroot 方式: certbot 會利用既有的 web server,在其 web root目錄下創建隱藏文件, Let's Encrypt 服務端會通過域名來訪問這些隱藏文件,以確認你的確擁有對應域名的控制權。
本文用的是 webroot 方式,也只推薦 webroot 方式,這也是前文第二步驗證域名所有權在 nginx 虛擬主機配置文件中添加 location 段落內容的原因。
嗚呼,安得 SSL 安全證書千萬張,大庇天下網站安全俱歡顏!希望互聯網早日進入 https 普及化。
本文鏈接: https://linuxstory.org/deploy-lets-encrypt-ssl-certificate-with-certbot
LinuxStory 原創教程,轉載請註明出處,否則必究相關責任。
Firma Platformu Türkiye’nin en büyük firma rehberidir.
Have any favorite blog posts or writers? Share them with us in the comments!
Resetting Firestick Remote – resettingfirestickremote.com
Tanıtım forumu ile işletmeni hemen tanıt.
Liftmaster Battery Replacement – liftmasterbatteryreplacement.com
Knock Sensor Replacement – knocksensorreplacement.com
Esta es la primera vez que visito este sitio y estoy muy feliz de leer todo en un solo lugar.
Hocam Ellerinize Sağlık, ceza avukatı ve ceza davası hakkında Detaylı ve Güzel Makale Olmuş.
It means so much to receive positive feedback and know that my content is appreciated. I strive to bring new ideas and insights to my readers.
Ders kitabı cevapları için hemen takip et.
Love this appreciation for great content
Zey forum ile gündemi takip et.
Hisse forumu ile sende yatırımına yön ver.
en iyi eskortlar burada çıtır
sislikadin
BTC forum ile kripto dünyasını keşfet.
Your positive and uplifting words are like a ray of sunshine on a cloudy day Thank you for spreading light and positivity in the world
Haber Forum ile gündemi takip et.
Ekonomi forumu ile gündemi takip et.
Eskortların en güzeli burada anında kapında eskort kadın seksin dorukları burada
Soru bankası cevapları için hemen ziyaret et.
Firma sorgulama hizmeti ile tüm merak ettiklerin burada…
bilgi, rehberlik, teknoloji, sağlık, yaşam, eğitim, ticaret, yemek tarifleri, gezi rehberleri, kültür ve sanat, pratik bilgiler, püf noktaları, günlük yaşam ipuçları, diyet önerileri, spor ve egzersiz, anne ve çocuk, iş ilanları, kariyer rehberi, sürdürülebilir yaşam, minimalizm, ikinci el ürünler, firma rehberi, hayvan dostları, rüya tabirleri, dini bilgiler, burç yorumları, finansal rehberlik, girişimcilik, online eğitim, siber güvenlik, web geliştirme, yapay zeka, mobil uygulamalar, hobi ve el sanatları, oyun rehberi, blog içerikleri, kullanıcı yorumları, yaşam tarzı, bilgi merkezi, infolife
Dxd Global | Development dxd global, global dxd, deluxe bilisim, deluxe global, IT solutions, web developer, worpress global, wordpress setup
hey my friend, good webiste. welcome to read our research about online gaming.
This is exactly what I needed to read today Your words have given me a new perspective and renewed hope Thank you
Порадьте топ 10 кращих конструкторів сайтів для стартапів.
I am blown away by the depth and detail in your posts Keep up the excellent work and thank you for sharing your knowledge with us
I love how this blog celebrates diversity and inclusivity It’s a reminder that we are all unique and should embrace our differences
Dxd Global | Development dxd global, global dxd, deluxe bilisim, deluxe global, IT solutions, web developer, worpress global, wordpress setup
This blog is like a safe haven for me, where I can escape the chaos of the world and indulge in positivity and inspiration
I do not even understand how I ended up here, but I assumed this publish used to be great
We just wanted to take a moment to acknowledge all the hard work and effort you’ve been putting in lately. Keep up the amazing job, you’re doing great!
It’s always a joy to stumble upon content that genuinely makes an impact and leaves you feeling inspired. Keep up the great work!
Your positivity and optimism are contagious It’s impossible to read your blog without feeling uplifted and inspired Keep up the amazing work
Leave a comment and let us know what your favorite blog post has been so far!
From start to finish, your content is simply amazing. You have a talent for making complex topics easy to understand and I always come away with valuable insights.
Your posts always seem to lift my spirits and remind me of all the good in the world Thank you for being a beacon of positivity
Your writing has a way of resonating with me and making me feel understood Thank you for being a relatable and authentic voice
Eskort Çapa- en ucuz bakımlı 18 lik çıtır eskortlar
Share with us in the comments your favorite blog posts of all time!
Drop a link to your favorite blog post of yours in the comments below, I’d love to read more.
I love how this blog promotes a healthy and balanced lifestyle It’s a great reminder to take care of our bodies and minds
Every time I read one of your posts, I come away with something new and interesting to think about. Thanks for consistently putting out such great content!
There is definately a lot to find out about this subject. I like all the points you made trial 3 days : https://downloads.pythonanywhere.com
Your blog is a great source of positivity and inspiration in a world filled with negativity Thank you for making a difference
Well-articulated and full of value. Please check my post at https://mazkingin.com
This blog is a great resource for anyone looking to live a more mindful and intentional life Thank you for providing valuable advice and tips
As someone who struggles with mental health, I appreciate the support and empathy displayed in your blog It means a lot to know I’m not alone
hey my friend, good webiste. welcome to read our research about online gaming.
Infolife, teknoloji, yaşam, sağlık, ticaret ve eğitim gibi birçok kategoride pratik bilgiler ve rehberlik sunan, hayatı kolaylaştıran bilgi platformudur.
Your latest blog post was truly inspiring and had some great insights. I can’t wait to see what else you have in store.
Share your favorite blog post in the comments below!
Love this blog! The content is always so relevant and insightful, keep up the great work!
For the reason that the admin of this site is working, no uncertainty very quickly it will be renowned, due to its quality contents about Yunnan Coffee.
Bahisbu giriş adresi ile sende kazan.
Bahisbu giriş adresi için hemen ziyaret et.
Поради щодо вибору CRM для НКО. Які CRM системи найкраще підходять для некомерційних організацій? Чи є спеціалізовані рішення?
Thank you for the amazing blog post!
Your passion for what you do is evident in every post It’s inspiring to see someone truly fulfilling their purpose and making a positive impact
Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others. trial 3 days : https://downloads.pythonanywhere.com
Oil Changed – oilchanged.org
Betturkey giriş adresi için ziyaret et.
Blink batteries – blinkbatteries.com
Jeep remote start – jeepremotestart.com
C4 transmission – c4transmission.com
Urfa haberleri için hemen web sitesini ziyaret et.
Canlı maç izlemek için ziyaret et.
I have been struggling with this issue for a while and your post has provided me with much-needed guidance and clarity Thank you so much
The positivity and optimism conveyed in this blog never fails to uplift my spirits Thank you for spreading joy and positivity in the world
Dxd Global | Development dxd global, global dxd, deluxe bilisim, deluxe global, IT solutions, web developer, worpress global, wordpress setup
İzmir haberlerini takip etmek için Son Mühür’ü takip et.
Your passion for what you do is evident in every post It’s inspiring to see someone truly fulfilling their purpose and making a positive impact
Slot giriş adresi için hemen ziyaret et.
Key Fob Battery – keyfobbattery.org
Your honesty and vulnerability in sharing your personal experiences is truly admirable It takes courage to open up and I applaud you for it
Pretty! This has been a really wonderful post. Many thanks for providing these details.trial 3 days : https://downloads.pythonanywhere.com
SMS Onay hizmeti için hemen ziyaret et. En uygun fiyatlar burada…
Your posts always provide me with a new perspective and encourage me to look at things differently Thank you for broadening my horizons
Welcome to the best casino in TW.
[…] 本文鏈接: https://linuxstory.org/deploy-lets-encrypt-ssl-certificate-with-certbot […]
[…] https://linuxstory.org/deploy-lets-encrypt-ssl-certificate-with-certbot/zh-tw/ […]
作為一名負責的遊客,必須寫下到此一游
[…] 我們還將安裝此 certbot 以自動續訂 Let』s Encrypt 的證書。(譯者註:想了解更多內容,可閱讀HTTPS 簡介及使用官方工具 Certbot 配置 Let』s Encrypt SSL 安全證書詳細教程 | Linu…。) […]
[…] 《HTTPS 簡介及使用官方工具 Certbot 配置 Let』s Encrypt SSL 安全證書詳細教程》 […]
[…] certbot有一個依賴python-cryptography,要求版本>=7.0,然而apt-get里的最高版本只有6.1,使用pip手動安裝,apt-get又無法識別,只能另尋他路。 搜索了一下,看到這篇文章,EFF官方還搞了一個客戶端certbot-auto,wget下來之後運行自動下載依賴,這次安裝倒沒有碰到什麼兼容性問題,但這個東西似乎不支持Cloudflare DNS插件的安裝。 […]
[…] https://linuxstory.org/deploy-lets-encrypt-ssl-certificate-with-certbot […]
這篇文章有點誤人子弟,環境不同跟著你會走很大的彎路,浪費很多時間。
事實上,跟著官網,選擇好自己的環境,按提示去做非常容易,幾分鐘就搞定了。
[…] HTTPS 簡介及使用官方工具 Certbot 配置 Let』s Encrypt SSL 安全證書詳細教程 […]
[…] LinuxStory 原創教程: https://linuxstory.org/deploy-lets-encrypt-ssl-certificate-with-certbot/ […]
大佬,你的域名已經標註為不安全拉~~~~
[…] HTTPS 簡介及使用官方工具 Certbot 配置 Let』s Encrypt SSL 安全證書詳細教程 […]
[…] certbot有一個依賴python-cryptography,要求版本>=7.0,然而apt-get里的最高版本只有6.1,使用pip手動安裝,apt-get又無法識別,只能另尋他路。 搜索了一下,看到這篇文章,EFF官方還搞了一個客戶端certbot-auto,wget下來之後運行自動下載依賴,這次安裝倒沒有碰到什麼兼容性問題。 […]
大佬,怎麼樣才能創建www.xxx.com這樣的證書呢,現在只能創建xxx.com這樣的
另外,證書怎麼樣才能刪除二次重新創建呢
博主,我用的是lnmp1.4版本,不會自動更新證書,能幫我看看原因嗎?
[…] cerbot項目地址 通用教程 […]
[…] cerbot項目地址 通用教程 […]
[…] HTTPS 簡介及使用官方工具 Certbot配置Let』sEncrypt SSL安全證書詳細教程 […]
博主,打擾下,我按您的方式配置完畢,目前網站首頁已可用 HTTPS 訪問並且顯示安全,但同時產生了另外幾個問題。
重啟 nginx 時提示 「conflicting server name “site.com” on 0.0.0.0:80, ignored」
原 HTTP 鏈接未自動跳轉到 HTPS
網站內頁使用 HTTPS 訪問時返回 404
系統是 CentOS 6,Nginx 配置文件在此 https://1drv.ms/t/s!AtnAYMg0k1P1gox2NsLlHHvlI904ew ,能否幫忙看下
[…] HTTPS 簡介及使用官方工具 Certbot 配置 Let’s Encrypt SSL 安全證書詳細教程 […]
[…] HTTPS 簡介及使用官方工具 Certbot 配置 Let』s Encrypt SSL 安全證書詳細教程 […]
你好 博主 我也是按照您的方法申請好了證書 但是提示證書無效 錯誤代碼: SSL_ERROR_BAD_CERT_DOMAIN
[…] 申請一個證書,極力推薦Let’s Encrypt,免費而且可以認證。申請方法見這篇文章。 […]
[…] HTTPS 簡介及使用官方工具 Certbot 配置 Let’s Encrypt SSL 安全證書詳細教程 […]
你好博主,我按照你的教程部署了,但是瀏覽器提示不安全,伺服器的證書和域名信息不相符
[…] https://linuxstory.org/deploy-lets-encrypt-ssl-certificate-with-certbot […]
步驟清晰準確,我按照你提供的步驟已經成功部署ssl證書了。 唯一不足的就是90天後要手動更新一次證書,但為了安全,忍了~
[…] 來源: LinuxStory https://linuxstory.org/deploy-lets-encrypt-ssl-certificate-with-certbot/ […]
你好版主,這段時間我一直為部署ssl而煩惱,我的伺服器環境是ubuntu16.0.4 + lamp環境,試了很多次,在網上找了很多文章都不能正確安裝配置letsencrypt,希望能等到版主的幫助,我的電子郵箱:zxmatline@163.com.,非常感謝
你好,請問你遇到的問題是什麼?可否描繪一下?