1、到nginx官网下载源码包,我使用的是:
2、安装GCC
命令:yum -y install gcc gcc-c++ autoconf automake
3、安装相应模块
命令:yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
zlib:Nginx提供GZIP模块,需要GZIP库支持
openssl:ngnix提供SSL功能
pcre:支持地址重写rewrite功能
4、解压nginx(我的nignix存放在/data文件夹中)
命令:
cd /data
tar -zvxf nginx-1.9.5.tar.gz
5、创建用户和用户组
命令:
groupadd -r nginx
useradd -s /sbin/nologin -g nginx -r nginx
查看创建的用户
id nginx
6、配置nginx模块、编译源码与安装
命令:
可以查看nginx配置文件的帮助
进入nginx解压目录
cd /data/nginx-1.9.5
./configure --help
配置
配置文件配置
./configure \
--prefix=/usr/local/ngnix \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nignx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-file-aio \
--with-ipv6 \
--with-pcre
配置成功:
编译安装
命令:
make && make install
7、运行nginx
命令
/usr/sbin/nginx -c /etc/nginx/nginx.conf
若(出现错误)
是由于文件夹/var/cache/nginx/client_temp不存在
创建文件夹即可
命令:
mkdir /var/cache/nginx/client_temp -pv
重新启动nginx
/usr/sbin/nginx -c /etc/nginx/nginx.conf
查看nginx是否启动
命令:
ps -aux |grep nginx
运行成功
停止ngnix
命令:
/usr/sbin/nginx -s stop 或 使用kill命令
reload,当你修改配置时,用此命令不用再重启就生效了
/usr/sbin/nginx -s reload
检查配置文件语法
/usr/sbin/nginx -t -c /etc/ngnix/nginx.conf
配置系统服务
添加文件
vi /etc/init.d/nginx
<pre name="code" class="html">#!/bin/sh
#
# nginx Startup script for nginx
#
# chkconfig: - 85 15
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# description: nginx is an HTTP and reverse proxy server
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop nginx
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
if [ -L $0 ]; then
initscript=`/bin/readlink -f $0`
else
initscript=$0
fi
sysconfig=`/bin/basename $initscript`
if [ -f /etc/sysconfig/$sysconfig ]; then
. /etc/sysconfig/$sysconfig
fi
nginx=${NGINX-/usr/sbin/nginx}
prog=`/bin/basename $nginx`
conffile=${CONFFILE-/etc/nginx/nginx.conf}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
pidfile=${PIDFILE-/var/run/nignx/nginx.pid}
SLEEPMSEC=${SLEEPMSEC-200000}
UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS-5}
RETVAL=0
start() {
echo -n $"Starting $prog: "
daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} ${prog}
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
killproc -p ${pidfile} ${prog} -HUP
RETVAL=$?
echo
}
upgrade() {
oldbinpidfile=${pidfile}.oldbin
configtest -q || return
echo -n $"Starting new master $prog: "
killproc -p ${pidfile} ${prog} -USR2
echo
for i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do
/bin/usleep $SLEEPMSEC
if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
echo -n $"Graceful shutdown of old $prog: "
killproc -p ${oldbinpidfile} ${prog} -QUIT
RETVAL=$?
echo
return
fi
done
echo $"Upgrade failed!"
RETVAL=1
}
configtest() {
if [ "$#" -ne 0 ] ; then
case "$1" in
-q)
FLAG=$1
;;
*)
;;
esac
shift
fi
${nginx} -t -c ${conffile} $FLAG
RETVAL=$?
return $RETVAL
}
rh_status() {
status -p ${pidfile} ${nginx}
}
# See how we were called.
case "$1" in
start)
rh_status >/dev/null 2>&1 && exit 0
start
;;
stop)
stop
;;
status)
rh_status
RETVAL=$?
;;
restart)
configtest -q || exit $RETVAL
stop
start
;;
upgrade)
rh_status >/dev/null 2>&1 || exit 0
upgrade
;;
condrestart|try-restart)
if rh_status >/dev/null 2>&1; then
stop
start
fi
;;
force-reload|reload)
reload
;;
configtest)
configtest
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"
RETVAL=2
esac
exit $RETVAL
修改文件权限
命令:
chmod a+x /etc/init.d/ngnix
在运行命令
chkconfig --add nginx
运行命令查看
chkconfig --list nginx
配置成功
重新启动服务
service nginx restart
若出现错误,需要仔细检查/etc/init.d/nginx 文件是否错误
重启完成
停止服务
service nginx stop