1. Nginx簡(jiǎn)介
Nginx (發(fā)音為[engine x])專(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 代理服。
Linux系統(tǒng):CentOS 7.3
2. 安裝準(zhǔn)備
2.1 gcc安裝
安裝 nginx 需要先將官網(wǎng)下載的源碼進(jìn)行編譯,編譯依賴 gcc 環(huán)境,如果沒(méi)有 gcc 環(huán)境,則需要安裝:
[root@nginx ~]# yum -y install gcc-c++
2.2 pcre安裝
PCRE(Perl Compatible Regular Expressions) 是一個(gè)Perl庫(kù),包括 perl 兼容的正則表達(dá)式庫(kù)。nginx 的 http 模塊使用 pcre 來(lái)解析正則表達(dá)式,所以需要在 linux 上安裝 pcre 庫(kù),pcre-devel 是使用 pcre 開(kāi)發(fā)的一個(gè)二次開(kāi)發(fā)庫(kù)。nginx也需要此庫(kù)。
[root@nginx ~]# yum -y install pcre pcre-devel
2.3 zlib安裝
zlib 庫(kù)提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對(duì) http 包的內(nèi)容進(jìn)行 gzip ,所以需要在 Centos 上安裝 zlib 庫(kù)。
[root@nginx ~]# yum -y install zlib zlib-devel
2.4 OpenSSL安裝
OpenSSL 是一個(gè)強(qiáng)大的安全套接字層密碼庫(kù),囊括主要的密碼算法、常用的密鑰和證書(shū)封裝管理功能及 SSL 協(xié)議,并提供豐富的應(yīng)用程序供測(cè)試或其它目的使用。
nginx 不僅支持 http 協(xié)議,還支持 https(即在ssl協(xié)議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫(kù)。
[root@nginx ~]# yum -y install openssl openssl-devel
3. Nginx安裝
3.1 Nginx版本
下載網(wǎng)址:https://nginx.org/en/download.html
選擇最新的穩(wěn)定版nginx-1.12.2
版本說(shuō)明:
Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以說(shuō)是開(kāi)發(fā)版
Stable version:最新穩(wěn)定版,生產(chǎn)環(huán)境上建議使用的版本
Legacy versions:遺留的老版本的穩(wěn)定版
3.2 Nginx下載
使用wget命令下載
[root@nginx ~]# wget -c https://nginx.org/download/nginx-1.12.2.tar.gz
如沒(méi)有wget命令則安裝:
[root@nginx ~]# yum -y install wget
3.3 解壓
[root@nginx ~]# tar -zxvf nginx-1.12.2.tar.gz
3.4 安裝配置
3.4.1 新建nginx用戶和組
[root@nginx include]# groupadd nginx
[root@nginx include]# useradd -g nginx -d /home/nginx nginx
[root@nginx include]# passwd nginx
3.4.2第三方模塊安裝
本文以安裝第三方模塊sticky為例,版本為1.,2.5,下載地址:
可以到Linux公社資源站下載:
——————————————分割線——————————————
免費(fèi)下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2018年資料/9月/27日/CentOS 7.3編譯安裝Nginx 1.12.2/
下載方法見(jiàn) http://www.linuxidc.com/Linux/2013-07/87684.htm
——————————————分割線——————————————
上傳解壓:
[root@nginx ~]# tar -zxvf nginx-goodies-nginx-sticky-module-ng-08a395c66e42..gz
[root@nginx ~]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-1.2.5
3.4.3 安裝
[root@nginx ~]# cd nginx-1.12.2
[root@nginx 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 #監(jiān)控nginx狀態(tài),需在nginx.conf配置
–with-http_ssl_module #支持HTTPS
–with-http_sub_module #支持URL重定向
–with-http_gzip_static_module #靜態(tài)壓縮
–add-module=/root/nginx-sticky-1.2.5 #安裝sticky模塊
3.5 編譯
[root@nginx nginx-1.12.2]# make && make install
報(bào)錯(cuò):
/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c: 在函數(shù)‘ngx_http_sticky_misc_sha1’中:
/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:15: 錯(cuò)誤:‘SHA_DIGEST_LENGTH’未聲明(在此函數(shù)內(nèi)第一次使用)
u_char hash[SHA_DIGEST_LENGTH];
^
/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:15: 附注:每個(gè)未聲明的標(biāo)識(shí)符在其出現(xiàn)的函數(shù)內(nèi)只報(bào)告一次
/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:10: 錯(cuò)誤:未使用的變量‘hash’ [-Werror=unused-variable]
u_char hash[SHA_DIGEST_LENGTH];
^
/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c: 在函數(shù)‘ngx_http_sticky_misc_hmac_sha1’中:
/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:242:15: 錯(cuò)誤:‘SHA_DIGEST_LENGTH’未聲明(在此函數(shù)內(nèi)第一次使用)
u_char hash[SHA_DIGEST_LENGTH];
解決方法:
修改ngx_http_sticky_misc.c文件,新增#include <openssl/sha.h>和#include <openssl/md5.h>模塊
[root@nginx nginx-1.12.2]# sed -i ’12a #include <openssl/sha.h>’ /root/nginx-sticky-1.2.5/ngx_http_sticky_misc.c
[root@nginx nginx-1.12.2]# sed -i ’12a #include <openssl/md5.h>’ /root/nginx-sticky-1.2.5/ngx_http_sticky_misc.c
重新編譯:
[root@nginx nginx-1.12.2]# make && make install
3.6 nginx命令全局執(zhí)行設(shè)置
[root@nginx bin]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
4. Nginx相關(guān)命令
4.1 版本查看
[root@nginx ~]# nginx -v
nginx version: nginx/1.12.2
4.2 查看加載的模塊
[root@nginx ~]# 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 啟動(dòng)
[root@nginx nginx-1.12.2]# nginx
4.3.2 停止
[root@nginx nginx-1.12.2]# nginx -s stop
[root@nginx nginx-1.12.2]# nginx -s quit
4.3.3 動(dòng)態(tài)加載
[root@nginx nginx-1.12.2]# ngins -s reload
4.3.4 測(cè)試配置文件nginx.conf正確性
[root@nginx ~]# 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進(jìn)程處理任務(wù)完畢進(jìn)行停止。
nginx -s stop:此方式相當(dāng)于先查出nginx進(jìn)程id再使用kill命令強(qiáng)制殺掉進(jìn)程。
nginx -s reload:動(dòng)態(tài)加載,當(dāng)配置文件nginx.conf有變化時(shí)執(zhí)行該命令動(dòng)態(tài)加載。
4.4 開(kāi)機(jī)自啟動(dòng)
編輯/etc/rc.d/rc.local文件,新增一行/usr/local/nginx/sbin/nginx
[root@nginx rc.d]# cd /etc/rc.d
[root@nginx rc.d]# sed -i ’13a /usr/local/nginx/sbin/nginx’ /etc/rc.d/rc.local
[root@nginx rc.d]# chmod u+x rc.local
5. 更改默認(rèn)端口
編輯配置文件/usr/local/nginx/conf/nginx.conf,將默認(rèn)端口80修改為81:
[root@nginx ~]# view /usr/local/nginx/conf/nginx.conf
加載配置:
[root@nginx ~]# nginx -s reload
6. 訪問(wèn)Nginx
6.1 關(guān)閉防火墻
[root@nginx ~]# firewall-cmd –state
running
[root@nginx ~]# systemctl stop firewalld.service
[root@nginx ~]# firewall-cmd –state
not running
6.2 訪問(wèn)Nginx
http://localhost:81