Nginx startup scripts and Chkconfig management in CentOS systems

Source: Internet
Author: User
Tags centos

After installing the Nginx, the reboot requires "Kill-hup Nginx process number" to reload, which is obviously inconvenient. It would be much easier to manage it directly through scripting, like Apache.

Nginx official had already thought well, also provided this script, address: Http://wiki.nginx.org/RedHatNginxInitScript. Here are the admin scripts here:

The code is as follows Copy Code

#!/bin/sh
#
# Nginx-this script starts and stops the Nginx daemon
#
# Chkconfig:-85 15
# Description:nginx is a HTTP (s) server, HTTP (s) reverse\
# Proxy and IMAP/POP3 proxy server
# Processname:nginx
# config:/etc/nginx/nginx.conf
# config:/etc/sysconfig/nginx
# Pidfile:/var/run/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/sbin/nginx"
prog=$ (basename $nginx)

Nginx_conf_file= "/etc/nginx/nginx.conf"

[-f/etc/sysconfig/nginx] &&. /etc/sysconfig/nginx

Lockfile=/var/lock/subsys/nginx

Make_dirs () {
# Make required Directories
User= ' $nginx-v 2>&1 | grep "Configure arguments:" | Sed ' s/[^*]*--user=\ ([^]*\). */\1/g '-'
If [-Z "' grep $user/etc/passwd ']"; Then
Useradd-m-s/bin/nologin $user
Fi
Options= ' $nginx-v 2>&1 | grep ' Configure arguments: '
for opt in $options; Todo
If [' Echo $opt | grep '. *-temp-path ']; Then
Value= ' echo $opt | Cut-d "=" F 2 '
if [!-D "$value"]; Then
# echo "Creating" $value
Mkdir-p $value && chown-r $user $value
Fi
Fi
Done
}

Start () {
[x $nginx] | | Exit 5
[f $NGINX _conf_file] | | Exit 6
Make_dirs
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
Sleep 1
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 the above script to the/etc/init.d/nginx file and modify two places:

The nginx= "/usr/sbin/nginx" is modified to the path of the Nginx execution program.
Nginx_conf_file= "/etc/nginx/nginx.conf" is modified to the path of the configuration file.
Once saved, the Nginx service can be managed through this script:

The code is as follows Copy Code
$/etc/init.d/nginx Start
$/etc/init.d/nginx Stop
$/etc/init.d/nginx Reload

...
using Chkconfig for Management

The above method completes the script manages the Nginx service the function, but still is not very convenient, for instance wants to set up nginx to boot and so on. You can use Chkconfig to set this up.

First add the Nginx service to the Chkconfig management list:

The code is as follows Copy Code
Chkconfig--add/etc/init.d/nginx

After adding this, you can use the service to start the Nginx, restart, and so on.

The code is as follows Copy Code
$ service Nginx Start
$ Service Nginx Stop
$ service Nginx Reload

...
Set Terminal mode startup:

The code is as follows Copy Code

$ chkconfig--level 3 Nginx on

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.