LNMP一键重启服务(Nginx,PHP-fpm,Mysql)

LNMP框架一键启动:对于unix系统手动安装(非一键安装)LNMP框架来说,一键启动是非常有必要的。

服务命令有: "Usage: lnmp {start|stop|reload|restart|kill|status|allrestart}"

启动服务分为:

1、启动nginx;

2、启动mysql

3、启动php-fpm

4、启动其它的服务。

直接上代码:请把文件放于/usr/bin目录下运行

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script!"
    exit 1
fi

PHPFPMPIDFILE=/usr/local/php/var/run/php-fpm.pid

arg1=$1
arg2=$2

lnmp_start()
{
    echo "Starting LNMP..."
    /etc/init.d/nginx start
    /etc/init.d/mysqld start
    echo "Starting php-fpm..."
    /usr/local/php/sbin/php-fpm -c /etc/php.ini -y /usr/local/php/etc/php-fpm.conf
    echo "done."
}

lnmp_stop()
{
    echo "Stoping LNMP..."
    /etc/init.d/nginx stop
    /etc/init.d/mysqld stop
	echo "killall php-fpm"
    killall php-fpm
    echo "done."
}

lnmp_reload()
{
    echo "Reload LNMP..."
    /etc/init.d/nginx reload
	/etc/init.d/mysql reload
	echo "done."
}

lnmp_kill()
{
    echo "Kill nginx,php-fpm,mysql process..."
    killall nginx
    killall mysqld
    killall php-fpm
    killall php-cgi
    echo "done."
}

lnmp_status()
{
    /etc/init.d/nginx status
    #下面两种方式查看是否启动php-fpm服务
    #if [ -f $PHPFPMPIDFILE ]; then
    #    echo "php-fpm is runing!"
    #else
    #    echo "php-fpm is stop!"
    #fi
	if netstat -tnpl | grep php-fpm;then
        echo "php-fpm is runing!"
    else
        echo "php-fpm is stop!"
    fi
    /etc/init.d/mysqld status
}

case "${arg1}" in
    start)
        lnmp_start
        ;;
    stop)
        lnmp_stop
        ;;
    restart)
        lnmp_stop
        lnmp_start
        ;;
	allrestart)
        lnmp_stop
        lnmp_start
		echo "start svnserve"
		service svnserve start
		echo "start sendmail"
		/etc/init.d/sendmail start
        ;;
    reload)
        lnmp_reload
        ;;
	kill)
        lnmp_kill
        ;;
    status)
        lnmp_status
        ;;
    
    *)
        echo "Usage: lnmp {start|stop|reload|restart|kill|status|allrestart}"

        ;;
esac
exit

输入 lnmp restart

LNMP一键重启服务(Nginx,PHP-fpm,Mysql)_第1张图片

输入lnmp status:

另外,顺便附上启动nginx的文件,该文件放于目录“/etc/rc.d/init.d”下,参数请根据自己的目录调整

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

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

# Author:   licess
# website:  https://lnmp.org

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx-1.8.0/sbin/$NAME
CONFIGFILE=/usr/local/nginx-1.8.0/conf/$NAME.conf
PIDFILE=/usr/local/nginx-1.8.0/logs/$NAME.pid
if [ -s /bin/ss ]; then
    StatBin=/bin/ss
else
    StatBin=/bin/netstat
fi


case "$1" in
    start)
        echo -n "Starting $NAME... "

        if $StatBin -tnpl | grep -q nginx;then
            echo "$NAME (pid `pidof $NAME`) already running."
            exit 1
        fi

        $NGINX_BIN -c $CONFIGFILE

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Stoping $NAME... "

        if ! $StatBin -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            $0 force-quit
        else
            echo " done"
        fi
        ;;

    status)
        if $StatBin -tnpl | grep -q nginx; then
            PID=`pidof nginx`
            echo "$NAME (pid $PID) is running..."
        else
            echo "$NAME is stopped."
            exit 0
        fi
        ;;

    force-quit|kill)
        echo -n "Terminating $NAME... "

        if ! $StatBin -tnpl | grep -q nginx; then
            echo "$NAME is is stopped."
            exit 1
        fi

        kill `pidof $NAME`

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    reload)
        echo -n "Reload service $NAME... "

        if $StatBin -tnpl | grep -q nginx; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "$NAME is not running, can't reload."
            exit 1
        fi
        ;;

    configtest)
        echo -n "Test $NAME configure files... "

        $NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
        exit 1
        ;;

esac

 

你可能感兴趣的:(LNMP,nginx,php)