步驟一:建構Nginx伺服器
1)使用源碼包安裝nginx軟體包
- [[email protected] ~]# yum -y install gcc pcre-devel openssl-devel //安裝依賴包
- [[email protected] ~]# useradd -s /sbin/nologin nginx
- [[email protected] ~]# tar -xf nginx-1.10.3.tar.gz
- [[email protected] ~]# cd nginx-1.10.3
- [[email protected] nginx-1.10.3]# ./configure \
- > --prefix=/usr/local/nginx \ //指定安裝路徑
- > --user=nginx \ //指定使用者
- > --group=nginx \ //指定組
- > --with-http_ssl_module //開啟SSL加密功能
- [r[email protected] nginx-1.10.3]# make && make install //編譯并安裝
2)nginx指令的用法
- [root@proxy ~]# /usr/local/nginx/sbin/nginx //啟動服務
- [root@proxy ~]# /usr/local/nginx/sbin/nginx -s stop //關閉服務
- [[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload //重新加載配置檔案
- [[email protected] ~]# /usr/local/nginx/sbin/nginx -V //檢視軟體資訊
- [[email protected] ~]# ln -s /usr/local/nginx/sbin/nginx /sbin/ //友善後期使用
netstat指令可以檢視系統中啟動的端口資訊,該指令常用選項如下:
-a顯示所有端口的資訊
-n以數字格式顯示端口号
-t顯示TCP連接配接的端口
-u顯示UDP連接配接的端口
-l顯示服務正在監聽的端口資訊,如httpd啟動後,會一直監聽80端口
-p顯示監聽端口的服務名稱是什麼(也就是程式名稱)
nginx服務預設通過TCP 80端口監聽用戶端請求:
- [email protected] ~]# netstat -anptu | grep nginx
- tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10441/nginx
3)設定防火牆與SELinux(非必須的操作,如果有則關閉)
- [[email protected] ~]# systemctl stop firewalld
- [[email protected] ~]# setenforce 0
4)測試首頁檔案
Nginx Web服務預設首頁文檔存儲目錄為/usr/local/nginx/html/,在此目錄下預設有一個名為index.html的檔案,使用用戶端通路測試頁面:
- [[email protected] ~]# curl http://192.168.4.5
- <html>
- <head>
- <title>Welcome to nginx!</title>
- </head>
- <body bgcolor="white" text="black">
- <center><h1>Welcome to nginx!</h1></center>
- </body>
- </html>
步驟二:更新Nginx伺服器
1)編譯新版本nginx軟體
- [[email protected] ~]# tar -zxvf nginx-1.12.2.tar.gz
- [[email protected] ~]# cd nginx-1.12.2
- [[email protected] nginx-1.12.2]# ./configure \
- > --prefix=/usr/local/nginx \
- > --user=nginx \
- > --group=nginx \
- > --with-http_ssl_module
- [[email protected] nginx-1.12.2]# make
2) 備份老的nginx主程式,并使用編譯好的新版本nginx替換老版本
- [[email protected] nginx-1.12.2]# mv /usr/local/nginx/sbin/nginx \
- >/usr/local/nginx/sbin/nginxold
- [[email protected] nginx-1.12.2]# cp objs/nginx /usr/local/nginx/sbin/ //拷貝新版本
- [[email protected] nginx-1.12.2]# make upgrade //更新
- #或者使用killall nginx殺死程序後再啟動nginx。
- /usr/local/nginx/sbin/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
- kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
- sleep 1
- test -f /usr/local/nginx/logs/nginx.pid.oldbin
- kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
- [roo[email protected] ~]# /usr/local/nginx/sbin/nginx –v //檢視版本
步驟三:用戶端通路測試
1)分别使用浏覽器和指令行工具curl測試伺服器頁面
如果使用firefox火狐浏覽器,注意在ssh遠端的時候一定要加-X選項。
- [[email protected] ~]# firefox http://192.168.4.5
- [[email protected] ~]# curl http://192.168.4.5