首先编辑一个以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… “

  1. if netstat -tnpl | grep -q nginx;then<br /> echo "$NAME (pid `pidof $NAME`) already running."<br /> exit 1<br /> fi
  2. $NGINX_BIN -c $CONFIGFILE
  3. if [ "$?" != 0 ] ; then<br /> echo " failed"<br /> exit 1<br /> else<br /> echo " done"<br /> fi<br /> ;;
  4. stop)<br /> echo -n "Stoping $NAME... "
  5. if ! netstat -tnpl | grep -q nginx; then<br /> echo "$NAME is not running."<br /> exit 1<br /> fi
  6. $NGINX_BIN -s stop
  7. if [ "$?" != 0 ] ; then<br /> echo " failed. Use force-quit"<br /> exit 1<br /> else<br /> echo " done"<br /> fi<br /> ;;
  8. 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 /> ;;
  9. force-quit)<br /> echo -n "Terminating $NAME... "
  10. if ! netstat -tnpl | grep -q nginx; then<br /> echo "$NAME is not running."<br /> exit 1<br /> fi
  11. kill `pidof $NAME`
  12. if [ "$?" != 0 ] ; then<br /> echo " failed"<br /> exit 1<br /> else<br /> echo " done"<br /> fi<br /> ;;
  13. restart)<br /> $0 stop<br /> sleep 1<br /> $0 start<br /> ;;
  14. reload)<br /> echo -n "Reload service $NAME... "
  15. 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 /> ;;
  16. configtest)<br /> echo -n "Test $NAME configure files... "
  17. $NGINX_BIN -t<br /> ;;
  18. *)<br /> echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"<br /> exit 1<br /> ;;

esac

blob.png

需要注意上图所标部分,是需要根据自己安装的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