安装前提
1、安装必备软件
yum -y install zlib zlib-devel openssl openssl-devel
2、安装pcre 支持nginx伪静态(若已安装直接跳过)
cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
mkdir /usr/local/pcre // 创建安装目录
tar zxvf pcre-8.30.tar.gz
cd pcre-8.30
./configure --prefix=/usr/local/pcre // 配置
make & make install
开始安装Nginx
1、#添加www组和用户,设置不允许www用户直接登录系统
[root@admin local]# groupadd www
[root@admin local]# useradd -g www www -s /bin/false
2、下载源码
[root@admin local]# cd /usr/local/src
[root@admin local]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
[root@admin local]# tar -zxvf nginx-1.6.2.tar.gz
[root@admin local]# cd nginx-1.6.2/
3、创建安装路径
[root@admin local]# mkdir /usr/local/nginx
4、安装
[root@admin nginx]# ./configure --prefix=/usr/local/nginx
[root@admin nginx]# make & make install
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="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/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 {
rm -f ${NGINX_PID} 2>/dev/null
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
配置Nginx自启动脚本
1、将启动脚本保存至/etc/init.d/nginx 文件中
2、设置脚本启动权限
[root@admin nginx]# chmod +x /etc/init.d/nginx
3、执行nginx命令
/etc/init.d/nginx start 或 service nginx start //启动
/etc/init.d/nginx stop 或 service nginx stop //关闭nginx
/etc/init.d/nginx restart 或 service nginx restart //重启nginx
配置开机自动启动
chkconfig --add nginx
chkconfig --level 2345 nginx on