- ! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run ‘update-rc.d -f nginx defaults’, or use the appropriate command on your
# distro. For CentOS/Redhat run: ‘chkconfig —add nginx’ - http://lnmp.org">Author: licess
# website: http://lnmp.org
首先编辑一个以nginx命名的文件
vi nginx
! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run ‘update-rc.d -f nginx defaults’, or use the appropriate command on your
# distro. For CentOS/Redhat run: ‘chkconfig —add nginx’
BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
Author: licess
# website: http://lnmp.org
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/apps/nginx/sbin/$NAME
CONFIGFILE=/apps/nginx/conf/$NAME.conf
PIDFILE=/apps/nginx/logs/$NAME.pid
case “$1” in
start)
echo -n “Starting $NAME… “
if netstat -tnpl | grep -q nginx;then<br /> echo "$NAME (pid `pidof $NAME`) already running."<br /> exit 1<br /> fi
$NGINX_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then<br /> echo " failed"<br /> exit 1<br /> else<br /> echo " done"<br /> fi<br /> ;;
stop)<br /> echo -n "Stoping $NAME... "
if ! netstat -tnpl | grep -q nginx; then<br /> echo "$NAME is not running."<br /> exit 1<br /> fi
$NGINX_BIN -s stop
if [ "$?" != 0 ] ; then<br /> echo " failed. Use force-quit"<br /> exit 1<br /> else<br /> echo " done"<br /> fi<br /> ;;
status)<br /> if netstat -tnpl | grep -q nginx; then<br /> PID=`pidof nginx`<br /> echo "$NAME (pid $PID) is running..."<br /> else<br /> echo "$NAME is stopped"<br /> exit 0<br /> fi<br /> ;;
force-quit)<br /> echo -n "Terminating $NAME... "
if ! netstat -tnpl | grep -q nginx; then<br /> echo "$NAME is not running."<br /> exit 1<br /> fi
kill `pidof $NAME`
if [ "$?" != 0 ] ; then<br /> echo " failed"<br /> exit 1<br /> else<br /> echo " done"<br /> fi<br /> ;;
restart)<br /> $0 stop<br /> sleep 1<br /> $0 start<br /> ;;
reload)<br /> echo -n "Reload service $NAME... "
if netstat -tnpl | grep -q nginx; then<br /> $NGINX_BIN -s reload<br /> echo " done"<br /> else<br /> echo "$NAME is not running, can't reload."<br /> exit 1<br /> fi<br /> ;;
configtest)<br /> echo -n "Test $NAME configure files... "
$NGINX_BIN -t<br /> ;;
*)<br /> echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"<br /> exit 1<br /> ;;
esac
需要注意上图所标部分,是需要根据自己安装的Nginx路径进行修改的
文件编写完毕后给与一个执行权限
chmod +x nginx
然后将文件移至启动目录下
mv nginx /etc/init.d/
添加开机自启
chkconfig —add nginx
chkconfig nginx on
然后就可以使用
启动:service nginx start
停止:service nginx stop
重启:service nginx restart
重新加载配置文件:service nginx reload