一、Nginx介绍

   Nginx ("engine x") 是一个高性能的HTTP反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

Nginx是一款轻量级Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东新浪网易腾讯淘宝等。


二、Nginx的安装方式的对比:

1rpm -ivh 包名.rpm

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

缺点:不能定制。

2yum安装解决rpm安装的依赖问题,安装更简单。

优点:简单、易用、高效

缺点:不能定制。

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

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

优点:可以定制。

缺点:复杂、效低。

4、定制化制作rpm包,搭建yum仓库,把我定制的rpm包放到yum仓库,进行yum安装。

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

缺点:复杂。


三、编译方式安装Nginx

3.1安装之前确认一下相关依赖包

openssl openssl-devel pcre pcre-devel

[root@web02 nginx-1.6.3]# rpm -qa openssl openssl-devel pcre pcre-devel 

pcre-7.8-7.el6.x86_64

pcre-devel-7.8-7.el6.x86_64

openssl-devel-1.0.1e-48.el6_8.3.x86_64

openssl-1.0.1e-48.el6_8.3.x86_64

有以上结果返回代表已经安装了相关依赖包,如果没有安装就使用以下命令进行安装。

3.2安装依赖包

[root@web02 yum.repos.d]# yum install pcre pcre-devel -y

[root@web02 yum.repos.d]# yum install openssl openssl-devel -y

[root@web02 nginx-1.6.3]# rpm -qa openssl openssl-devel pcre pcre-devel #<==检查一下。

3.3创建用户及相关目录

[root@web02 ~]# mkdir /home/oldboy/tools/ -p   #<====用于存放软件的目录

[root@web02 ~]# useradd -s /sbin/nologin/ -M www  #<=====创建给nginx服务用的虚拟用户

[root@web02 ~]# cd /home/oldboy/tools/

[root@web02 tools]# wget -q http://nginx.org/download/nginx-1.6.3.tar.gz

[root@web02 tools]# ls

nginx-1.6.3.tar.gz

[root@web02 tools]# tar xf nginx-1.6.3.tar.gz

[root@web02 tools]# ls

nginx-1.6.3  nginx-1.6.3.tar.gz

[root@web02 tools]# cd nginx-1.6.3

[root@web02 nginx-1.6.3]# ./configure --user=www --group=www --prefix=/application/nginx-1.6.3/ --with-http_ssl_module --with-http_stub_status_module

checking for OS

 + Linux 2.6.32-573.el6.x86_64 x86_64

checking for C compiler ... found

 + using GNU C compiler

 + gcc version: 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) 

checking for gcc -pipe switch ... found

checking for gcc builtin atomic operations ... found

............                       #<===C源文件检查编译,太多文件这里省略一部分

creating objs/Makefile


Configuration summary

  + using system PCRE library

  + using system OpenSSL library

  + md5: using OpenSSL library

  + sha1: using OpenSSL library

  + using system zlib library


  nginx path prefix: "/application/nginx-1.6.3/"

  nginx binary file: "/application/nginx-1.6.3//sbin/nginx"

  nginx configuration prefix: "/application/nginx-1.6.3//conf"

  nginx configuration file: "/application/nginx-1.6.3//conf/nginx.conf"

  nginx pid file: "/application/nginx-1.6.3//logs/nginx.pid"

  nginx error log file: "/application/nginx-1.6.3//logs/error.log"

  nginx http access log file: "/application/nginx-1.6.3//logs/access.log"

  nginx http client request body temporary files: "client_body_temp"

  nginx http proxy temporary files: "proxy_temp"

  nginx http fastcgi temporary files: "fastcgi_temp"

  nginx http uwsgi temporary files: "uwsgi_temp"

  nginx http scgi temporary files: "scgi_temp"

###出现了标×××的部分提示,没有相关报错,证明编译成功。

[root@web02 nginx-1.6.3]# make

[root@web02 nginx-1.6.3]# make install

[root@web02 nginx-1.6.3]# ls      #<===编译前的文件情况      

auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src

[root@web02 nginx-1.6.3]# ls

conf  html  logs  sbin             #<===编译后的文件情况   

[root@web02 nginx-1.6.3]# ln -s /application/nginx-1.6.3/ /application/nginx   #创建软链接

[root@web02 nginx-1.6.3]# ls -l /application/nginx/

总用量 16

drwxr-xr-x 2 root root 4096 12月 11 10:44 conf

drwxr-xr-x 2 root root 4096 12月 11 10:44 html

drwxr-xr-x 2 root root 4096 12月 11 10:44 logs

drwxr-xr-x 2 root root 4096 12月 11 10:44 sbin

到此软件编译安装完成。

四、启动Nginx服务

[root@web02 nginx-1.6.3]# /application/nginx/sbin/nginx

[root@web02 nginx-1.6.3]# lsof -i :80

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

nginx   11067 root    6u  IPv4  22775      0t0  TCP *:http (LISTEN)

nginx   11068  www    6u  IPv4  22775      0t0  TCP *:http (LISTEN)


经验小结:

ln -s /application/nginx-1.6.3/ /application/nginx

软连接妙用:这条命令可是生产环境的经验。

1)将nginx安装的路径通过软链接的方式更改为/application/nginx/ 方便人员使用。

2)安装时指定版本号路径是为了便于查看区分当前使用的Nginx版本,也方便以后升级。

3)内部人员使用路径/application/nginx/

4)当nginx软件升级编译成带新版本号和版本后,删除原来的软链接,再重新建立尊重的到/application/nginx/的软链接就好。即版本升级,重新创建软链接的路径不变还是/application/nginx/

5)程序中如果有引用nginx路径的地方,不需要做任何更改,因为升级后访问路径还是/application/nginx/