原文 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

 

linux(centos)上配置nginx、mysql、php-fpm、redis开机启动<转>的更多相关文章

  1. Linux centos 6 配置php环境,扩展redis

    1.首先安装一个虚拟机(我自己版本:VM 10.0.4) yum -y install openssl psmisc openssl-devel php-devel pcre-devel gcc gc ...

  2. Centos上配置nginx+uwsgi+负载均衡配置

    负载均衡在服务端开发中算是一个比较重要的特性.因为Nginx除了作为常规的Web服务器外,还会被大规模的用于反向代理后端,Nginx的异步框架可以处理很大的并发请求,把这些并发请求hold住之后就可以 ...

  3. Linux2 在Linux(CentOS)上配置SSH免登陆

    前言:      本文主要是我在安装hadoop之前,需要先配置SSH免登陆.通过网上搜索,发现不少类似的资料,但多少都有些小问题,所以结合自己的实践,记录在此,作为参考.如果能帮助到其他人,自然是更 ...

  4. 阿里云服务器Linux CentOS安装配置(八)nginx安装、配置、域名绑定

    阿里云服务器Linux CentOS安装配置(八)nginx安装.配置.域名绑定 1.安装nginx yum -y install nginx 2.启动nginx service nginx star ...

  5. Linux CentOS上安装 MySQL 8.0.16

    前言: 因为我需要在我新安装的Linux CentOS系统服务器中安装和配置MySQL服务器,然而对于我们这种Linux使用小白而言在Linux系统中下载,解压,配置MySQL等一系列的操作还是有些耗 ...

  6. 阿里云服务器Linux CentOS安装配置(三)yum安装mysql

    阿里云服务器Linux CentOS安装配置(三)yum安装mysql 1.执行yum安装mysql命令:yum -y install mysql-server mysql-devel 2.启动mys ...

  7. 阿里云服务器Linux CentOS安装配置(一)购买阿里云服务器

    阿里云服务器Linux CentOS安装配置(一)购买阿里云服务器 我在阿里云购买的服务器配置 CPU:1核 内存:2G 系统盘:40G 公共镜像:CentOS 6.5 64位 公网带宽:1Mbps ...

  8. 阿里云服务器Linux CentOS安装配置(零)目录

    阿里云服务器Linux CentOS安装配置(零)目录 阿里云服务器Linux CentOS安装配置(一)购买阿里云服务器 阿里云服务器Linux CentOS安装配置(二)yum安装svn 阿里云服 ...

  9. 阿里云服务器Linux CentOS安装配置(七)域名解析

    阿里云服务器Linux CentOS安装配置(七)域名解析 1.购买域名 登录阿里云,左侧菜单点击[域名],然后[域名注册],完成域名购买.(一般首年45元) 2.添加域名解析 在域名列表里点击你的域 ...

随机推荐

  1. angular 实现总价满100折扣

    <div ng-controller="CartController"> <div ng-repeat="item in items"> ...

  2. how to use a xml_id in field domain

    "[('parent_id','child_of', %(other_module.xml_id)d)]"

  3. TreeList的VisibleNodesCount,Noes.Count,AllNdoesCount以及焦点节点的删除

    初始5个Nodes 隐藏23节点,打印全部节点Tag 显示23,打印全部节点Tag 隐藏全部节点,打印节点Tag TreeList.Nodes.Count == TreeList.AllNodesCo ...

  4. bzoj3639: Query on a tree VII

    Description You are given a tree (an acyclic undirected connected graph) with n nodes. The tree node ...

  5. 应用SVN(CentOS中搭建SVN服务器)

    简单介绍如何在虚拟机 CentOS 中,搭建 SVN 服务器. 软件版本信息 Vmware 10.0.0 build-1295980 CentOS 7.0-1406-x64 Java 1.7.0_67 ...

  6. SharePoint2013 Powershell script to get site Title, Site Owner, Site user count and usage

    Powershell script to get site Title, Site Owner, Site user count and usage Add-PSSnapin microsoft.sh ...

  7. 图片的css自适应

    当需要css来缩放图片的时候,可以采用外层容器100%或者任意百分比, 内层图片img tag 没有宽高,用sass写经过断点后的mixin中的样式就是这样: .workscon_section{ w ...

  8. 哟哟哟,JAVA组装的聊天室,最简单的实现

    太码多码码,总是多些感觉~~~ 打了快一个小时啊, 但看着一行一行的出来, 还是有成就感的~~:) VerySimpleChatServer.java import java.io.*; import ...

  9. nodejs触发事件的两种方式

    nodejs触发事件的两种方式: 方式之一:通过实例化events.EventEmitter //引入events模块 var events = require('events'); //初始化eve ...

  10. 数据结构(Splay平衡树):COGS 339. [NOI2005] 维护数列

    339. [NOI2005] 维护数列 时间限制:3 s   内存限制:256 MB [问题描述] 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线‘ _ ’表示实际 ...