linux(centos)上配置nginx、mysql、php-fpm、redis开机启动<转>

原文 http://levi.cg.am/archives/2925

I、nginx开机启动

  1. 在/etc/init.d/目录下创建脚本
    1
    vi  /etc/init .d /nginx
  2. 更改脚本权限
    1
    chmod 775 /etc/init .d /nginx
  3. 编写脚本内容
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    #!/bin/bash
    # nginx Startup script for the Nginx HTTP Server
    # it is v.0.0.2 version.
    # chkconfig: - 85 15
    # description: Nginx is a high-performance web and proxy server.
    #              It has a lot of features, but it's not for everyone.
    # processname: nginx
    # pidfile: /var/run/nginx.pid
    # config: /usr/local/nginx/conf/nginx.conf
    nginxd= /usr/local/webserver/nginx/sbin/nginx
    nginx_config= /usr/local/webserver/nginx/conf/nginx .conf
    nginx_pid= /usr/local/webserver/nginx/logs/nginx .pid
    RETVAL=0
    prog= "nginx"
    # Source function library.
    /etc/rc .d /init .d /functions
    # Source networking configuration.
    /etc/sysconfig/network
    # Check that networking is up.
    [ ${NETWORKING} = "no" ] && exit 0
    [ -x $nginxd ] || exit 0
    # Start nginx daemons functions.
    start() {
    if [ -e $nginx_pid ]; then
        echo "nginx already running...."
        exit 1
    fi
        echo -n $ "Starting $prog: "
        daemon $nginxd -c ${nginx_config}
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
        return $RETVAL
    }
    # Stop nginx daemons functions.
    stop() {
             echo -n $ "Stopping $prog: "
             killproc $nginxd
             RETVAL=$?
             echo
             [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/webserver/nginx/logs/nginx .pid
    }
     
    reload() {
         echo -n $ "Reloading $prog: "
         #kill -HUP `cat ${nginx_pid}`
         killproc $nginxd -HUP
         RETVAL=$?
         echo
    }
    # See how we were called.
    case "$1" in
    start)
             start
             ;;
    stop)
             stop
             ;;
    reload)
             reload
             ;;
    restart)
             stop
             start
             ;;
    status)
             status $prog
             RETVAL=$?
             ;;
    *)
             echo $ "Usage: $prog {start|stop|restart|reload|status|help}"
             exit 1
    esac
    exit $RETVAL
  4. 设置开机启动
    1
    chkconfig nginxd on

II、设置mysql开机启动

  1. 将mysql安装目录下 support-files目录下的mysql.server文件拷贝到/etc/init.d/目录下并改名为mysqld,并更改权限
    1
    chmod 775 /etc/init .d /mysqld
  2. 设置开机启动
    1
    chkconfig mysqld on

III、php-fpm开机启动

  1. 在/etc/init.d/目录下创建脚本
    1
    vi /etc/init .d /php-fpm
  2. 更改脚本权限
    1
    chmod 775 /etc/init .d /php-fpm
  3. 编写脚本内容
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    #!/bin/sh
    #
    # php-fpm - this script starts and stops the php-fpm daemin
    #
    # chkconfig: - 85 15
    # processname: php-fpm
    # config:      /usr/local/php/etc/php-fpm.conf
     
    set -e
     
    PATH= /usr/local/sbin : /usr/local/bin : /sbin : /bin : /usr/sbin : /usr/bin
    DESC= "php-fpm daemon"
    NAME=php-fpm
    DAEMON= /usr/local/php/sbin/ $NAME     // 这里设成自己的目录
    CONFIGFILE= /usr/local/php/etc/php-fpm .conf   // 这里设成自己的目录
    PIDFILE= /usr/local/php/var/run/ $NAME.pid   // 这里设成自己的目录
    SCRIPTNAME= /etc/init .d/$NAME   // 这里设成自己的目录
     
    # If the daemon file is not found, terminate the script.
    test -x $DAEMON || exit 0
     
    d_start(){
         $DAEMON -y $CONFIGFILE || echo -n " already running"
    }
     
    d_stop(){
         kill -QUIT ` cat $PIDFILE` || echo -n " no running"
    }
     
    d_reload(){
         kill -HUP ` cat $PIDFILE` || echo -n " could not reload"
    }
     
    case "$1" in
         start)
             echo -n "Starting $DESC: $NAME"
             d_start
             echo "."
             ;;
         stop)
             echo -n "Stopping $DESC: $NAME"
             d_stop
             echo "."
             ;;
         reload)
             echo -n "Reloading $DESC configuration..."
             d_reload
             echo "Reloaded."
             ;;
         restart)
             echo -n "Restarting $DESC: $NAME"
             d_stop
             # Sleep for two seconds before starting again, this should give the nginx daemon some time to perform a graceful stop
             sleep 2
             d_start
             echo "."
             ;;
         *)
             echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload)" >&2
             exit 3
             ;;
    esac
    exit 0
  4. 设置开机启动
    1
    chkconfig php-fpm on

Ⅳ、设置redis开机启动

  1. 在/etc/init.d/目录下创建脚本
    1
    vi /etc/init .d /redis
  2. 更改脚本权限
    1
    chmod 775 /etc/init .d /redis
  3. 编写脚本内容
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    ###########################
    PATH= /usr/local/bin : /sbin : /usr/bin : /bin
     
    REDISPORT=6379
    EXEC= /usr/local/bin/redis-server
    REDIS_CLI= /usr/local/bin/redis-cli
     
    PIDFILE= /var/run/redis .pid
    CONF= "/etc/redis.conf"
     
    case "$1" in
         start)
             if [ -f $PIDFILE ]
             then
                     echo "$PIDFILE exists, process is already running or crashed"
             else
                     echo "Starting Redis server..."
                     $EXEC $CONF
             fi
             if [ "$?" = "0" ]
             then
                   echo "Redis is running..."
             fi
             ;;
         stop)
             if [ ! -f $PIDFILE ]
             then
                     echo "$PIDFILE does not exist, process is not running"
             else
                     PID=$( cat $PIDFILE)
                     echo "Stopping ..."
                     $REDIS_CLI -p $REDISPORT SHUTDOWN
                     while [ -x ${PIDFILE} ]
                    do
                         echo "Waiting for Redis to shutdown ..."
                         sleep 1
                     done
                     echo "Redis stopped"
             fi
             ;;
        restart|force-reload)
             ${0} stop
             ${0} start
             ;;
       *)
         echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
             exit 1
    esac
    ##############################
  4. 设置开机启动
    1
    chkconfig redis on

至此,大功告成。可以用命令 chkconfig 查看开机启动服务列表

1
chkconfig --list

附录:

1、nigx重启错误

bind() to 0.0.0.0:80 failed (98: Address already in use)

这个是nginx重启是 经常遇到的。   网上找了很多信息 都是没有啥用。说的乱七八糟的。   发现原来是nginx重复重启。自己占用了端口。 解决方法

1
killall -9 nginx

杀掉nginx 进程  然后重启就行了。

1
service nginx restart

2、php-fpm 启动 关闭

php-fpm 不再支持 php-fpm 补丁具有的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,需要使用信号控制:

master进程可以理解以下信号

  • SIGINT, SIGTERM 立刻终止
  • SIGQUIT 平滑终止
  • SIGUSR1 重新打开日志文件
  • SIGUSR2 平滑重载所有worker进程并重新载入配置和二进制模块

示例:

  1. php-fpm 关闭:
    1
    kill -SIGINT ` cat /usr/local/php/var/run/php-fpm .pid`
  2. php-fpm 重启:
    1
    kill -SIGUSR2 ` cat /usr/local/php/var/run/php-fpm .pid`

其次配置文件不再使用的xml 格式,改为了INI,但是配置参数几乎和以前一样,可参照xml格式的格式配置。

3、nginx 启动 关闭

  1. nginx的启动(nginx.conf文件基本上位于nginx主目录中的conf目录中)
    1
    nginx -c nginx.conf
  2. nginx的停止(nginx.pid文件基本上位于nginx主目录中的logs目录中)
    1
    ps -ef | grep nginx

    可发现数个nginx进程,其中标有master的为主进程,其它为子进程, 停止nginx主要就是对主进程进行信号控制.

    1. 从容停止
      1
      kill -QUIT ` cat nginx.pid`
    2. 快速停止
      1
      kill -TERM ` cat nginx.pid`

      or

      1
      kill -INT ` cat nginx.pid`
    3. 强制停止
      1
      kill -9 ` cat nginx.pid`
  3. nginx的平滑重启
    首先要验证新的配置文件是否正确:
    1
    nginc -t -c nginx.conf

    成功后向主进程发送HUP信号即可: [/shell]kill -HUP `cat nginx.pid`[/shell]

4、nginx的平滑升级

  1. 备份好旧的可执行文件,使用新版本替换旧版本
  2. kill -USR2 旧版本的主进程PID 进行平滑升级, 此时新老版本共存
  3. kill -WINCH 旧版本的主进程PID  逐步关闭旧主进程的工作进程
  4. 当旧主进程产生的工作进程全部关闭后, 可以决定是否使用新版本还是旧版本.(需要使用kill命令来杀死新或旧主进程)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #!/bin/sh
    BASE_DIR= '/usr/local/'
    ${BASE_DIR}nginx /sbin/nginx -t -c ${BASE_DIR}nginx /conf/nginx .conf >& ${BASE_DIR}nginx /logs/nginx .start
    info=` cat ${BASE_DIR}nginx /logs/nginx .start`
    if [ ` echo $info | grep -c "syntax is ok" ` - eq 1 ]; then
    if [ ` ps aux| grep "nginx" | grep -c "master" ` == 1 ]; then
    kill -HUP ` cat ${BASE_DIR}nginx /logs/nginx .pid`
    echo "ok"
    else
    killall -9 nginx
    sleep 1
    ${BASE_DIR}nginx /sbin/nginx
    fi
    else
    echo "######## error: ########"
    cat ${BASE_DIR}nginx /logs/nginx .start
    fi

5、CentOS修改系统环境变量

我这里拿php作为一个例子,我的php安装在/usr/local/webserver/php下,没有把php加入环境变量时,你在命令行执行

1
2
#查看当前php的版本信息
[root@CentOS ~] # php -v

会提示你此命令不存在。

下面详细说说linux下修改环境变量的方法

方法一:

在/etc/profile文件中添加变量【对所有用户生效(永久的)】
用VI在文件/etc/profile文件中增加变量,该变量将会对Linux下所有用户有效,并且是“永久的”。

1
[root@CentOS ~] # vi /etc/profile

在文件末尾加上如下两行代码

1
2
PATH= /usr/local/webserver/php/bin :$PATH
export PATH

如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# /etc/profile
 
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
 
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
 
pathmunge () {
     case ":${PATH}:" in
         *: "$1" :*)
             ;;
         *)
             if [ "$2" = "after" ] ; then
                 PATH=$PATH:$1
             else
                 PATH=$1:$PATH
             fi
     esac
}
 
if [ -x /usr/bin/id ]; then
     if [ -z "$EUID" ]; then
         # ksh workaround
         EUID=` id -u`
         UID=` id -ru`
     fi
     USER= "`id -un`"
     LOGNAME=$USER
     MAIL= "/var/spool/mail/$USER"
fi
 
# Path manipulation
if [ "$EUID" = "0" ]; then
     pathmunge /sbin
     pathmunge /usr/sbin
     pathmunge /usr/local/sbin
else
     pathmunge /usr/local/sbin after
     pathmunge /usr/sbin after
     pathmunge /sbin after
fi
 
HOSTNAME=` /bin/hostname 2> /dev/null `
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
     export HISTCONTROL=ignoreboth
else
     export HISTCONTROL=ignoredups
fi
 
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
 
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
     umask 002
else
     umask 022
fi
 
for i in /etc/profile .d/*.sh ; do
     if [ -r "$i" ]; then
         if [ "${-#*i}" != "$-" ]; then
             . "$i"
         else
             . "$i" > /dev/null 2>&1
         fi
     fi
done
 
unset i
unset pathmunge
 
PATH= /usr/local/webserver/php/bin :$PATH
export PATH

要是刚才的修改马上生效,需要执行以下代码

1
[root@CentOS ~] # source /etc/profile

这时再查看系统环境变量,就能看见刚才加的东西已经生效了

1
2
[root@CentOS ~] # echo $PATH
/usr/local/webserver/php/bin : /usr/lib/qt-3 .3 /bin : /usr/local/sbin : /usr/local/bin : /sbin : /bin : /usr/sbin : /usr/bin : /root/bin

现在就能直接使用php命令了(而不是像之前写很长一串/usr/local/webserver/php/bin/php -v),例如查看当前php的版本

1
2
3
4
[root@CentOS ~] # php -v
PHP 5.3.8 (cli) (built: Jun 27 2012 14:28:20)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
方法二:

在用户目录下的.bash_profile文件中增加变量【对单一用户生效(永久的)】
用VI在用户目录下的.bash_profile文件中增加变量,改变量仅会对当前用户有效,并且是“永久的”。具体操作和方法1一样,这里就不在列举代码了。

方法三:

直接运行export命令定义变量【只对当前shell(BASH)有效(临时的)】

在shell的命令行下直接使用[export变量名=变量值]定义变量,该变量只在当前的shell(BASH)或其子shell(BASH)下是有效的,shell关闭了,变量也就失效了,再打开新shell时就没有这个变量,需要使用的话还需要重新定义。例如

1
export PATH= /usr/local/webserver/php/bin :$PATH

 

转载于:https://www.cnblogs.com/debmzhang/p/4195176.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值