部署支持 https 的 Nginx 服務

經過 Certbot 爲 nginx 開啓https支持。html

環境

  • CentOS 7.1
  • python2.x(這玩意系統裏原本就有)

安裝Nginx

sudo yum install nginx -y

順手啓動:python

sudo systemctl start nginx

順手設置開機啓動:linux

sudo systemctl enable nginx

嗯,就完成了。 至於配置文件,會在後面設置。nginx

配置https

此處咱們使用 Let's Encrypt 提供的證書。且爲了方便設置,使用 Certbot 配置工具。git

工具得到

證書機構: Let's Encrypt - https://letsencrypt.org
配置工具: Certbot - https://certbot.eff.org/web

事實上,你根本用不到上面兩個連接,我把它們寫在這只是爲了方便了解其餘細節,順便表示尊重。api

實際上咱們能夠直接經過包管理器獲取 Certbot 工具。瀏覽器

首先須要安裝 EPEL 源:app

sudo yum install epel-release -y

而後安裝 Certbot :dom

sudo yum install python2-certbot-nginx -y

工具安裝完成。

使用 Certbot

Certbot 使用命令行中的交互式配置,咱們啓動它,而後跟着指示一步一步完成就行。

1、 啓動 Certbot

經過命令:

sudo certbot --nginx

2、 填寫郵箱

在下述提示後,填寫你的郵箱地址。

Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): xxxx@xxxx.com

輸入你的郵箱地址,回車肯定。

3、 贊成用戶協議

下述提示提醒你閱讀並贊成用戶協議之類的。

Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf.
You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

輸入字母 A 回車肯定。

4、 請求分享你的郵箱

意思是他們會沒事給你發發廣告郵件。贊成就是了 ╮(╯▽╰)╭

Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

輸入字母 Y 回車肯定。

5、 指定域名

因爲咱們在安裝nginx後沒有配置站點,因此此處要求咱們提供域名,配置工具會幫咱們填寫nginx的配置文件。

No names were found in your configuration files. Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c' to cancel): www.xxxx.com xxx.xxxx.com

輸入你本身的域名(多個域名中間用空格隔開)回車肯定。

6、 重定向

會詢問你是否是要把全部http請求重定向到https。固然要了~

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. 
You can undo this change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

輸入數字 2 回車肯定。

7、 完成

此時配置已經完成。你能夠在接下來的輸出中找到以下段落:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://www.xxxx.com and https://xxx.xxxx.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=www.xxxx.com
https://www.ssllabs.com/ssltest/analyze.html?d=xxx.xxxx.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

意思就是你已經成功配置了 www.xxxx.com xxx.xxxx.com 兩個域名(就是在 步驟五 輸入的那兩個,固然,你輸入了多少個這裏就會顯示多少個)。
而且你能夠在 https://www.ssllabs.com/ 這個網站上測試域名的狀態。

8、 證書過時

因爲 Let's Encrypt 的免費證書有效期是90天,因此你須要每80幾天從新申請一次。

Certbot 能夠經過簡單的命令完成這個工做:

certbot renew

若是你仍是以爲麻煩,能夠把這個操做設爲定時任務,每80幾天運行一次,就能夠高枕無憂了。

其餘

支持https的nginx已經徹底配置完成。接下來把你的站點放在nginx的目錄下就行,通常是 /usr/share/nginx/html 若是不是這裏,你能夠在nginx的配置文件裏找到,配置文件位於 /etc/nginx/nginx.conf

在瀏覽器中打開站點,就能看到地址欄上的小綠鎖了~


原文發佈於 https://blog.moonlightwatch.me/linux/2018/08/30/nginx-https/

相關文章
相關標籤/搜索