--- GA: G-7GF24SD6DM --- # [Debian×Let's Encrypt] 自己產生可用的網站免費SSL/TLS HTTPS憑證 ## 前情提要 之前就有做過憑證與證書,最近到期了。 現在想說完整寫次文件,以便後面就不用再繼續翻。 如有需要參考的朋友也歡迎取用。 歡迎留言讓我知道這篇文章對你有用🙏 ## I、安裝套件 ```shell= # apt update # apt install certbot openssl ``` ## II、certbot使用 ```shell! # certbot certonly --manual --preferred-challenges dns -d *.example.com // ----- // certonly # 只要證書/憑證 // --manual # 手動驗證 // --preferred-challenges dns # 驗證方式DNS // -d *.example.com # 證書網域 // ----- ``` 這邊可以使用廣域域名,  上面會要你去給一個Email地址,會有些資訊寄給你,我記得有收過即將到期的信。 申請憑證需要一些驗證手段,如同剛剛上面寫DNS,我們需要去加入些DNS TXT記錄。  以我個人使用Cloudflare為例,可以去新增一個像這樣子的記錄:  接著會卡在一個等你按`Enter`的階段。  請注意,新增完成後不要急著按`Enter`,可使用[DNS Checker](https://dnschecker.org/),檢查DNS記錄發佈出去了嗎?  若按下Enter沒有驗證成功,需要再做一次指令。 成功的話就會如下圖:  也會告訴你證書放在哪了。 ## III、取用證書 產生憑證檔案如下: ```shell= /etc/letsencrypt/live/example.com# ls -l total 20 -rw-r--r-- 1 root root 692 Jan 14 08:15 README lrwxrwxrwx 1 root root 32 Jan 14 08:15 cert.pem -> ../../archive/example.com/cert1.pem lrwxrwxrwx 1 root root 33 Jan 14 08:15 chain.pem -> ../../archive/example.com/chain1.pem lrwxrwxrwx 1 root root 37 Jan 14 08:15 fullchain.pem -> ../../archive/example.com/fullchain1.pem lrwxrwxrwx 1 root root 35 Jan 14 08:15 privkey.pem -> ../../archive/example.com/privkey1.pem // ----- // cert.pem # 網站的SSL憑證,未有完整憑證鏈 // chain.pem # 憑證內容的中繼憑證 // fullchain.pem # 完整憑證鏈之憑證(=cert.pem + chain.pem) // privkey.pem # 私鑰 ``` 一般網站到這邊就可以取用走了。 因我是要做NAS使用的,有些服務需要自行產生證書(例:[Emby Media Server](https://emby.media/support/articles/Secure-Your-Server.html#using-secure-https-connections)) 我要將檔案給複製出來,除此之外還需轉換證書格式(PEM to pk12) 這邊我先將證書轉換格式,還需安裝OpenSSL套件(APT)。 ```shell! openssl pkcs12 -export -in /etc/letsencrypt/live/example.com/fullchain.pem -inkey /etc/letsencrypt/live/example.com/privkey.pem -out ./examplecom.p12 -name "examplecom" -passout pass: // ----- // pkcs12 # 指定格式 // -export # 匯出 // -in # 給定證書檔案 // -inkey # 私鑰 // -out # 輸出路徑 // -name # 給證書取個名字,隨意取 // -passout pass: # 證書密碼,可以為pass:冒號後面不填表示沒密碼 ``` 接著有個指令可以測試產生證書是否OK: ```shell! openssl pkcs12 -info -in examplecom.p12 Enter Import Password: # 若你未給予密碼設定,直接ENTER即可。 ``` `subkect=CN = *.exampe.com`,這邊理應會看到申請的地址。 下面還會看到由Let's Encrypt組織簽發證書。  再往下看,有簽發憑證鏈可以看看。  ## IV、續期與更新 隔了段時間後,期限到了自然需要更新。 我們下達這邊的指令: ```shell! certbot certonly --manual --preferred-challenges dns --force-renewal -d Example.com // ----- // certonly: 僅獲取或續期憑證,不自動安裝到網頁伺服器。 // --manual: 指定使用手動驗證模式。 // --preferred-challenges dns: 明確指定使用 DNS-01 挑戰。 // --force-renewal: 強制續期,即使 Certbot 認為憑證還不需要續期。 // -d Example.com: 指定要續期的域名。 ``` 接著又會回到驗證的步驟,再給他做一次就對了👍 ----- ### 文章授權 LICENSE Copyright 2025 [ChanYuRick](https://github.com/rick890101). [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/) #### 說明~zh-TW~: * 讀者可以將文章內容程式原始碼轉做他用,包括但不限於商業用途、學術研究、個人與非營利使用等。 * 本文章基於CC4.0進行授權,應遵守相關條款進行引用。 #### Help~en-US~: * Readers are allowed to repurpose the source code provided in the article for other uses, including but not limited to commercial purposes, academic research, personal use, and non-profit use. * This article is licensed under CC4.0, and citations should comply with the relevant terms.
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up