1 nginx基础知识

1.1 nginx简介

Nginx("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。目前官方 Nginx 并不支持 Windows,只能在Linux、UNIX、BSD 系统下安装和使用。Nginx 本身只是一个 HTTP 和反向代理服务器,它无法像 Apache 一样通过安装各种模块来支持不同的页面脚本,例如 PHP、CGI 等。

 

1.2 nginx功能

Nginx 支持简单的负载均衡和容错;支持作为基本 HTTP 服务器的功能,例如日志、压缩、Byte ranges、Chunked responses、SSL、虚拟主机等等,应有尽有。

 

2 nginx部署方法

2.1 准备介质

Nginx部署之前,首先根据项目的需要选择需要安装的组件,实际环境一般会考虑需要支持gzip压缩和rewrite模块。所以安装的第一步是下载Nginx及Nginx的相关组件。

1) Nginx本身
  下载地址:http://nginx.org/en/download.html


2) gzip压缩依赖库:zlib
  下载地址: http://www.zlib.net


3) Rewrite模块的正则表达式依赖库:pcre (简称:Perl兼容正则表达式)
  下载地址:http://www.pcre.org 
 

2.2 安装步骤

实际条件:RedHat 6.5 64bit

系统自带组件:

zlib为:1.2.3版本

[root@localhost ~]# rpm -qa zlib

wKioL1TE9KnDHkD4AABCX73Q62s610.jpg


pcre为:7.8版本

[root@localhost ~]# rpm -qa pcre

wKiom1TE883yNEy9AAA7YnJUHns243.jpg 


上传nginx-0.8.55.tar.gz至文件夹/usr/local/src,并解压安装:

[root@localhost ~]# cd /usr/local/src

[root@localhost src]# ls

[root@localhost src]# tar zxvf nginx-0.8.55.tar.gz

[root@localhost src]# cd nginx-0.8.55

[root@localhost nginx-0.8.55]# ./configure --prefix=/usr/local/nginx

报错:(猜测是因为pcre-7.8版本过高,nginx-0.8.55版本太低)wKiom1TE883jLlD_AAEqhg-b5cQ501.jpg


尝试安装高版本的nginx-1.6.2:

[root@localhost nginx-0.8.55]# cd ..

[root@localhost src]# ls

[root@localhost src]# tar zxvf nginx-1.6.2.tar.gz

[root@localhost src]# cd nginx-1.6.2

[root@localhost nginx-1.6.2]# ./configure--prefix=/usr/local/nginx

依旧会出现该问题:

wKioL1TE9KnjFT57AAEwSfQM1Mc251.jpg


上网查资料,解决方法:

安装pcre-devel(对照操作系统版本去rpmfind.net下载或从光盘中获得

pcre-devel-7.8-6.el6.x86_64.rpm,并上传至/usr/local/src)

[root@localhost src]# ls

[root@localhost src]# rpm -ivh pcre-devel-7.8-6.el6.x86_64.rpm

[root@localhost src]# cd nginx-0.8.55

[root@localhost nginx-0.8.55]# ./configure--prefix=/usr/local/nginx

wKiom1TE882CtUi4AAMU3XUlUjs157.jpg

[root@localhost src]# make
[root@localhost nginx-0.8.55]# make install


2.3 管理nginx

检查nginx配置文件:

[root@localhostnginx-0.8.55]# /usr/local/nginx/sbin/nginx –t

wKioL1TE9KmiPL9BAAC2FcLJYFA977.jpg


nginx启动及状态查看:

[root@localhostnginx-0.8.55]# /usr/local/nginx/sbin/nginx

[root@localhostnginx-0.8.55]# netstat -tunlp |grep nginx

wKiom1TE883wMBWMAAB3FcnV2Xg374.jpg


查看nginx主进程号:

[root@localhostnginx-0.8.55]# ps -ef | grep "nginx: master process" | grep -v"grep" | awk -F ' ' '{print $2}'

wKioL1TE9KnQmksWAABwnqeNWXI068.jpg

 

停止nginx:

[root@localhost nginx-0.8.55]# kill -TERM 26614 (主进程号) 或

[root@localhost nginx-0.8.55]# /usr/local/nginx/sbin/nginx -sstop


重启nginx:

[root@localhost nginx-0.8.55]# /usr/local/nginx/sbin/nginx -s reload

 

配置开机自动启动nginx:

[root@localhost nginx-0.8.55]# vi /etc/rc.local

wKiom1TE89CCd0ewAAC_uUeW6D0817.jpg

 

查看nginx 手动安装的模块:

[root@localhost nginx-0.8.55]# /usr/local/nginx/sbin/nginx –V 

wKioL1TE9WLT2EhaAAC_uUeW6D0747.jpg


3 nginx升级步骤

查看当前使用版本并记录之前编译的参数:

[root@localhost src]# /usr/local/nginx/sbin/nginx –V

wKiom1TE9IayVempAAC4gp5prpc395.jpg

 

按照之前编译的参数进行解压、编译,不用安装(make install):

[root@localhost src]# ls

[root@localhost src]# tar zxvf nginx-1.6.2.tar.gz

[root@localhost src]# cd nginx-1.6.2

[root@localhost nginx-1.6.2]# ./configure--prefix=/usr/local/nginx

wKioL1TE9WLS_UIuAAMauOyKF3w232.jpg

[root@localhost nginx-1.6.2]# make

 

替换nginx文件:

[root@localhost nginx-1.6.2]# mv /usr/local/nginx/sbin/nginx/usr/local/nginx/sbin/nginx.bak20141231

[root@localhost nginx-1.6.2]# find / -name nginx

wKiom1TE9IbA2oR4AAByxW6wjlw349.jpg

[root@localhost nginx-1.6.2]# cp objs/nginx/usr/local/nginx/sbin/nginx

 

测试新文件是否生效:

[root@localhost nginx-1.6.2]# /usr/local/nginx/sbin/nginx –t

wKioL1TE9WLgmibdAADKEpA4MFg214.jpg

 

查看最新版本:

[root@localhost nginx-1.6.2]# /usr/local/nginx/sbin/nginx –v

wKiom1TE9IbBwbteAABZYVYr9lE082.jpg