一、安装开发组件
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 #variables NGINX_BIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/etc/nginx/nginx.conf" NGINX_PID="/var/run/nginx/nginx.pid" NETSTAT="/bin/netstat" alter=$1 prog=nginx #load system function . /etc/rc.d/init.d/functions #function:echo ok or error function if_no { if [ $2 == 0 ]; then echo -n $"$1 ${prog}:" && success && echo else echo -n $"$1 ${prog}:" && failure && echo fi } #start nginx function start { if [ -s ${NGINX_PID} ]; then echo "nginx already running" else if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then rm -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 $? fi fi } #stp nginx function stop { if [ -s ${NGINX_PID} ]; then cat ${NGINX_PID} | xargs kill -QUIT if_no stop $? else if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then rm -f ${NGINX_PID} 2>/dev/null if_no stop 0 else rm -f ${NGINX_PID} 2>/dev/null kill `${NETSTAT} -tnpl | grep nginx | awk ‘{ print $7}‘ | cut -d ‘/‘ -f 1` if_no stop $? fi fi } function restart { if [ -s ${NGINX_PID} ]; then cat ${NGINX_PID} | xargs kill -HUP if_no restart $? else stop sleep 1 start fi } 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 in start) 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.vim
au BufRead,BufNewFile /etc/nginx/* if &ft ==‘‘ | setfiletype nginx | endif
八、配置nginx.conf
rm -rf /etc/nginx/nginx.conf
vi /etc/nginx/nginx.conf
user 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; } } }
访问正常
本文出自 “晓洋” 博客,请务必保留此出处http://princepar.blog.51cto.com/1448665/1636026
原文:http://princepar.blog.51cto.com/1448665/1636026