Nginx and PHP-FPM Startup/restart Scripts

Source: Internet
Author: User
Nginx and PHP-FPM Startup/restart Scripts

Both Nginx and PHP on the server are compiled and installed by source code, and do not support the same nginx (Start|restart|stop|reload) as before. Get your hands on your own. The following script should be used in Rhel, Fedora,centos.

First, Nginx startup script/etc/init.d/nginx

#!/bin/bash
#
# Startup script for Nginx-this script starts and stops the Nginxdaemon
#
# Chkconfig: -8515
# 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 are up.
["$NETWORKING" = "no"] && exit0

nginx= "/usr/local/nginx/sbin/nginx"
prog=$ (basename $nginx)

Nginx_conf_file= "/usr/local/nginx/conf/nginx.conf"

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

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
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/null2>&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: $0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
Exit 2
Esac

#==============================================================

Save after editing, execute the following command

sudo chmod+x/etc/Init.d/Nginxsudo /Sbin/Chkconfig Nginx on# Check   it out.sudo /Sbin/Chkconfig--listNginxnginx0: Off1: Off2: On3: On4: On5: On6: Off

Complete! You can use the following command to manage Nginx.

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

Second, php-fpm startup script/etc/init.d/php-fpm

#!/bin/bash
#
# Startup script for the PHP-FPM server.
#
# chkconfig:345 85 15
# description:php is an html-embedded scripting language
# PROCESSNAME:PHP-FPM
# config:/usr/local/php/etc/php.ini

# Source function library.
. /etc/rc.d/init.d/functions

Php_path=/usr/local
desc= "PHP-FPM Daemon"
name=php-fpm
# PHP-FPM Path
daemon= $PHP _path/php/sbin/$NAME
# Configuration file path
configfile= $PHP _path/php/etc/php-fpm.conf
# PID file path (in php-fpm.conf settings)
pidfile= $PHP _path/php/var/run/$NAME. pid
Scriptname=/etc/init.d/$NAME

# Gracefully Exit If the package has been removed.
Test-x $DAEMON | | Exit 0

Rh_start () {
$DAEMON-y $CONFIGFILE | | Echo-n "Already Running"
}

Rh_stop () {
Kill-quit ' Cat $PIDFILE ' | | Echo-n "Not Running"
}

Rh_reload () {
Kill-hup ' Cat $PIDFILE ' | | Echo-n "can ' t reload"
}

Case "$" in
Start
Echo-n "Starting $DESC: $NAME"
Rh_start
echo "."
;;
Stop
Echo-n "Stopping $DESC: $NAME"
Rh_stop
echo "."
;;
Reload
Echo-n "Reloading $DESC configuration ..."
Rh_reload
echo "Reloaded."
;;
Restart
Echo-n "Restarting $DESC: $NAME"
Rh_stop
Sleep 1
Rh_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
Exit 3
;;
Esac
Exit 0

#=====================================================

Save after editing, execute the following command

sudo chmod+x/etc/Init.d/php-fpmsudo /Sbin/Chkconfig PHP-FPM on# Check   it out.sudo /Sbin/Chkconfig--listphp-fpmphp-fpm0: Off1: Off2: On3: On4: On5: On6: Off complete! You can use the following command to manage PHP-FPM service php-fpm startservice php-fpm stopservice php-fpm restartservice php-fpm Reload/ETC/INIT.D/PHP-FPM start/etc/init.d/php-fpm stop/etc/init.d/php-fpm restart/etc/init.d/php-fpm Reload

Note: The program path inside must be written right, here is the default path yo! And for PHP-FPM, the default PID file is not set, to use this must first enable the PID file in the configuration file! ~
  • 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.