什麼是 Let’s Encrypt
Let’s Encrypt 是網際網路安全研究組織 (ISRG) 提供的免費證書認證機構。它提供了一種輕松自動的方式來擷取免費的 SSL/TLS 證書 - 這是在 Web 伺服器上啟用加密和 HTTPS 流量的必要步驟。擷取和安裝證書的大多數步驟可以通過使用名為 Certbot 的工具進行自動化。
特别地,該軟體可在可以使用 shell 的伺服器上使用:換句話說,它可以通過 SSH 連接配接使用。
在本教程中,我們将看到如何使用
certbot
擷取免費的 SSL 證書,并在 Ubuntu 16.04 伺服器上使用 Nginx。
安裝 Certbot
第一步是安裝
certbot
,該軟體用戶端可以幾乎自動化所有的過程。 Certbot 開發人員維護自己的 Ubuntu 倉庫,其中包含比 Ubuntu 倉庫中存在的軟體更新的軟體。
添加 Certbot 倉庫:
# add-apt-repository ppa:certbot/certbot
複制
接下來,更新 APT 源清單:
# apt-get update
複制
此時,可以使用以下
apt
指令安裝
certbot
:
# apt-get install certbot
複制
Certbot 現已安裝并可使用。
獲得證書
有各種 Certbot 插件可用于擷取 SSL 證書。這些插件有助于獲驗證書,而證書的安裝和 Web 伺服器配置都留給管理者。
我們使用一個名為 Webroot 的插件來擷取 SSL 證書。
在有能力修改正在提供的内容的情況下,建議使用此插件。在證書頒發過程中不需要停止 Web 伺服器。
配置 NGINX
Webroot 會在 Web 根目錄下的
.well-known
目錄中為每個域建立一個臨時檔案。在我們的例子中,Web 根目錄是
/var/www/html
。確定該目錄在 Let’s Encrypt 驗證時可通路。為此,請編輯 NGINX 配置。使用文本編輯器打開
/etc/nginx/sites-available/default
:
# $EDITOR /etc/nginx/sites-available/default
複制
在該檔案中,在
server
塊内,輸入以下内容:
location ~ /.well-known { allow all; }
複制
儲存,退出并檢查 NGINX 配置:
# nginx -t
複制
沒有錯誤的話應該會顯示如下:
nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
複制
重新開機 NGINX:
# systemctl restart nginx
複制
使用 Certbot 獲驗證書
下一步是使用 Certbot 的 Webroot 插件擷取新證書。在本教程中,我們将保護示例域 www.example.com。需要指定應由證書保護的每個域。執行以下指令:
# certbot certonly --webroot --webroot-path=/var/www/html -d www.example.com
複制
在此過程中,Cerbot 将詢問有效的電子郵件位址,用于進行通知。還會要求與 EFF 分享,但這不是必需的。在同意服務條款之後,它将獲得一個新的證書。
最後,目錄
/etc/letsencrypt/archive
将包含以下檔案:
-
:Let’s Encrypt 加密鍊證書。chain.pem
-
:域名證書。cert.pem
-
:fullchain.pem
和cert.pem
的組合。chain.pem
-
:證書的私鑰。privkey.pem
Certbot 還将建立符号連結到
/etc/letsencrypt/live/domain_name/
中的最新證書檔案。這是我們将在伺服器配置中使用的路徑。
在 NGINX 上配置 SSL/TLS
下一步是伺服器配置。在
/etc/nginx/snippets/
中建立一個新的代碼段。 snippet 是指一段配置,可以包含在虛拟主機配置檔案中。如下建立一個新的檔案:
# $EDITOR /etc/nginx/snippets/secure-example.conf
複制
該檔案的内容将指定證書和密鑰位置。粘貼以下内容:
ssl_certificate /etc/letsencrypt/live/domain_name/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/domain_name/privkey.pem;
複制
在我們的例子中,
domain_name
是
example.com
。
編輯 NGINX 配置
編輯預設虛拟主機檔案:
# $EDITOR /etc/nginx/sites-available/default
複制
如下:
server { listen 80 default_server; listen [::]:80 default_server; server_name www.example.com return 301 https://$server_name$request_uri; # SSL configuration # listen 443 ssl default_server; listen [::]:443 ssl default_server; include snippets/secure-example.conf # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # ...
}
複制
這将啟用 NGINX 加密功能。
儲存、退出并檢查 NGINX 配置檔案:
# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
複制
重新開機 NGINX:
# systemctl restart nginx
複制