2020年11月24日

Ubutun 16.04 编译安装 PHP7.4 稳定版

点击数:6

安装

下载 php-7.4.10并解压:

 wget https://www.php.net/distributions/php-7.4.10.tar.gz
 tar -zxvf php-7.4.10.tar.gz
 cd php-7.4.10

编译

./configure \
  --prefix=/usr/local/php \
  --with-config-file-path=/etc \
  --enable-fpm \
  --with-fpm-user=www \
  --with-fpm-group=www \
  --enable-pdo \
  --with-mysqli \
  --with-pdo-mysql \
  --with-mysql-sock \
  --with-pdo-sqlite \
  --with-iconv-dir \
  --with-freetype-dir \
  --with-png-dir=/usr/local/include/libpng16 \
  --with-jpeg-dir \
  --with-zlib \
  --with-zlib-dir \
  --with-iconv \
  --with-bz2 \
  --enable-libxml \
  --with-libxml-dir \
  --with-readline \
  --enable-xml \
  --with-xmlrpc \
  --disable-debug \
  --disable-rpath \
  --enable-bcmath \
  --enable-session \
  --enable-shmop \
  --enable-simplexml \
  --enable-sockets \
  --enable-sysvmsg \
  --enable-sysvsem \
  --enable-sysvshm \
  --enable-wddx \
  --enable-shared \
  --enable-inline-optimization \
  --enable-filter \
  --with-libdir=lib64 \
  --with-xsl \
  --with-curl \
  --with-cdb \
  --with-gmp \
  --enable-mbregex \
  --enable-mbregex-backtrack \
  --enable-mbstring \
  --with-mcrypt \
  --with-pcre-regex \
  --with-pcre-dir \
  --with-libmbfl \
  --with-onig \
  --enable-json \
  --enable-ftp \
  --enable-dom \
  --enable-exif \
  --enable-gd \
  --enable-gd-native-ttf \
  --enable-gd-jis-conv \
  --with-openssl-dir=/usr/include/openssl \
  --with-openssl \
  --with-mhash \
  --enable-pcntl \
  --enable-zip \
  --enable-soap \
  --enable-calendar \
  --with-pear \
  --with-gettext \
  --without-sqlite3 \
  --disable-fileinfo \
  --enable-maintainer-zts \
  --enable-mysqlnd-compression-support \
  --enable-opcache

安装:

make && make install

编译错误解决

No package ‘sqlite3’ found

错误如下:

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

No package 'sqlite3' found

解决方法:

apt-get install libsqlite3-dev

configure: error: Unable to locate gmp.h

错误内容如下:

checking for GNU MP support... yes

configure: error: Unable to locate gmp.h

解决方法:

wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
tar -zxvf oniguruma-6.9.4.tar.gz
 cd oniguruma-6.9.4/
 ./autogen.sh
 ./configure
 make && make install

recipe for target ‘ext/phar/phar.phar’ failed

错误如下:

Generating phar.phar

chmod: cannot access 'ext/phar/phar.phar': No such file or directory

Makefile:458: recipe for target 'ext/phar/phar.phar' failed

make: [ext/phar/phar.phar] Error 1 (ignored)

解决方法:

cp ext/phar/phar/phar.php ext/phar/phar.phar

libonig.so.5: cannot open shared object file

错误如下:

/root/php-7.4.10/sapi/cli/php: error while loading shared libraries: libonig.so.5: cannot open shared object file: No such file or directory

解决方法:

下面几条命令,加$表示在命令行执行,# 表示注释说明,没加其他符号的就是文件内容。

$ find / -name libonig.so.5
# /usr/local/lib/libonig.so.5 找到的文件路径
# 进入  /etc/ld.so.conf.d/ 目录
$ cd /etc/ld.so.conf.d/
# 打开libc.conf文件,如果没有就创建一个文件
$ vim libc.conf
# 输入下面内容保存
/usr/local/lib
$ ldconfig

创建启动脚本

/etc/init.d/目录下创建 php-fpm脚本,脚本内容如下,prefix=/usr/local/php就是 PHP 安装的目录,仔细检查 php-fpm 可执行文件以及 php-fpm.conf 配置文件的路径是否正确。

#! /bin/sh

### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    remote_fsnetwork
# Required-Stop:     remote_fsnetwork
# 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=/usr/local/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 --pidphp_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 --daemonizephp_opts

        if [ "?" != 0 ] ; then
            echo " failed"
            exit 1
        fi

        wait_for_pid createdphp_fpm_PID

        if [ -n "try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    stop)
        echo -n "Gracefully shutting down php-fpm "

        if [ ! -rphp_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 removedphp_fpm_PID

        if [ -n "try" ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;

    status)
        if [ ! -rphp_fpm_PID ] ; then
            echo "php-fpm is stopped"
            exit 0
        fi

        PID=`cat php_fpm_PID`
        if ps -pPID | grep -q PID; then
            echo "php-fpm (pidPID) 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 `catphp_fpm_PID`

        wait_for_pid removed php_fpm_PID

        if [ -n "try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    restart)
        0 stop0 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 `catphp_fpm_PID`

        echo " done"
    ;;

    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload|status}"
        exit 1
    ;;

esac

保存完上面的脚本内容之后,给脚本添加执行权限:

chmod 755 /etc/init.d/php-fpm

Ubutun 16.04 中使用sysv-rc-conf命令加入开机启动:

# 如果 sysv-rc-conf 命令不存在,需要安装
apt-get install sysv-rc-conf
sysv-rc-conf php-fpm on

如果是低版本的 Ubutun ,可以使用 chkconfig 命令:

chkconfig --add /etc/init.d/php-fpm
# 或者直接写 php-fpm
#chkconfig --add sysv-rc-conf
chkconfig sysv-rc-conf on

启动php-fpm

/etc/init.d/php-fpm start
# 或者
service php-fpm start

发表评论