天天看點

建構Nginx伺服器

步驟一:建構Nginx伺服器

1)使用源碼包安裝nginx軟體包

  1. [[email protected] ~]# yum -y install gcc pcre-devel openssl-devel        //安裝依賴包
  2. [[email protected] ~]# useradd -s /sbin/nologin nginx
  3. [[email protected] ~]# tar -xf nginx-1.10.3.tar.gz
  4. [[email protected] ~]# cd nginx-1.10.3
  5. [[email protected] nginx-1.10.3]# ./configure \
  6. > --prefix=/usr/local/nginx \                //指定安裝路徑
  7. > --user=nginx \                            //指定使用者
  8. > --group=nginx \                            //指定組
  9. > --with-http_ssl_module                        //開啟SSL加密功能
  10. [r[email protected] nginx-1.10.3]# make && make install    //編譯并安裝

2)nginx指令的用法

  1. [root@proxy ~]# /usr/local/nginx/sbin/nginx                    //啟動服務
  2. [root@proxy ~]# /usr/local/nginx/sbin/nginx -s stop            //關閉服務
  3. [[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload        //重新加載配置檔案
  4. [[email protected] ~]# /usr/local/nginx/sbin/nginx -V                //檢視軟體資訊
  5. [[email protected] ~]# ln -s /usr/local/nginx/sbin/nginx /sbin/        //友善後期使用

netstat指令可以檢視系統中啟動的端口資訊,該指令常用選項如下:

-a顯示所有端口的資訊

-n以數字格式顯示端口号

-t顯示TCP連接配接的端口

-u顯示UDP連接配接的端口

-l顯示服務正在監聽的端口資訊,如httpd啟動後,會一直監聽80端口

-p顯示監聽端口的服務名稱是什麼(也就是程式名稱)

nginx服務預設通過TCP 80端口監聽用戶端請求:

  1. [email protected] ~]# netstat -anptu | grep nginx
  2. tcp        0        0 0.0.0.0:80        0.0.0.0:*        LISTEN        10441/nginx

3)設定防火牆與SELinux(非必須的操作,如果有則關閉)

  1. [[email protected] ~]# systemctl stop firewalld
  2. [[email protected] ~]# setenforce 0

4)測試首頁檔案

Nginx Web服務預設首頁文檔存儲目錄為/usr/local/nginx/html/,在此目錄下預設有一個名為index.html的檔案,使用用戶端通路測試頁面:

  1. [[email protected] ~]# curl http://192.168.4.5
  2. <html>
  3. <head>
  4. <title>Welcome to nginx!</title>
  5. </head>
  6. <body bgcolor="white" text="black">
  7. <center><h1>Welcome to nginx!</h1></center>
  8. </body>
  9. </html>

步驟二:更新Nginx伺服器

1)編譯新版本nginx軟體

  1. [[email protected] ~]# tar -zxvf nginx-1.12.2.tar.gz
  2. [[email protected] ~]# cd nginx-1.12.2
  3. [[email protected] nginx-1.12.2]# ./configure \
  4. > --prefix=/usr/local/nginx \
  5. > --user=nginx \
  6. > --group=nginx \
  7. > --with-http_ssl_module
  8. [[email protected] nginx-1.12.2]# make            

2) 備份老的nginx主程式,并使用編譯好的新版本nginx替換老版本

  1. [[email protected] nginx-1.12.2]# mv /usr/local/nginx/sbin/nginx \
  2. >/usr/local/nginx/sbin/nginxold
  3. [[email protected] nginx-1.12.2]# cp objs/nginx /usr/local/nginx/sbin/     //拷貝新版本
  4. [[email protected] nginx-1.12.2]# make upgrade                            //更新
  5. #或者使用killall nginx殺死程序後再啟動nginx。
  6. /usr/local/nginx/sbin/nginx -t
  7. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  8. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  9. kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
  10. sleep 1
  11. test -f /usr/local/nginx/logs/nginx.pid.oldbin
  12. kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
  13. [roo[email protected] ~]# /usr/local/nginx/sbin/nginx –v                //檢視版本

步驟三:用戶端通路測試

1)分别使用浏覽器和指令行工具curl測試伺服器頁面

如果使用firefox火狐浏覽器,注意在ssh遠端的時候一定要加-X選項。

  1. [[email protected] ~]# firefox http://192.168.4.5
  2. [[email protected] ~]# curl http://192.168.4.5

繼續閱讀