Linux中Nginx安装与配置详解(CentOS-6.5:nginx-1.5.0)

来源:互联网 发布:车牌识别算法 编辑:程序博客网 时间:2024/05/17 04:28

1 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发布。

2 Nginx下载
1)pcre
http://www.pcre.org/
2)nginx
http://nginx.org/

3 Nginx安装
3.1 安装前的准备
1)准备 pcre-8.12.tar.gz。该文件为正则表达式库。让nginx支持rewrite需要安装这个库。
2) 准备 nginx-1.5.0.tar.gz。该文件为nginx的linux版本安装文件。
3)确保进行了安装了linux常用必备支持库。

Linux中必备常用支持库的安装(CentOS-6.5)

在CentOS安装软件的时候,可能缺少一部分支持库,而报错。这里首先安装系统常用的支持库。那么在安装的时候就会减少很多的错误的出现。

# yum install -y gcc gdb strace gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs patch e2fsprogs-devel krb5-devel libidn libidn-devel openldap-devel nss_ldap openldap-clients openldap-servers libevent-devel libevent uuid-devel uuid mysql-devel

3.2 正则表达式库安装
1)确保进行了安装了linux常用必备支持库。检查是否安装了g++、gcc。rpm -qa | grep gcc 之后需要出现3个包如下图所示。如果没有出现。需要安装g++、gcc。

# yum install gcc-c++


2) 上传pcre-8.12.tar.gz, nginx-1.5.0.tar.gz 到 /usr/local/src/nginx目录下。

3)解压pcre-8.12.tar.gz
# cd /usr/local/src/nginx
# tar zxvf pcre-8.12.tar.gz

4)进入解压后的目录
# cd pcre-8.12 

5)配置
# ./configure
6) 编译
# make
7) 安装
# make install

3.3 Nginx安装
0) 创建用户nginx使用的www用户。
# groupadd www #添加www组 
# useradd -g www www -s /bin/false #创建nginx运行账户www并加入到www组,不允许www用户直接登录系统

创建安装目录与日志目录
a) 安装目录
# mkdir /usr/local/nginx
b) 日志目录
# mkdir /data0/logs/nginx
# chown www:www /data0/logs/nginx -R

1) 判断系统是否安装了zlib-devel。如果没有安装。使用
# yum install -y zlib-devel



2) 解压
# cd /usr/local/src/nginx
# tar zxvf nginx-1.5.0.tar.gz

3) 进入目录
# cd nginx-1.5.0

4) 配置。通常将软件安装在/usr/local/目录下。
# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
5)编译
# make

6) 安装
# make install

7) 检查是否安装成功
# cd /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

3.4 配置防火墙80端口
#修改防火墙配置: 
# vi + /etc/sysconfig/iptables
#添加配置项 
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
#重启防火墙 
# service iptables restart

3.5 上传配置文件
1)上传nginx.conf (文件下载地址见上面的Linux公社资源站)
# cd /usr/local/nginx/conf
# rz nginx.conf
2) 上传fastcgi_params.phis
# rz fastcgi_params.phis

3.6 启动停止重启与测试
1)启动
#方法1
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#方法2
# cd /usr/local/nginx/sbin
# ./nginx


2) 停止
#查询nginx主进程号 
ps -ef | grep nginx
#停止进程 
kill -QUIT 主进程号 
#快速停止 
kill -TERM 主进程号 
#强制停止 
pkill -9 nginx

3) 重启(首次启动需:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf)
/usr/local/nginx/sbin/nginx -s reload

4)测试
#测试端口 
netstat -na | grep 80
#浏览器中测试 
http://ip:80


#user  nobody;  worker_processes  1;    #error_log  logs/error.log;  #error_log  logs/error.log  notice;  #error_log  logs/error.log  info;    #pid        logs/nginx.pid;    events {      worker_connections  1024;  }    http {      include      mime.types;      default_type  application/octet-stream;        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '      #                  '$status $body_bytes_sent "$http_referer" '      #                  '"$http_user_agent" "$http_x_forwarded_for"';        #access_log  logs/access.log  main;        sendfile        on;      #tcp_nopush    on;        #keepalive_timeout  0;      keepalive_timeout  65;        #gzip  on;        #kafka监控    server {          listen      9921;    #外网端口        server_name  kafkaOffsetServer;  #服务名          #charset koi8-r;            #access_log  logs/host.access.log  main;            #location / {            #root  html;              #index  index.html index.htm;          #}          location / {              proxy_pass http://10.2.20.49:8089;  #端口对应集群服务页面地址        }            #error_page  404              /404.html;            # redirect server error pages to the static page /50x.html          #          error_page  500 502 503 504  /50x.html;          location = /50x.html {              root  html;          }      }        #hdfs服务      server {          listen      9922;          server_name  hadoopHdfsServer;            #charset koi8-r;            #access_log  logs/host.access.log  main;            #location / {            #root  html;              #index  index.html index.htm;          #}          location / {              proxy_pass http://10.2.20.40:50070;          }            #error_page  404              /404.html;            # redirect server error pages to the static page /50x.html          #          error_page  500 502 503 504  /50x.html;          location = /50x.html {              root  html;          }      }          #yarn服务    server {          listen      9923;    #外网端口        server_name  yarnServer;  #服务名          #charset koi8-r;            #access_log  logs/host.access.log  main;            #location / {            #root  html;              #index  index.html index.htm;          #}          location / {              proxy_pass http://10.2.20.39:8088;  #端口对应集群服务页面地址        }            #error_page  404              /404.html;            # redirect server error pages to the static page /50x.html          #          error_page  500 502 503 504  /50x.html;          location = /50x.html {              root  html;          }      }        #jobHistory服务    server {          listen      9924;    #外网端口        server_name  jobHistoryServer;  #服务名          #charset koi8-r;            #access_log  logs/host.access.log  main;            #location / {            #root  html;              #index  index.html index.htm;          #}          location / {              proxy_pass http://10.2.20.41:19888;  #端口对应集群服务页面地址        }            #error_page  404              /404.html;            # redirect server error pages to the static page /50x.html          #          error_page  500 502 503 504  /50x.html;          location = /50x.html {              root  html;          }      }        #hue服务    server {          listen      9925;    #外网端口        server_name  hueServer;  #服务名          #charset koi8-r;            #access_log  logs/host.access.log  main;            #location / {            #root  html;              #index  index.html index.htm;          #}          location / {              proxy_pass http://10.2.20.44:8888;  #端口对应集群服务页面地址        }            #error_page  404              /404.html;            # redirect server error pages to the static page /50x.html          #          error_page  500 502 503 504  /50x.html;          location = /50x.html {              root  html;          }      }        #oozie服务    server {          listen      9926;    #外网端口        server_name  oozieServer;  #服务名          #charset koi8-r;            #access_log  logs/host.access.log  main;            #location / {            #root  html;              #index  index.html index.htm;          #}          location / {              proxy_pass http://10.2.20.69:11000;  #端口对应集群服务页面地址        }            #error_page  404              /404.html;            # redirect server error pages to the static page /50x.html          #          error_page  500 502 503 504  /50x.html;          location = /50x.html {              root  html;          }      }}