一、安装开发组件
yum -y groupinstall "Development Tools" "Server Platform Development" --省事安装 yum -y install pcre-devel --不安装此包,安装过程中会报错缺少pcre二、添加运行nginx程序的用户及组 groupadd -r nginx useradd -r -g nginx nginx三、编译和安装nginx ./configure \--prefix=/usr/local/nginx \ --指定安装位置
--conf-path=/etc/nginx/nginx.conf \ --配置文件位置
--error-log-path=/var/log/nginx/error.log \ --错误日志存放位置
--http-log-path=/var/log/nginx/access.log \ --http访问日志位置
--pid-path=/var/run/nginx/nginx.pid \ --nginx的pid文件存放位置
--lock-path=/var/lock/nginx.lock \ --文件锁定,防止误操作
--user=nginx \ --nginx运行的非特权用户
--group=nginx \ --nginx运行的非特权用户组
--with-http_ssl_module \ --支持https
--with-http_stub_status_module \ --支持nginx状态查询
--http-client-body-temp-path=/var/tmp/nginx/client/ \ --http客户端请求临时文件路径
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http代理临时文件路径
--with-pcre --支持rewrite重写
make && make install四、创建nginx所需的临时文件目录mkdir /var/tmp/nginx
五、nginx启动关闭命令/usr/local/nginx/sbin/nginx -t 测试正常
/usr/local/nginx/sbin/nginx 启动nginx killall nginx 关闭nginx六、创建nginx自启动 vi /etc/rc.d/init.d/nginx#!/bin/bash# nginx This shell script takes care of starting and stopping# nginx## chkconfig: - 13 68# description: nginx is a web server### BEGIN INIT INFO# Provides: $named# Short-Description: start|stop|status|restart|configtest### END INIT INFO#variablesNGINX_BIN="/usr/local/nginx/sbin/nginx"NGINX_CONF="/etc/nginx/nginx.conf"NGINX_PID="/var/run/nginx/nginx.pid"NETSTAT="/bin/netstat"alter=$1prog=nginx#load system function. /etc/rc.d/init.d/functions#function:echo ok or errorfunction if_no {if [ $2 == 0 ]; thenecho -n $"$1 ${prog}:" && success && echoelseecho -n $"$1 ${prog}:" && failure && echofi}#start nginxfunction start {if [ -s ${NGINX_PID} ]; thenecho "nginx already running"elseif [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; thenrm -f ${NGINX_PID} 2>/dev/null${NGINX_BIN} -c ${NGINX_CONF}if_no start $? else${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1 > ${NGINX_PID}if_no start $?fifi}#stp nginxfunction stop {if [ -s ${NGINX_PID} ]; thencat ${NGINX_PID} | xargs kill -QUITif_no stop $?else if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; thenrm -f ${NGINX_PID} 2>/dev/nullif_no stop 0elserm -f ${NGINX_PID} 2>/dev/nullkill `${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1`if_no stop $?fifi}function restart {if [ -s ${NGINX_PID} ]; thencat ${NGINX_PID} | xargs kill -HUPif_no restart $?elsestopsleep 1startfi}function status {${NETSTAT} -tnpl | grep nginx | grep LISTEN[ $? == 0 ] && echo "nginx is running" || echo "nginx is not running"}function configtest {${NGINX_BIN} -t}case $alter instart)start;;stop)stop;;restart)restart;;status)status;;configtest)configtest;;*)echo "use:${NGINX} {start|stop|restart|status|configtest}";;esac
chkconfig --add nginx
chkconfig nginx on
七、配置nginx文件支持vim编辑
mkdir .vim/syntax -pv 拷贝nginx.vim文件到.vim/syntax/目录中 vi .vim/filetype.vimau BufRead,BufNewFile /etc/nginx/* if &ft =='' | setfiletype nginx | endif
八、配置nginx.conf
rm -rf /etc/nginx/nginx.conf vi /etc/nginx/nginx.confuser nginx;worker_processes 10;worker_rlimit_nofile 10000;error_log /var/log/nginx/error.log;pid /var/run/nginx.pid;events { worker_connections 4096;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; index index.html index.htm index.jsp; root /usr/local/tomcat7/webapps/ROOT; location / { index index.jsp; proxy_pass } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
访问正常