nginx專(zhuān)為性能優(yōu)化而開(kāi)發(fā),其最知名的優(yōu)點(diǎn)是它的穩(wěn)定性和低系統(tǒng)資源消耗,以及對(duì)并發(fā)連接的高處理能力(單臺(tái)物理服務(wù)器可支持30000~50000個(gè)并發(fā)連接), 是一個(gè)高性能的 http 和反向代理服務(wù)器,也是一個(gè)imap/pop3/smtp 代理服。
安裝 Certbot
第一步是安裝 certbot,該軟件客戶端可以幾乎自動(dòng)化所有的過(guò)程。 Certbot 開(kāi)發(fā)人員維護(hù)自己的 ubuntu 倉(cāng)庫(kù),其中包含比 Ubuntu 倉(cāng)庫(kù)中存在的軟件更新的軟件。
添加 Certbot 倉(cāng)庫(kù):
#?add-apt-repository?ppa:certbot/certbot
接下來(lái),更新 APT 源列表:
#?apt-get?update
此時(shí),可以使用以下 apt 命令安裝 certbot:
#?apt-get?install?certbot
Certbot 現(xiàn)已安裝并可使用。
獲得證書(shū)
有各種 Certbot 插件可用于獲取 ssl 證書(shū)。這些插件有助于獲取證書(shū),而證書(shū)的安裝和 Web 服務(wù)器配置都留給管理員。
我們使用一個(gè)名為 Webroot 的插件來(lái)獲取 SSL 證書(shū)。
在有能力修改正在提供的內(nèi)容的情況下,建議使用此插件。在證書(shū)頒發(fā)過(guò)程中不需要停止 Web 服務(wù)器。
配置 nginx
Webroot 會(huì)在 Web 根目錄下的 .well-known 目錄中為每個(gè)域創(chuàng)建一個(gè)臨時(shí)文件。在我們的例子中,Web 根目錄是 /var/www/html。確保該目錄在 Let’s Encrypt 驗(yàn)證時(shí)可訪問(wèn)。為此,請(qǐng)編輯 NGINX 配置。使用文本編輯器打開(kāi) /etc/nginx/sites-available/default:
#?$EDITOR?/etc/nginx/sites-available/default
在該文件中,在 server 塊內(nèi),輸入以下內(nèi)容:
location?~?/.well-known?{ allow?all; }
保存,退出并檢查 NGINX 配置:
#?nginx?-t
沒(méi)有錯(cuò)誤的話應(yīng)該會(huì)顯示如下:
nginx:?the?configuration?file?/etc/nginx/nginx.conf?syntax?is?ok nginx:?configuration?file?/etc/nginx/nginx.conf?test?is?successful
重啟 NGINX:
#?systemctl?restart?nginx
使用 Certbot 獲取證書(shū)
下一步是使用 Certbot 的 Webroot 插件獲取新證書(shū)。在本教程中,我們將保護(hù)示例域 www.example.com。需要指定應(yīng)由證書(shū)保護(hù)的每個(gè)域。執(zhí)行以下命令:
#?certbot?certonly?--webroot?--webroot-path=/var/www/html?-d?www.example.com
在此過(guò)程中,Cerbot 將詢問(wèn)有效的電子郵件地址,用于進(jìn)行通知。還會(huì)要求與 EFF 分享,但這不是必需的。在同意服務(wù)條款之后,它將獲得一個(gè)新的證書(shū)。
最后,目錄 /etc/letsencrypt/archive 將包含以下文件:
-
chain.pem:Let’s Encrypt 加密鏈證書(shū)。
-
cert.pem:域名證書(shū)。
-
fullchain.pem:cert.pem和 chain.pem 的組合。
-
privkey.pem:證書(shū)的私鑰。
Certbot 還將創(chuàng)建符號(hào)鏈接到 /etc/letsencrypt/live/domain_name/ 中的最新證書(shū)文件。這是我們將在服務(wù)器配置中使用的路徑。
在 NGINX 上配置 SSL/TLS
下一步是服務(wù)器配置。在 /etc/nginx/snippets/ 中創(chuàng)建一個(gè)新的代碼段。 snippet 是指一段配置,可以包含在虛擬主機(jī)配置文件中。如下創(chuàng)建一個(gè)新的文件:
#?$EDITOR?/etc/nginx/snippets/secure-example.conf
該文件的內(nèi)容將指定證書(shū)和密鑰位置。粘貼以下內(nèi)容:
ssl_certificate?/etc/letsencrypt/live/domain_name/fullchain.pem; ssl_certificate_key?/etc/letsencrypt/live/domain_name/privkey.pem;
在我們的例子中,domain_name 是 example.com。
編輯 NGINX 配置
編輯默認(rèn)虛擬主機(jī)文件:
#?$EDITOR?/etc/nginx/sites-available/default
如下:
server?{ listen?80?default_server; listen?[::]:80?default_server; server_name?www.example.comreturn?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?ok nginx:?configuration?file?/etc/nginx/nginx.conf?test?is?successful
重啟 NGINX:
#?systemctl?restart?nginx