centos7编译安装 php7.4

  • 内容
  • 评论
  • 相关


这是一个 LNMP 环境的PHP7.4,安装了 PDO、curl、fileinfo、openssl、memcache、redis、swoole、yaf、yar、amqp、imagick、mcrypt、MongoDB等常用的扩展

1.安装依赖包

yum -y install autoconf libxml2 libxml2-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses curl gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel expat-devel xmlrpc-c xmlrpc-c-devel libicu-devel libmemcached-devel librabbitmq librabbitmq-devel ImageMagick ImageMagick-devel libevent libevent-devel libyaml libyaml-devel libssh2 libssh2-devel

2.下载并解压php7.4

cd /usr/local/src
wget https://xmyunwei.com/php-7.4.11.tar.gz
tar -zxvf php-7.4.11.tar.gz
cd php-7.4.11 

3.编译安装(./configure --help 查看编译参数)

编译前,新增用户组,用户,用于编译使用

id www
#id: www: no such user
groupadd www
#groupadd: group 'www' already exists
//开始添加www用户
useradd -g www www -s /sbin/nologin
//再查看一下
id www
#uid=1001(www) gid=1000(www) 组=1000(www)

安装更新 libzip

#先删除旧版本
yum remove -y libzip
#下载编译安装
wget https://libzip.org/download/libzip-1.7.3.tar.gz
tar -zxvf libzip-1.7.3.tar.gz
cd libzip-1.7.3

#安装cmake3
yum install -y cmake3
ln -s /usr/bin/cmake3 /usr/bin/cmake

#查询cmake版本

cmake --version
-----------------------
cmake3 version 3.17.3

CMake suite maintained and supported by Kitware (kitware.com/cmake).
-----------------------

#在libzip-1.7.3文件夹内

mkdir build && cd build
cmake ..
make && make install

#检查环境变量
echo $PKG_CONFIG_PATH

##添加环境变量
#如果变量返回值为空:
export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/

#如果变量返回有库链接
export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/:$PKG_CONFIG_PATH

永久写入:

vim /etc/profile

PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/:/usr/local/lib/pkgconfig/:/server/openssl/11/lib/pkgconfig/
export PKG_CONFIG_PATH

#保存退出

#使配置生效
source /etc/profile

开始编译(根据自己需要增减)

cd /usr/local/src/php-7.4.11
./configure \
--prefix=/server/php/74 \
--with-config-file-path=/server/php/74/etc \
--with-fpm-user=www \
--with-fpm-group=www \
--with-curl \
--enable-gd \
--with-gettext \
--with-iconv \
--with-kerberos \
--with-libdir=lib64 \
--with-mysqli=mysqlnd \
--with-openssl \
--with-pdo-mysql=mysqlnd \
--with-pdo-sqlite \
--with-pear \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-zlib-dir \
--with-bz2 \
--with-mhash \
--enable-fpm \
--enable-bcmath \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-xml \
--enable-fpm \
--enable-exif \
--disable-debug

这里需要注意的是在php7.4 编译参数 --with-gd 要改成了 --enable-gd

error: Package requirements (sqlite3 > 3.7.4) were not met

yum install libsqlite3x-devel -y

error: Package requirements (oniguruma) were not met

yum install oniguruma-devel -y

当yum install 提示 "没可用软件包"

yum install -y epel-release

由于官方源比较慢,我用阿里的

wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y update 

off_t undefined 报错

configure: error: off_t undefined; check your library configuration

off_t 类型是在 头文件 unistd.h中定义的,
在32位系统 编程成 long int ,64位系统则编译成 long long int ,
在进行编译的时候 是默认查找64位的动态链接库,
但是默认情况下 centos 的动态链接库配置文件/etc/ld.so.conf里并没有加入搜索路径,
这个时候需要将 /usr/local/lib64 /usr/lib64 这些针对64位的库文件路径加进去。

#添加搜索路径到配置文件
echo '/usr/local/lib64
/usr/lib64'>> /etc/ld.so.conf.d/local_lib.conf

#然后 更新配置
ldconfig -v

/usr/local/include/zip.h:59:21: fatal error: zipconf.h: No such file or directory

cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
到这里说明编译成功了~

到这里说明编译成功了~

编译没问题了,执行

make && make install

安装完毕

4.配置环境

执行完安装命令后php7.4就已经安装在到了/server/php/74目录下了

/server/php/74/bin/php -v

查看版本

添加环境变量

vim /etc/profile

添加到最后

PATH=/server/php/74/bin:$PATH
export PATH

更新环境变量

source /etc/profile

查看版本

php -v

5.配置php-fpm

cp php.ini-production /server/php/74/etc/php.ini
cp /server/php/74/etc/php-fpm.conf.default /server/php/74/etc/php-fpm.conf

修改php-fpm.conf 配置文件

vim /server/php/74/etc/php-fpm.conf 

配置详情

[global]
pid = /server/php/74/var/run/php-fpm.pid
error_log = /server/php/74/var/log/php_error.log
log_level = notice

[www]
listen = /tmp/php-cgi-74.sock
listen.backlog = 8192
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.status_path = /phpfpm_74_status
pm.max_children = 80
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 20
request_terminate_timeout = 100
request_slowlog_timeout = 30
slowlog = var/log/slow.log

创建php-fpm脚本至/etc/init.d目录

vim /etc/init.d/php-fpm-74

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

# Author:   PandaMan
# website:  https://xmyunwei.com

#注意prefix路径根据实际情况填写
prefix=/server/php/74
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

修改文件权限

chmod +x /etc/init.d/php-fpm-74
启动php-fpm
systemctl enable php-fpm-74
systemctl start php-fpm-74

#查看状态

systemctl status php-fpm-74

评论

0条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注