nginx是一个开源的、支持高性能、高并发的www服务和代理服务软件。nginx不但是一个优秀的web服务软件,还可以具有反向代理负载均衡和缓存服务功能,在反向代理负载均衡功能方面,它类似于大名鼎鼎的LVS负责均衡以及haproxy专业代理软件,但是nginx部署起来更为简单,方便;在缓存服务功能方面,它有类似于squid等专业的缓存服务软件。


反向代理或负责均衡服务。

在反向代理或负载均衡服务方面,nginx可以作为web服务、PHP等登台服务及memcached缓存的代理服务器,它具有类似专业反向代理软件(haproxy)的功能,同事也是一个优秀的邮件代理服务软件(最早开发这个产品的的目的之一也是作为邮件代理服务),但是nginx的代理功能还是相对简单了些,特别是不支持tcp的代理,nginx1.9.0已发布,已开始支持tcp的代理了,nginx的代理功能也在逐渐增强。


linux中软件安装方法:

1 rpm -ivh 包名.rpm

    有依赖问题,安装A,A需要先安装B,B先安装C等。

2 yum安装,yum包管理器,yum安装解决rpm安装的依赖问题,安装更简单化。

    优点:简单,易用,高效

    缺点:不能定制

    centos切换在线yum源地址为aliyun地址方法:

安装nginx服务实战

安装nginx服务实战

        复制对应版本的切换命令,在centos中输入切换命令即可:

[[email protected] tools]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

3 编译(c语言源码-编译二进制等)

    ./configure(配置过程),make(编译),make install(安装)

    优点:可以定制

    缺点:复杂,效率低。

4 定制制作rmp包,搭建yum仓库,把定位的rpm包放到yum仓库,进行yum安装

    优点:结合了2的优点和3的优点

    缺点:复杂。


开始安装nginx

编译安装(本文是nginx编译安装方法)

到目前为止,还未发现操作系统ISO镜像自带或默认情况yum可以直接通过rpm包方法安装nginx的情况。如果需要使用yum安装nginx,则需要配置epel yum源或者去官方寻找。接下来重点讲nginx的编译方式安装,如果是大规模的安装,可以先根据自身的业务需求定制好rpm包,然后放到yum仓库里通过yum来安装,有关rpm包定制及yum仓库搭建请看博客:http://user.qzone.qq.com/49000448/blog/1426987479


在web01服务器上面安装

1)安装nginx所需的pcre库

    pcre(perl兼容正则表达式),安装pcre库是为了使nginx具备URL重写功能的Rewrite模块,如果不安装pcre库,则nginx无法使用rewrite模块功能,nginx的rewrite模块功能几乎是企业应用必须。安装pcre库的过程如下:

    1.1    当前linux系统环境如下:

        

[[email protected] ~]# cat /etc/redhat-release 
CentOS release 6.8 (Final)
[[email protected] ~]# uname -r
2.6.32-642.el6.x86_64
[[email protected]b01 ~]# uname -m
x86_64

    1.2    操作命令如下:

        采用yum安装方式安装pcre(推荐方法),命令如下

[[email protected] ~]# yum install pcre pcre-devel -y
#如下是yum安装操作后检查安装结果
[[email protected] ~]# rpm -qa pcre pcre-devel
pcre-7.8-7.el6.x86_64
pcre-devel-7.8-7.el6.x86_64
提示:yum安装的pcre版本有些低,不过一般情况不影响使用。

    1.3    安装nginx

        nginx的英文官网网站是http://nginx.org ,从这里可以查看nginx的各个软件版本信息。nginx软件有三种版本:稳定版,开发版和历史稳定版本。开发版本更新较快,包含最新的功能和bug的修复,但同时也可能遇到新的bug,开发版的更新一旦稳定下来,就会被纳入稳定版中。但是,有些新功能不会被加到旧的稳定版本中去,稳定版本的更新较慢,但是软件bug也会较少,可以作为企业生产环境的首选,因此通畅建议选择使用稳定版。当然,在实际工作中,选择稳定版本时,也尽量别去用最新的稳定版,建议比已出来的最新版晚6-10个月的稳定版比较好,本文中选择稳定版本nginx-1.6.3 stable

        nginx的安装非常简答,具体操作过程如下:

    打开nginx官网:http://nginx.org 选择稳定版本1.6.6,右键属性复制下载链接:

安装nginx服务实战

安装nginx服务实战

 

        

[[email protected] tools]# cd /home/oldboy/tools  #切换到tools目录下
[[email protected] tools]# pwd
/home/oldboy/tools
如下是下载nginx1.6.3 wget -q就是不显示下载
[[email protected] tools]# wget -q 
然后ls查看下是否下载完成
[[email protected] tools]# ls
nginx-1.6.3.tar.gz
查看文件有多大?才787K
[[email protected] tools]# ls -hil
total 788K
261196 -rw-r--r-- 1 root root 787K Apr  8  2015 nginx-1.6.3.tar.gz
如下是解压,xf最简化的,以前使用zxvf
[[email protected] tools]# tar xf nginx-1.6.3.tar.gz

[[email protected] tools]# cd nginx-1.6.3
[[email protected] nginx-1.6.3]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
查看yum官网有没有提供nginx安装包:
[[email protected] nginx-1.6.3]# yum list|grep nginx
pcp-pmda-nginx.x86_64                      3.10.9-9.el6                  base  
如上查看没有nginx包

        查看配置过程

[[email protected] nginx-1.6.3]# ./configure --help

        如下修改配置过程(未涉及安装)

[[email protected] nginx-1.6.3]# ./configure --user=www --group=www --with-http_ssl_module --with-http_sub_modulehttp_sub_module --prefix=/application/nginx-1.6.3/

        上面修改配置会提示有个软件没有安装

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

        安装openssl软件:

[[email protected] nginx-1.6.3]# yum install openssl openssl-devel -y

        

        新建虚拟用户:

[[email protected] nginx-1.6.3]# useradd www -s /sbin/nologin -M

        继续运行配置文件:

[[email protected] nginx-1.6.3]# ./configure --user=www --group=www --with-http_ssl_module --with-http_sub_module --prefix=/application/nginx-1.6.3/

        查看配置是否成功,看上面运行配置文件的结果,如果一眼看不出来就echo $?返回值为0代表nginx配置成功了。

[[email protected] nginx-1.6.3]# echo $?
0

        然后执行make(编译nginx)

[[email protected] nginx-1.6.3]# make

        继续执行make install (安装nginx)

[[email protected] nginx-1.6.3]# make install

        创建软链接:

[[email protected] nginx-1.6.3]# ln -s /application/nginx-1.6.3/ /application/nginx

        这样工作中使用的就时nginx 访问到的是nginx-1.6.3,后期nginx-1.6.3升级,直接把软链接删除,重新再创建一个nginx软链接即可。

[[email protected] nginx-1.6.3]# ll /application/nginx
lrwxrwxrwx 1 root root 25 Aug 19 14:50 /application/nginx -> /application/nginx-1.6.3/
[[email protected] nginx-1.6.3]# ll /application/nginx
lrwxrwxrwx 1 root root 25 Aug 19 14:50 /application/nginx -> /application/nginx-1.6.3/
[[email protected] nginx-1.6.3]# ll /application/
total 4
lrwxrwxrwx 1 root root   25 Aug 19 14:50 nginx -> /application/nginx-1.6.3/
drwxr-xr-x 6 root root 4096 Aug 19 14:47 nginx-1.6.3


        安装完成后启动nginx,如果没有反应就是启动成功。

[[email protected] nginx-1.6.3]# /application/nginx/sbin/nginx

        查看www默认端口

[[email protected] nginx-1.6.3]# netstat -lntup|grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      5103/nginx          
[[email protected] nginx-1.6.3]# lsof -i :80
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   5103 root    6u  IPv4  18400      0t0  TCP *:http (LISTEN)
nginx   5104  www    6u  IPv4  18400      0t0  TCP *:http (LISTEN)

        web服务器地址:

[[email protected] nginx-1.6.3]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:79:73:42  
          inet addr:10.0.0.8  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe79:7342/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9081 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6119 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:9627625 (9.1 MiB)  TX bytes:563049 (549.8 KiB)

客户端输入地址10.0.0.8出现nginx欢迎界面表示nginx安装成功:

安装nginx服务实战



安装步骤:

安装nginx服务实战