Nginx 安装

1、Nginx WEB入门简介
Nginx [engine x]是Igor Sysoev编写的一个HTTP和反向代理服务器,另外它也可以作为邮件代理服务器。 它已经在众多流量很大的俄罗斯网站上使用了很长时间,这些网站包括Yandex、Mail.Ru、VKontakte,以及Rambler。
据Netcraft统计,在2012年8月份,世界上最繁忙的网站中有11.48%使用Nginx作为其服务器或者代理服务器。目前互联网主流公司360、百度、新浪、腾讯、阿里等都在使用nginx作为自己的web服务器。
Nginx由内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是Nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
Nginx相对于Apache优点:
1)高并发响应性能非常好,官方Nginx处理静态文件并发5w/s
2)反向代理性能非常好。(可用于负载均衡)
3)内存和cpu占用率低。(为Apache的1/5-1/10)
4)功能较Apache少(常用功能均有)
5)对php可使用cgi方式和fastcgi方式。

2、 Nginx WEB安装配置
安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

首先需要安装pcre库
然后再安装Nginx,安装pcre支持rewrite库
#源码安装pcre
2.1、下载 PCRE 安装包,下载地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
cd /usr/src/
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
2.2、解压安装包:
tar zxvf pcre-8.35.tar.gz
2.3、进入安装包目录
cd pcre-8.35
2.4、编译安装
./configure
make && make install
2.5、查看pcre版本
pcre-config --version

3、安装 Nginx
#下载Nginx源码包
cd /usr/src ;
wget -c http://nginx.org/download/nginx-1.4.2.tar.gz
#解压Nginx源码包
tar -xzf nginx-1.4.2.tar.gz
#进入解压目录,然后sed修改Nginx版本信息为WS
cd nginx-1.4.2 ; sed -i -e ‘s/1.4.2//g’ -e ‘s/nginx/WS/g’ -e ‘s/“NGINX”/“WS”/g’ src/core/nginx.h
#预编译Nginx
useradd nginx;
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
#.configure预编译成功后,执行make命令进行编译
make
#make执行成功后,执行make install 正式安装
make install
#自此Nginx安装完毕
/usr/local/nginx/sbin/nginx -t 检查nginx配置文件是否正确,返回OK即正确。
[root@localhost ~]# /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
然后启动nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx 回车即可。查看进程是否已启动:
[root@localhost ~]# ps -ef |grep nginx
nobody 5381 30285 0 May16 ? 00:04:31 nginx: worker process
root 30285 1 0 2014 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 32260 32220 0 12:34 pts/0 00:00:00 grep nginx
[root@localhost ~]#

4、Nginx 虚拟主机配置
在真实的服务器环境,为了充分利用服务器资源,一台nginx web服务器同时会配置N个虚拟域名主机,即多个域名对于同样一个80端口。然后服务器IP数量很多,也可以配置基于多个IP对应同一个端口。
vi修改nginx.conf server段配置内容如下:
#virtual hosts config 2014/5/18
server {
listen 80;
server_name www.a.com;
#access_log logs/host.access.log main;
location / {
root html/a;
index index.html index.htm;
}
server {
listen 80;
server_name www.b.com
#access_log logs/host.access.log main;
location / {
root html/b;
index index.html index.htm;
}
创建两个不同的目录mkdir –p /usr/local/nginx/html/{a,b},然后分别在两个目录创建两个不同的index.html网站页面即可。通过客户端配置hosts指向两个域名,然后在IE浏览器访问测试效果。