1. 程式人生 > >Linux 搭建Nginx並新增配置 SSL 證書

Linux 搭建Nginx並新增配置 SSL 證書

2. 安裝準備 2.1 gcc安裝 安裝 nginx 需要先將官網下載的原始碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:
[[email protected] ~]# yum -y install gcc-c++

 

2.2 pcre安裝 PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 相容的正則表示式庫。nginx 的 http 模組使用 pcre 來解析正則表示式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。
[[email protected] ~]# yum -y install pcre pcre-devel

 

2.3 zlib安裝 zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。
[[email protected] ~]# yum -y install zlib zlib-devel

 

2.4 OpenSSL安裝 OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼演算法、常用的金鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程式供測試或其它目的使用。 nginx 不僅支援 http 協議,還支援 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。
[
[email protected]
~]# yum -y install openssl openssl-devel
  3. Nginx安裝 3.1 Nginx版本 下載網址: https://nginx.org/en/download.html 圖片.png 圖片.png 選擇最新的穩定版 nginx-1.12.2 版本說明: Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以說是開發版 Stable version:最新穩定版,生產環境上建議使用的版本 Legacy versions:遺留的老版本的穩定版 3.2 Nginx下載 使用wget命令下載
1
[[email protected] ~]# wget -c https://nginx.org/download/nginx-1.12.2.tar.gz

 

如沒有wget命令則安裝:
[[email protected] ~]# yum -y install wget

 

3.3 解壓
[[email protected] ~]# tar -zxvf nginx-1.12.2.tar.gz
  3.4.1 新建nginx使用者和組
[[email protected] include]# groupadd nginx 
[[email protected] include]# useradd -g nginx -d /home/nginx nginx 
[[email protected] include]# passwd nginx

 

3.4.2第三方模組安裝 本文以安裝第三方模組sticky為例,版本為1.,2.5,下載地址: https://pan.baidu.com/s/1Zpv6axGNUJkkGcam7EoLaQ 密碼:6jaq 上傳解壓: [[email protected] ~]# tar -zxvf nginx-goodies-nginx-sticky-module-ng-08a395c66e42..gz [[email protected] ~]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-1.2.5 3.4.3 安裝 [[email protected] ~]# cd nginx-1.12.2 [[email protected] nginx-1.12.2]# ./configure --add-module=/root/nginx-sticky-1.2.5 指定使用者、路徑和模組配置(可選): ./configure \ --user=nginx --group=nginx \ #安裝的使用者組 --prefix=/usr/local/nginx \ #指定安裝路徑 --with-http_stub_status_module \ #監控nginx狀態,需在nginx.conf配置 --with-http_ssl_module \ #支援HTTPS --with-http_sub_module \ #支援URL重定向 --with-http_gzip_static_module #靜態壓縮 --add-module=/root/nginx-sticky-1.2.5 #安裝sticky模組 3.5 編譯 [[email protected] nginx-1.12.2]# make &&。、 報錯: /root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c: 在函式‘ngx_http_sticky_misc_sha1’中: /root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:15: 錯誤:‘SHA_DIGEST_LENGTH’未宣告(在此函式內第一次使用) u_char hash[SHA_DIGEST_LENGTH]; ^ /root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:15: 附註:每個未宣告的識別符號在其出現的函式內只報告一次 /root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:10: 錯誤:未使用的變數‘hash’ [-Werror=unused-variable] u_char hash[SHA_DIGEST_LENGTH]; ^ /root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c: 在函式‘ngx_http_sticky_misc_hmac_sha1’中: /root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:242:15: 錯誤:‘SHA_DIGEST_LENGTH’未宣告(在此函式內第一次使用) u_char hash[SHA_DIGEST_LENGTH]; 圖片.png 圖片.png 解決方法: 修改ngx_http_sticky_misc.c檔案,新增#include <openssl/sha.h>和#include <openssl/md5.h>模組
[[email protected] nginx-1.12.2]# sed -i '12a #include <openssl/sha.h>' /root/nginx-sticky-1.2.5/ngx_http_sticky_misc.c 
[[email protected] nginx-1.12.2]# sed -i '12a #include <openssl/md5.h>' /root/nginx-sticky-1.2.5/ngx_http_sticky_misc.c

 

重新編譯:
[[email protected] nginx-1.12.2]# make && make install

 

3.6 nginx命令全域性執行設定
[[email protected] bin]# cd /usr/local/nginx/sbin/ 
[[email protected] sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
  4. Nginx相關命令 4.1 版本檢視
[[email protected] ~]# nginx -v nginx version: nginx/1.12.2

 

4.2 檢視載入的模組
[[email protected] ~]# nginx -V nginx version: nginx/1.12.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) configure arguments: --add-module=/root/nginx-sticky-1.2.5/

 

4.3 啟停命令 4.3.1 啟動
[[email protected] nginx-1.12.2]# nginx

 

4.3.2 停止
[[email protected] nginx-1.12.2]# nginx -s stop 
[[email protected] nginx-1.12.2]# nginx -s quit

 

4.3.3 動態載入
[[email protected] nginx-1.12.2]# ngins -s reload

 

4.3.4 測試配置檔案nginx.conf正確性 [[email protected] ~]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful nginx -s quit:此方式停止步驟是待nginx程序處理任務完畢進行停止。 nginx -s stop:此方式相當於先查出nginx程序id再使用kill命令強制殺掉程序。 nginx -s reload:動態載入,當配置檔案nginx.conf有變化時執行該命令動態載入。 4.4 開機自啟動 編輯/etc/rc.d/rc.local檔案,新增一行/usr/local/nginx/sbin/nginx
1 [[email protected] rc.d]# cd /etc/rc.d 
2 [[email protected] rc.d]# sed -i '13a /usr/local/nginx/sbin/nginx' /etc/rc.d/rc.local 
3 [[email protected] rc.d]# chmod u+x rc.local

 

  5. 更改預設埠 編輯配置檔案/usr/local/nginx/conf/nginx.conf,將預設埠80修改為81:
1 [[email protected] ~]# view /usr/local/nginx/conf/nginx.conf

 

圖片.png 圖片.png 載入配置:
1 [[email protected] ~]# nginx -s reload
  6. 訪問Nginx 6.1 關閉防火牆
1 [[email protected] ~]# firewall-cmd --state running 
2 [[email protected] ~]# systemctl stop firewalld.service 
3 [[email protected] ~]# firewall-cmd --state not running

 

6.2 訪問Nginx http://localhost:81 圖片.png 圖片.png 配置ssl證書之前,先準備SSL證書,至於獲取的途徑很多(阿里雲的服務,第三方服務購買)。這裡不詳細解釋。以下是我的SSL證書     準備好證書後,找到nginx的安裝目錄,我的安裝位置為:/usr/local/nginx     進入 config/nginx.conf 如果沒有裝winscp(一款視覺化檔案操作工具)的。可以通過命令列的方式,編輯nginx的config檔案。   開始配置檔案的修改 在修改配置檔案之前,最好做一個備份,防止修改錯誤,也能及時回退錯誤   1、找到第一個監聽80埠的server:一下是我修改好的server  
 1 server {
 2     listen 80;
 3     server_name 需要訪問的域名;
 4  
 5     rewrite ^(.*) https://$server_name$1 permanent; #這句是代表 把    http的域名請求轉成https
 6  
 7     #charset koi8-r;
 8     #access_log logs/host.access.log main;
 9     location / {
10         root html;
11         index index.html index.htm;
12        proxy_pass http://需要訪問的域名; #因為這裡還是80埠,所以保持http就可以了
13  
14     }
15 } 
  在實際的配置檔案中,最好把我上面的備註刪除   2、第一個server修改好了之後。那麼就需要開始配置第二個server。拉到檔案的底部。看到有一個https型別的server。而且已經全部被註釋封上了,這是我們需要改的第二個server,如圖:     這裡除了HTTPS server這行之外,其他的 # 刪除,啟動https模組  
 1 # HTTPS server
 2 #
 3 server {
 4     listen 443 ssl;
 5     server_name 需要訪問的域名,這裡也不用加https;
 6  
 7     ssl on;
 8  
 9     ssl_certificate /usr/local/nginx/ssl/ssl.pem; #這裡是ssl key檔案存放的絕對路徑,根據自己的檔名稱和路徑來寫
10     ssl_certificate_key /usr/local/nginx/ssl/ssl.key; #這裡是ssl key檔案存放的絕對路徑,根據自己的檔名稱和路徑來寫
11  
12  
13     ssl_session_cache shared:SSL:1m;
14     ssl_session_timeout 5m;
15  
16     ssl_ciphers HIGH:!aNULL:!MD5;
17     ssl_prefer_server_ciphers on;
18  
19     location / {
20         proxy_pass http://需要訪問的域名:8080/;
21     }
22 }

 

  配置好後,nginx的配置就算是完成了。不過這只是配置ssl證書的第一步!   接下來就是要讓配置檔案生效:   1、進去nginx的sbin資料夾,我的sbin資料夾在:/usr/local/nginx/sbin 執行以下語句:檢驗配置檔案是否有錯誤   ./nginx -t   如果nginx曾經安裝過SSL模組,那麼應該會顯示以下介面:(如果已經顯示配置成功,那麼可以跳過這一步,直接重啟nginx就可以了)   可是大多數第一次安裝https證書,都會報錯,說缺少SSL模組,如下:     2、這時候我們就可以先安裝SSL模組: 先確認2個位置: 1)我的nginx是安裝在了/usr/local/nginx/下 2)我的nginx的原始碼包放在了/usr/local/nginx/nginx/nginx-1.8.0下。如果沒有的話,重新下載你對應的nginx版本的原始碼包,找個目錄解壓   3、目錄切換到我們的原始碼包安裝位置:   cd /usr/local/nginx/nginx/nginx-1.8.0   4、執行語句,重新安裝ssl模組:   ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module   這時候可能又會有點小插曲,啟動ssl模組的時候,報錯了:   看到了error。就知道linux安裝失敗,停止了。這個錯誤是因為我們沒有安裝openssl openssl-devel(如果這一步沒有報錯的話,可以跳過下面的安裝openssl-devel的步驟)   5、那麼可以先執行安裝openssl openssl-devel語句:   yum -y install openssl openssl-devel   安裝上了 openssl-devel後,重新執行:./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module   6、這時候應該就執行配置成功了。配置成功後,那麼就需要編譯我們的配置。(注意這裡只能用make 而不要用make install,因為執行make install是覆蓋安裝的意思) 執行:   make   等待執行完成後,我們需要把新編譯的nginx模組替換原來的nginx。   7、還是老規矩,先備份舊的nginx,執行語句(這裡面複製的檔案的路徑需要根據你們安裝的目錄自行修改,如果和我安裝的路徑是一樣的那麼可以直接執行該語句):   cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak   8、關閉nginx(因為要把新的模組覆蓋舊的nginx)   先找到nginx埠號,如圖,目前我nginx的程序號為:13542   ps -ef|grep nginx     殺死該程序就可以了,執行語句:   kill -QUIT 13542   9、關閉nginx程序後就可以開始替換了(注意:我當前的位置是在我nginx的原始碼包中,目錄不要搞錯了)   執行:(複製到我的nginx的目錄中)   cp ./objs/nginx /usr/local/nginx/sbin/   然後就是啟動nginx。在啟動之前,也可以在測試一次配置檔案是否已經生效:   #先切換到sbin目錄 cd /usr/local/nginx/sbin/ #檢測nginx的配置檔案是否有錯誤 ./niginx -t   看到這樣的,就是已經成功了     最後啟動nginx:   /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf   因為剛才替換nginx模組的時候是把nginx程序都殺死了,所以要用上面的命令進行啟動,而不能使用reload重啟。   nginx正常啟動後,我們在訪問我們的網站,發現https就已經配置好了:     小拓展: 剛才配置nginx的時候是直接在配置檔案上做修改,其實我們可以把小的配置抽離出去,儘量保持原配置檔案不被修改   我們可以在原有配置檔案中,加上這句(注意作用域範圍,是引入到http的{}之內):   include vhost/*.conf;   接下來,需要找到對應的配置檔案,因為我們這裡的路徑是直接 vhost。所以在配置檔案同級目錄,新建一個 vhost 資料夾,而且我們引入的是 *.conf 。意思就是引入vhost中所有後綴是.conf的配置檔案:     在配置檔案中,加上我們剛才教程提到的配置:  
 1 server {
 2 listen 80;
 3 server_name 需要訪問的域名(不加http頭);
 4  
 5 rewrite ^(.*) https://$server_name$1 permanent;
 6  
 7 #charset koi8-r;
 8 #access_log logs/host.access.log main;
 9 location / {
10   root html;
11   index index.html index.htm;
12   proxy_pass http://需要訪問的域名
13  
14 }
15 }
16  
17 # HTTPS server
18 #
19 server {
20 listen 443 ssl;
21 server_name 需要訪問的域名;
22  
23 ssl on;
24  
25 ssl_certificate /usr/local/nginx/ssl/ssl.pem;
26 ssl_certificate_key /usr/local/nginx/ssl/ssl.key;
27  
28 ssl_session_cache shared:SSL:1m;
29 ssl_session_timeout 5m;
30  
31 ssl_ciphers HIGH:!aNULL:!MD5;
32 ssl_prefer_server_ciphers on;
33  
34 location / {
35 proxy_pass http://需要訪問的域名:8080/;
36 }
37 }
儲存後,也是使用nginx -t測試配置檔案是否成功,成功後重啟nginx即可。   如果有多個二級域名,那麼就複製多份 xx.conf檔案即可,配置檔案會自動引入到nginx的配置中,不過每次新增配置檔案都需要檢測一遍,然後重啟nginx。   附上nginx配置檔案
  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33 
 34     server {
 35         listen       80;
 36         server_name  test.feihe.com;
 37         #rewrite ^(.*) https://$server_name$1 permanent; #這句是代表 把http的域名請求轉成https
 38 
 39         #charset koi8-r;
 40 
 41         #access_log  logs/host.access.log  main;
 42 
 43         location / {
 44             root   html/;
 45             index  index.html index.htm;
 46         }
 47 
 48 
 49         location /coa/ {
 50             proxy_pass http://localhost:8080;
 51         }
 52 
 53         location /front/ {
 54             proxy_pass http://localhost:8080;
 55         }
 56 
 57         #error_page  404              /404.html;
 58 
 59         # redirect server error pages to the static page /50x.html
 60         #
 61         error_page   500 502 503 504  /50x.html;
 62         location = /50x.html {
 63             root   html;
 64         }
 65 
 66         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 67         #
 68         #location ~ \.php$ {
 69         #    proxy_pass   http://127.0.0.1;
 70         #}
 71 
 72         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 73         #
 74         #location ~ \.php$ {
 75         #    root           html;
 76         #    fastcgi_pass   127.0.0.1:9000;
 77         #    fastcgi_index  index.php;
 78         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 79         #    include        fastcgi_params;
 80         #}
 81 
 82         # deny access to .htaccess files, if Apache's document root
 83         # concurs with nginx's one
 84         #
 85         #location ~ /\.ht {
 86         #    deny  all;
 87         #}
 88     }
 89 
 90 
 91     # another virtual host using mix of IP-, name-, and port-based configuration
 92     #
 93     #server {
 94     #    listen       8000;
 95     #    listen       somename:8080;
 96     #    server_name  somename  alias  another.alias;
 97 
 98     #    location / {
 99     #        root   html;
100     #        index  index.html index.htm;
101     #    }
102     #}
103 
104 
105     # HTTPS server         ssl_protocols  SSLv2 SSLv3 TLSv1;
106     
107     server {
108         listen       443 ssl;
109         server_name  test.feihe.com;
110 
111         ssl                  on;
112         ssl_certificate      /usr/local/nginx/ssl/_.feihe.com_bundle.crt;  #這裡是ssl pem檔案存放的絕對路徑
113         ssl_certificate_key  /usr/local/nginx/ssl/_.feihe.com.key;  #這裡是ssl key檔案存放的絕對路徑
114 
115         ssl_session_cache    shared:SSL:1m;
116         ssl_session_timeout  5m;
117 
118 
119         ssl_ciphers  HIGH:!aNULL:!MD5;
120         ssl_prefer_server_ciphers   on;
121 
122         location / {
123             root   html/;
124             index  index.html index.htm;
125         }
126 
127 
128         location /coa/ {
129             proxy_pass http://localhost:8080;
130         }
131 
132         location /front/ {
133             proxy_pass http://localhost:8080;
134         }
135     }
136 
137 }