• 欢迎访问春风博客

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

生活笔记 xiaogang 5年前 (2018-04-08) 1244次浏览 0个评论

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

PHP5 PHP7 性能对比图

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

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

安装依赖

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

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

解压后进入PHP7.24目录

#解压
tar -xzvf php-7.2.4.tar.gz
#进入目录
cd php-7.2.4

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

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

开始编译

./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

安装

make && make install

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

#新建配置文件目录
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

# 字符集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

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

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

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

添加环境变量

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

启动

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

让tengine或Nginx 支持PHP

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

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

创建开机启动脚本

vim /etc/init.d/php-fpm

写入以下内容

#! /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加入服务 设置为启动

# 给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命令

# 启动
service php-fpm start
# 关闭
service php-fpm stop
# 重启
service php-fpm restart
# 重载配置文件
service php-fpm reload

ChunBlog.Com , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Centos7.4 编译安装PHP 7.24 并配置开机启动
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址