ChunBlog.Com

我愿像茶一样 把苦涩留在心里 散发出来的都是清香



Centos7.4 编译安装PHP 7.24 并配置开机启动

网络资料显示PHP7性能是PHP5的三倍(wordpress 而言),而且代码向下兼容,春风也把PHP5.6更换成了PHP7.2,性能提升明显,而且感觉更节省内存。

PHP5 PHP7 性能对比图

PHP7性能多方评测结果图片,来源于网络

编译安装PHP 7.24
系统环境:Centos 7.4

安装依赖

1
yum -y install gd-devel zlib-devel libjpeg-devel libpng-devel libiconv-devel freetype-devel libxml2 libxml2-devel openssl openssl-devel curl-devel libxslt-devel libmcrypt-devel mhash

下载PHP7.24

1
wget http://cn2.php.net/distributions/php-7.2.4.tar.gz

解压后进入PHP7.24目录

1
2
3
4
#解压
tar -xzvf php-7.2.4.tar.gz
#进入目录
cd php-7.2.4

新建用户 我这里使用Tengine的用户

1
2
3
4
#新建www用户组
groupadd www
#新建Tengine用户
useradd -g www tengine -s /sbin/nologin

开始编译

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
./configure \
--prefix=/data/apps/php \
--with-config-file-path=/data/apps/conf/php/etc \
--with-mysql-sock=/dev/shm/mysql.sock \
--enable-fpm \
--with-fpm-user=tengine  \
--with-fpm-group=www \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared  \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir  \
--with-freetype-dir \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets  \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache

安装

1
make && make install

更改PHP安装目录所属用户 复制配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#新建配置文件目录
mkdir -p data/apps/conf/php/etc/php-fpm.d
#复制配置文件
cp -rf /data/apps/php/etc/* /data/apps/conf/php/etc
cp /root/php-7.2.4/php.ini* /data/apps/php/etc/
#到安装目录
cd /data/apps/conf/php/etc
#把php.ini-production复制为php.ini
cp php.ini-production php.ini
#把php-fpm.conf.default复制为php-fpm.conf
cp php-fpm.conf.default  php-fpm.conf
#把php-fpm.d/www.conf.default复制为php-fpm.d/www.conf
cp php-fpm.d/www.conf.default php-fpm.d/www.conf
#更改PHP安装目录所属用户
chown -R tengine:www /data/apps/php /data/apps/conf/php

配置php.ini

1
2
3
4
5
6
7
8
9
10
11
# 字符集UTF-8
default_charset = "UTF-8"
# 设置PHP的扩展库路径,默认被注释了。
extension_dir = "/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/"
#添加opcache扩展
zend_extension=opcache.so
# 设置PHP的时区
date.timezone = PRC
# 开启opcache,默认是0
[opcache]
opcache.enable=1

配置php-fpm.d目录下www.conf

1
2
3
4
5
6
7
; 设置用户和用户组
user = tengine
group = www
; 下面是默认值,如果PHP Nginx在一台服务器不建议使用 我这里注释掉
; listen = 127.0.0.1:9000
; sock目录改为/dev/shm物理硬件内存 读写速度比硬盘快N倍
listen = /dev/shm/php-fpm.sock

保存配置检验配置是否正确

1
/data/apps/php/sbin/php-fpm -t

添加环境变量

1
2
3
4
#编辑/etc/profile
vim /etc/profile
#在尾部加入
export PATH=/data/apps/php/bin:/data/apps/php/sbin

启动

1
/data/apps/php/sbin/php-fpm

让tengine或Nginx 支持PHP

编辑nginx.conf更改为以下内容。

1
2
3
4
5
6
7
        #PHP模块
        location ~ \.php$ {
            fastcgi_pass   unix:/dev/shm/php-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

创建开机启动脚本

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO
 
prefix=/data/apps/php
exec_prefix=${prefix}
 
php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid
 
 
php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"
 
 
wait_for_pid () {
    try=0
 
    while test $try -lt 35 ; do
 
        case "$1" in
            'created')
            if [ -f "$2" ] ; then
                try=''
                break
            fi
            ;;
 
            'removed')
            if [ ! -f "$2" ] ; then
                try=''
                break
            fi
            ;;
        esac
 
        echo -n .
        try=`expr $try + 1`
        sleep 1
 
    done
 
}
 
case "$1" in
    start)
        echo -n "Starting php-fpm "
 
        $php_fpm_BIN --daemonize $php_opts
 
        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        fi
 
        wait_for_pid created $php_fpm_PID
 
        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
 
    stop)
        echo -n "Gracefully shutting down php-fpm "
 
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
 
        kill -QUIT `cat $php_fpm_PID`
 
        wait_for_pid removed $php_fpm_PID
 
        if [ -n "$try" ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;
 
    status)
        if [ ! -r $php_fpm_PID ] ; then
            echo "php-fpm is stopped"
            exit 0
        fi
 
        PID=`cat $php_fpm_PID`
        if ps -p $PID | grep -q $PID; then
            echo "php-fpm (pid $PID) is running..."
        else
            echo "php-fpm dead but pid file exists"
        fi
    ;;
 
    force-quit)
        echo -n "Terminating php-fpm "
 
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
 
        kill -TERM `cat $php_fpm_PID`
 
        wait_for_pid removed $php_fpm_PID
 
        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
 
    restart)
        $0 stop
        $0 start
    ;;
 
    reload)
 
        echo -n "Reload service php-fpm "
 
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
 
        kill -USR2 `cat $php_fpm_PID`
 
        echo " done"
    ;;
 
    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload|status}"
        exit 1
    ;;
 
esac

把php-fpm加入服务 设置为启动

1
2
3
4
5
6
7
8
9
# 给php-fpm可执行权限
chmod +x /etc/init.d/php-fpm
# php-fpm加入服务
chkconfig --add php-fpm
# php-fpm 设置为开机启动
chkconfig php-fpm on
# 查看是否开机启动 2:no 3:no 4:no级别下为启动
chkconfig --list php-fpm
php-fpm         0:off   1:off   2:on    3:on    4:on    5:on    6:off

重启、启动、停止php-fpm命令

1
2
3
4
5
6
7
8
# 启动
service php-fpm start
# 关闭
service php-fpm stop
# 重启
service php-fpm restart
# 重载配置文件
service php-fpm reload


作者  :  耿保春

相识是一种美,人海茫茫遇见你!



文章评论

发表留言

About ME

about me

春风

我愿像茶一样,把苦涩留在心里,散发出来的都是清香

Contact ME