如何使用開源SSL提供商Let's Encrypt來保護Apache
本文將學習如何在Ubuntu伺服器上使用“Let's Encrypt”為Apache Web伺服器設定TLS/SSL證書,還將介紹如何使用cron作業自動更新證書。
先決條件
要完成此演示,我們需要滿足以下要求
- 具有sudo許可權的使用者在Ubuntu伺服器上。
- 已安裝Apache伺服器,並配置了正確的域名。
- 在伺服器上安裝依賴項
我們需要使用以下命令更新伺服器,並安裝git以從儲存庫獲取Let's Encrypt客戶端。
$ sudo apt-get update
$ sudo apt-get install git
將Let's Encrupt客戶端下載到伺服器
這裡我們將從儲存庫下載“Let's Encrypt”客戶端。我們需要將檔案儲存在單獨的位置。由於Let's Encrypt客戶端僅處於測試階段。不建議在生產環境中使用,我們可以將此SSL證書用於開發和測試目的,因此可能需要頻繁更新以修復錯誤並實現新功能。
讓我們將程式碼下載到/opt目錄中,執行以下命令以從github儲存庫獲取更新的程式碼
$ sudo git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt Cloning into '/opt/letsencrypt'... remote: Counting objects: 37273, done. remote: Compressing objects: 100% (54/54), done. remote: Total 37273 (delta 29), reused 1 (delta 1), pack-reused 37217 Receiving objects: 100% (37273/37273), 10.16 MiB | 1.35 MiB/s, done. Resolving deltas: 100% (26562/26562), done. Checking connectivity... done.
這會將程式碼從Let's Encrypt儲存庫獲取到本地資料夾中
設定SSL證書
我們需要使用Let's Encrypt客戶端為Apache生成SSL證書,這非常簡單。客戶端將自動獲取並安裝新的SSL證書,該證書對我們在引數中提供的域名有效。
以下是從伺服器生成和獲取SSL證書的過程。
$ cd /opt/letsencrypt $ sudo ./letsencrypt-auto --apache -d domain.com Bootstrapping dependencies for Debian-based OSes... Hit:1 http://in.archive.ubuntu.com/ubuntu xenial InRelease Hit:2 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease Hit:3 http://security.ubuntu.com/ubuntu xenial-security InRelease Hit:4 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease Reading package lists... Done Reading package lists... Done Building dependency tree Reading state information... Done ca-certificates is already the newest version (20160104ubuntu1). gcc is already the newest version (4:5.3.1-1ubuntu1). The following additional packages will be installed: libexpat1-dev libpython-dev libpython-stdlib libpython2.7-dev python-minimal python-pip-whl python-pkg-resources python2.7 python2.7-dev python2.7-minimal python3-virtualenv zlib1g-dev Suggested packages: augeas-doc augeas-tools python-doc python-tk python-setuptools python2.7-doc binfmt-support Recommended packages: libssl-doc The following NEW packages will be installed: augeas-lenses dialog libaugeas0 libexpat1-dev libffi-dev libpython-dev libpython-stdlib libpython2.7-dev libssl-dev python python-dev python-minimal python-pip-whl python-pkg-resources python-virtualenv python2.7 python2.7-dev python2.7-minimal python3-virtualenv virtualenv zlib1g-dev 0 upgraded, 21 newly installed, 0 to remove and 26 not upgraded. Need to get 33.5 MB of archives. . . . .
如果我們想要獲取和安裝單個有效域名或子域名的證書,可以使用這些選項。
如果我們需要為多個域名生成SSL證書,可以使用以下命令為多個域名獲取證書。
$sudo ./letsencrypt-auto --apache -d domain.com -d domain1.com
這將安裝依賴項,然後將顯示一個基於GUI的分步指南,以自定義證書選項。
如果我們丟失了金鑰,我們可以檢索它,系統會要求您提供郵件地址,以便接收任何通知
您可以選擇啟用http和https訪問,也可以將其配置為將所有http請求重定向到https。
安裝完成後,我們可以在/etc/letsencrypt/live中找到證書檔案。
我們可以使用以下連結驗證SSL證書
https://www.ssllabs.com/ssltest/analyze.html?d=example.com&latest
配置自動更新
通常,Let's Encrypt證書僅有效90天,因此建議每60天更新一次證書,這樣可以在沒有錯誤的情況下進行更新。我們可以使用renew命令來實現。以下是獲取證書更新的命令:
$ ./letsencrypt-auto renew
它將檢查依賴項並檢查更新並獲取證書。
如果我們在機器上為多個域名配置了證書,它將自動更新機器中的所有域名。
我們可以配置cron作業來執行此操作,它將定期自動更新證書,因為伺服器在證書過期前30天內不允許更新。
以下是一個cron作業示例,它將在每天凌晨5:00檢查更新
00 5 * * 1 /opt/letsencrypt/letsencrypt-auto renew >> /var/log/le-renew.log
在本文中,我們學習瞭如何配置和安裝來自“Let's Encrypt”的免費SSL證書,以保護使用Apache Web伺服器託管的網站,由於“Let's Encrypt”處於測試階段。我建議您檢視Let's Encrypt部落格以獲取重要更新。