PHP + Nginx + MongoDB set to enable the service automatically upon startup

Source: Internet
Author: User
Tags fpm mongodb

Nginx automatic startup script

Save the following script as an nginx file and put it in/etc/init. d/nginx

Then you can use

/Etc/init. d/nginx start command to start nginx
/Etc/init. d/nginx stop command to stop nginx
/Etc/init. d/nginx restart command restart nginx

Start nginx automatically at startup,

If you need to start the service, after saving the/etc/init. d/nginx file,

Run the following command:

The code is as follows: Copy code

Chkconfig -- add ningx

Chkconfig -- level nginx 2345 on

------------------- The split line contains the script content. You need to change the red font -------------------------------------------

The code is as follows: Copy code

#! /Bin/sh

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/$ NAME. conf
PIDFILE =/usr/local/nginx/logs/$ NAME. pid
SCRIPTNAME =/etc/init. d/$ NAME

Set-e
[-X "$ DAEMON"] | exit 0

Do_start (){
$ DAEMON-c $ CONFIGFILE | echo-n "nginx already running"
}

Do_stop (){
Kill-INT 'cat $ pidfile' | echo-n "nginx not running"
}

Do_reload (){
Kill-HUP 'cat $ pidfile' | echo-n "nginx can't reload"
}

Case "$1" in
Start)
Echo-n "Starting $ DESC: $ NAME"
Do_start
Echo "."
;;
Stop)
Echo-n "Stopping $ DESC: $ NAME"
Do_stop
Echo "."
;;
Reload | graceful)
Echo-n "Reloading $ DESC configuration ..."
Do_reload
Echo "."
;;
Restart)
Echo-n "Restarting $ DESC: $ NAME"
Do_stop
Do_start
Echo "."
;;
*)
Echo "Usage: $ SCRIPTNAME {start | stop | reload | restart}"> & 2
Exit 3
;;
Esac

Exit 0


 

I only wrote one above. I will write a script together with nginx, php, and mongodb.


It is too troublesome to start nginx, php, and mongodb one by one at a time. A script is written, but the log is output at MongoDB startup, so it is not perfect and logs are displayed.

The code is as follows: Copy code

/Root/webserver

#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Export LC_ALL = C
# Check if user is root
If [$ (id-u )! = "0"]; then
Printf "Error: You must be root to run this script! N"
Exit 1
Fi

NGINXDAEMON =/home/server/nginx-1.4.1/sbin/nginx
NGINXPIDFILE =/home/server/nginx-1.4.1/logs/nginx. pid
PHPFPMDAEMON =/home/server/php-5.4.15/sbin/php-fpm
PHPFPMPID =/home/server/php-5.4.15/var/run/php-fpm.pid
Export dbdaemon =/home/server/mongodb-linux-i686-2.4.3/bin/mongod
MONGODBCONF =/home/server/mongodb-linux-i686-2.4.3/mongodb. conf

Function_start ()
{
Printf "Starting Nginx + PHP + Mongodb... n"
If [-f $ NGINXPIDFILE]; then
Printf "Nginx is runing! N"
Else
$ NGINXDAEMON
Fi
If [-f $ PHPFPMPID]; then
Printf "PHP-FPM is runing! N"
Else
$ PHPFPMDAEMON
Fi
Lsof-I: 27017 & printf "MongoDB is runing! N "| $ MONGODBDAEMON -- config $ MONGODBCONF
}

Function_stop ()
{
Printf "Stoping Nginx + PHP... n"
If [-f $ NGINXPIDFILE]; then
Kill-QUIT 'cat $ nginxpidfile'
Else
Printf "Nginx program is not runing! N"
Fi

If [-f $ PHPFPMPID]; then
Kill-INT 'cat $ phpfpmpid'
Else
Printf "PHP-FPM program is not runing! N"
Fi

Lsof-I: 27017 & killall mongod | printf "MongoDB is not runing! N"
}

Function_status ()
{
If [-f $ NGINXPIDFILE]; then
Printf "Nginx is runing! N"
Else
Printf "Nginx is stop! N"
Fi
If [-f $ PHPFPMPID]; then
Printf "PHP-FPM is runing! N"
Else
Printf "PHP-FPM is stop! N"
Fi

Lsof-I: 27017 & printf "MongoDB is runing! N "| printf" MongoDB is stop! N"

}

If ["$1" = "start"]; then
Function_start
Elif ["$1" = "stop"]; then
Function_stop
Elif ["$1" = "restart"]; then
Function_stop
Function_start
Elif ["$1" = "status"]; then
Function_status
Else
Printf "Usage:/root/webserver {start | stop | restart | status} n"
Fi

Chmod + x/root/webserver

Add boot start:

The code is as follows: Copy code
Vi/etc/rc. local

Add

The code is as follows: Copy code
/Root/webserver start

Restart takes effect

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.