Total Pageviews

Tuesday, 6 December 2011

how to make an nginx init script under debian or centos vps after u compile nginx?

If you have used other options or have placed the nginx binary in a directory other than /usr/local/sbin/ then you may need to adjust the script shown below.

Stop

If you have nginx running then stop the process using:
sudo kill `cat /usr/local/nginx/logs/nginx.pid`

Init script

The script shown below is from an Ubuntu Feisty 'aptitude' install and has been adapted to take into account our custom install of nginx.
Let's go ahead and create the script:
sudo nano /etc/init.d/nginx
Inside the blank file place the following:

#! /bin/sh

### 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

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
        . /etc/default/nginx
fi

set -e

case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
                --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
                --exec $DAEMON
        echo "$NAME."
        ;;
  restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
                /usr/local/nginx/logs/$NAME.pid --exec $DAEMON
        sleep 1
        start-stop-daemon --start --quiet --pidfile \
                /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
          --exec $DAEMON
      echo "$NAME."
      ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0
 
You can also download the script from here (as it's a shell script, you may get a warning about it being a 'BIN' file, just save as normal and open in any text editor).
There's not really the space to go into the workings of the script but suffice to say, it defines where the main nginx binary is located so nginx can be started correctly.
It also defines where to find the nginx.pid file so the process and be correctly stopped and restarted.

Execute

As the init file is a shell script, it needs to have executable permissions.
We set them like so:
sudo chmod +x /etc/init.d/nginx
 
excerpt from http://articles.slicehost.com/2007/10/19/ubuntu-feisty-adding-an-nginx-init-script
以上为debian/ubuntu下的 nginx init script。
-----------------------------------------------------------------------------------

CentOS - Adding an Nginx Init Script

If you decided to install Nginx via source (see the previous article) you would have the latest and greatest version.
However, one disadvantage of installing from source is that init scripts are not created. No problem, let's go ahead and create one for easy control of Nginx and to ensure it restarts on a reboot.
I am assuming you have followed the previous article and installed Nginx from source.
If you have used other options or have placed the Nginx binary in a directory other than /usr/local/sbin/ then you will need to adjust the script shown below to match your installation.

Stop

If you have Nginx running then stop the process using:
sudo kill `cat /usr/local/nginx/logs/nginx.pid`

Init script

The script I use below is from a CentOS 'yum install' and has been adapted to take into account our custom install of Nginx.
Let's go ahead and create the script:
sudo nano /etc/init.d/nginx
Inside the blank file place the following:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an 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 is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/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 "$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

There's not really the space to go into the workings of the script but suffice to say, it defines where the main Nginx binary and pid files are located so Nginx can be started correctly.

Execute

As the init file is a shell script, it needs to have executable permissions.
We set them like so:
sudo chmod +x /etc/init.d/nginx

Chkconfig

Now we have the base script prepared, we need to add it to the default run levels:
sudo /sbin/chkconfig nginx on Let's check our work to confirm:
sudo /sbin/chkconfig --list nginx nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off Done.
The script will now be called on a reboot so Nginx will automatically start.

Start, Stop and Restart

Now we can start, stop, restart, and reload Nginx using these commands:
sudo /etc/init.d/nginx start ... sudo /etc/init.d/nginx stop ... sudo /etc/init.d/nginx restart ... sudo /etc/init.d/nginx reload You can also check the current status as well as the configuration syntax:
sudo /etc/init.d/nginx status ... sudo /etc/init.d/nginx configtest

以上为centos vps下的nginx init script。
 

No comments:

Post a Comment