本文地址http://comexchan.cnblogs.com/ ,作者Comex Chan,尊重知识产权,转载请注明出处,谢谢!
完成了Nginx的编译安装后,仅仅是能支持Nginx最基本的功能,为了在生产环境应用,还需要进行一些额外的设置。
设置系统服务
每次进入nginx的home目录进行nginx的开启、关闭、重载非常不方便,所以,我们需要注册系统服务,来避免此类问题。
进入nginx目录,创建系统服务文件:
cd /comexHome/nginx
vim nginx4comex
然后输入如下内容后wq退出
#!/bin/sh # Name:nginx4comex # nginx - this script starts and stops the nginx daemon # # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /comexHome/nginx/conf/nginx.conf # pidfile: /comexHome/nginx/nginx.pid # # Created By http://comexchan.cnblogs.com/ # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 NGINX_DAEMON_PATH="/comexHome/nginx/sbin/nginx" NGINX_CONF_FILE="/comexHome/nginx/conf/nginx.conf" NGINX_LOCK_FILE="/var/lock/subsys/nginx4comex" prog=$(basename $NGINX_DAEMON_PATH) start() { [ -x $NGINX_DAEMON_PATH ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $NGINX_DAEMON_PATH -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $NGINX_LOCK_FILE return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $NGINX_LOCK_FILE return $retval } restart() { configtest || return $? stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $NGINX_DAEMON_PATH -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $NGINX_DAEMON_PATH -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
授予执行权限:
chmod u+x /comexHome/nginx/nginx4comex
移动到系统服务目录
cp /comexHome/nginx/nginx4comex /etc/init.d
测试系统服务:
# 查看nginx状态
service nginx4comex status
# 停止nginx服务
service nginx4comex stop
# 启动nginx服务
service nginx4comex start
# 重启nginx服务(停机)
service nginx4comex restart
# 重载Nginx服务(不停机)
service nginx4comex reload
# 测试配置文件是否合法
service nginx4comex configtest
# 强制重新加载新配置(停机)
service nginx4comex force-reload
设置开启自启
生产环境的OS经常打补丁或者由于其他原因导致OS重启,为了减少人工介入,我们需要设置开机自启,在生产的OS重启时多点串行重启即可保证对业务基本无影响。
设置开机自启:
chkconfig nginx4comex on
检查开机启动设置结果
chkconfig --list nginx4comex
![image image](https://images2015.cnblogs.com/blog/897329/201605/897329-20160515192015586-1427946656.png)
=====================================================
本文地址http://comexchan.cnblogs.com/ ,作者Comex Chan,尊重知识产权,转载请注明出处,谢谢!