CentOS 7 下添加 PHP-FPM 至系统服务

Posted by Oscaner on December 14, 2018

创建 Nginx 启动脚本

1
> vim /etc/init.d/php-fpm
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh
# chkconfig:   2345 15 95

# description:  PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation \

# with some additional features useful for sites of any size, especially busier sites.
# DateTime: 2016-09-20

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

phpfpm="/usr/sbin/php-fpm"
prog=$(basename ${phpfpm})

lockfile=/var/lock/subsys/phpfpm

start() {
    [ -x ${phpfpm} ] || exit 5
    echo -n $"Starting $prog: "
    daemon ${phpfpm}
    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
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc ${phpfpm} -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
    ${phpfpm} -t
}

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
        ;;
    status)
        rh_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"
        exit 2
esac

设置权限

1
> chmod a+x /etc/init.d/php-fpm

注册服务

1
> chkconfig --add php-fpm

设置开机启动

1
2
> chkconfig php-fpm on
> chkconfig --list

PHP-FPM

1
2
3
4
5
6
7
8
9
10
# 查看 php-fpm 状态
> systemctl status php-fpm
# 启动 php-fpm
> systemctl start php-fpm
# 停止 php-fpm
> systemctl stop php-fpm
# 重启 php-fpm
> systemctl restart php-fpm
# 重载 php-fpm 配置
> systemctl reload php-fpm