Nginx and PHP-FPM startup & restart scripts

Source: Internet
Author: User
The Nginx and PHP on the server are both compiled and installed by source code. they do not have built-in service startup scripts like ubuntu, so they do not support nginx (start | restart | stop | reload. Self-help clothes and food. The following scripts are applicable to RHEL, Fedora, and CentOS. I. Nginx startup script/etc/init. d/nginx #! /Bin/bash # Nginx and PHP on the Startupscr server are both compiled and installed by source code. unlike ubuntu, there is a built-in service startup script, therefore, nginx (start | restart | stop | reload) is not supported. Self-help clothes and food. The following scripts are applicable to RHEL, Fedora, and CentOS.
I. Nginx startup script/etc/init. d/nginx

#! /Bin/bash
#
# Startup script for Nginx-this script starts and stops the nginx daemon
#
# 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/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/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

Save the edited file and run the following command:

Sudo chmod + x/etc/init. d/nginx
Sudo/sbin/chkconfig nginx on
# Check
Sudo/sbin/chkconfig -- list nginx
Nginx 0: off 1: off 2: on 3: on 4: on 5: on 6: off

Done! Run 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

2. 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 "$1" 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 the edited file and run the following command:

Sudo chmod + x/etc/init. d/php-fpm
Sudo/sbin/chkconfig php-fpm on
# Check
Sudo/sbin/chkconfig -- list php-fpm
Php-fpm 0: off 1: off 2: on 3: on 4: on 5: on 6: off

Done! You can use the following command to manage php-fpm.

Service php-fpm start
Service php-fpm stop
Service php-fpm restart
Service 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

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.