First step
Run command to close Nginx first
sudo kill ' cat/usr/local/nginx/logs/nginx.pid '
Second Step
Vi/etc/init.d/nginx
Enter the following content
Copy Code code as follows:
#!/bin/sh
#
# Nginx-this script starts and stops the Nginx daemin
#
# Chkconfig:-85 15
# Description:nginx is a HTTP (s) server, HTTP (s) reverse \
# Proxy and IMAP/POP3 proxy server
# Processname:nginx
# config:/usr/local/nginx/conf/nginx.conf
# Pidfile:/usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source Networking configuration.
. /etc/sysconfig/network
# Check that networking are up.
["$NETWORKING" = "no"] && exit 0
nginx= "/usr/local/nginx/sbin/nginx"
prog=$ (basename $nginx)
Nginx_conf_file= "/usr/local/nginx/conf/nginx.conf"
Lockfile=/var/lock/subsys/nginx
Start () {
[x $nginx] | | Exit 5
[f $NGINX _conf_file] | | Exit 6
Echo-n $ "Starting $prog:"
Daemon $nginx-C $NGINX _conf_file
Retval=$?
Echo
[$retval-eq 0] && Touch $lockfile
Return $retval
}
Stop () {
Echo-n $ "Stopping $prog:"
Killproc $prog-quit
Retval=$?
Echo
[$retval-eq 0] && rm-f $lockfile
Return $retval
}
Restart () {
Configtest | | Return $?
Stop
Start
}
Reload () {
Configtest | | Return $?
Echo-n $ "Reloading $prog:"
Killproc $nginx-hup
Retval=$?
Echo
}
Force_reload () {
Restart
}
Configtest () {
$nginx-T-C $NGINX _conf_file
}
Rh_status () {
Status $prog
}
Rh_status_q () {
Rh_status >/dev/null 2>&1
}
Case "$" in
Start
Rh_status_q && Exit 0
$
Stop
Rh_status_q | | Exit 0
$
Restart|configtest)
$
Reload
Rh_status_q | | Exit 7
$
Force-reload)
Force_reload
Status
Rh_status
Condrestart|try-restart)
Rh_status_q | | Exit 0
*)
echo $ "Usage: $ {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
Exit 2
Esac
Save exit
Third Step
chmod +x/etc/init.d/nginx
Fourth Step
/sbin/chkconfig Nginx on
Check it out.
Sudo/sbin/chkconfig--list Nginx
Nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
Complete!
After that, you can use the following command
Copy Code code as follows:
Service Nginx Start
Service Nginx Stop
Service Nginx Restart
Service Nginx Reload
/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart
/etc/init.d/nginx Reload
The following articles are published by other authors
Copy Code code as follows:
#vi/etc/init.d/nginx
#! /bin/sh
### BEGIN INIT INFO
# PROVIDES:NGINX-PHP-FPM (fastcgi)
# Required-start: $all
# Required-stop: $all
# Default-start:3 5
# default-stop:0 1 6
# Short-description:start and stop nginx-fcgi in external FASTCGI mode
# Description:start and stop nginx-fcgi in external FASTCGI mode
# http://www.linxutone.org Msn:cnseek@msn.com
### End INIT INFO
Set-e
Path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
desc= "Nginx Daemon"
Name=nginx
daemon=/usr/local/nginx/sbin/$NAME
Configfile=/usr/local/nginx/conf/nginx.conf
pidfile=/var/run/$NAME. PID
Scriptname=/etc/init.d/$NAME
# gracefully Exit if the package has been removed.
Test-x $DAEMON | | Exit 0
D_start () {
/USR/LOCAL/PHP-FCGI/SBIN/PHP-FPM Start >/dev/null 2>&1
$DAEMON-C $CONFIGFILE | | Echo-n "Already Running"
}
D_stop () {
/USR/LOCAL/PHP-FCGI/SBIN/PHP-FPM Stop >/dev/null 2>&1
Kill-quit ' Cat $PIDFILE ' | | Echo-n "Not Running"
}
D_reload () {
/USR/LOCAL/PHP-FCGI/SBIN/PHP-FPM Reload >/dev/null 2>&1
Kill-hup ' Cat $PIDFILE ' | | Echo-n "can ' t reload"
}
Case "$" in
Start
Echo-n "Starting $DESC: $NAME"
D_start
echo "."
Stop
Echo-n "Stopping $DESC: $NAME"
D_stop
echo "."
Reload
Echo-n "Reloading $DESC configuration ..."
D_reload
echo "Reloaded."
Restart)
Echo-n "Restarting $DESC: $NAME"
D_stop
Sleep 1
D_start
echo "."
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
Exit 3
Esac
Exit 0
#chmod U+x/etc/init.d/nginx
How to use:
Copy Code code as follows:
#/etc/init.d/nginx start
#/etc/init.d/nginx stop
#/etc/init.d/nginx restart
Note Modify the installation path
Copy Code code as follows:
#!/bin/bash
#
# Init file for Nginx server daemon
#
# chkconfig:234 99 99
# Description:nginx Server Daemon
#
# source Function Library
. /etc/rc.d/init.d/functions
# Pull in Sysconfig settings
[-f/etc/sysconfig/nginx] &&. /etc/sysconfig/nginx
Retval=0
Prog= "Nginx"
Pat=/usr/local/nginx
Nginxd=/usr/local/nginx/sbin/nginx
Pid_file=/usr/local/nginx/nginx.pid
Start ()
{
Echo-n $ "Starting $prog:"
$NGINXD 2>/dev/null $OPTIONS && Success | | Failure
Retval=$?
["$RETVAL" = 0] && Touch/var/lock/subsys/nginx
Echo
}
Stop ()
{
Echo-n $ "Shutting down $prog:"
Killproc Nginx
Retval=$?
Echo
[$RETVAL-eq 0] && rm-f/var/lock/subsys/nginx
Return $RETVAL
}
Reload ()
{
Echo-n $ "Reloading Nginx:"
Killproc Nginx-hup
Retval=$?
Echo
Return $RETVAL
}
Case "$" in
Start
Start
Stop
Stop
Restart)
Stop
Start
Reload
Reload
Status
Status-p $PID _file Nginx
Retval=$?
*)
echo $ "Usage: $ {Start|stop|restart|reload|status}"
Retval=1
Esac
Exit $RETVAL