准备工作

下载 PHP 7.4 源码

PHP版本发布:(https://www.php.net/downloads)[https://www.php.net/downloads]

这里选择的是最新的 PHP 7.4.9 (其实8.0 的 Beta 测试版已经出来了)

1
wget https://www.php.net/distributions/php-7.4.9.tar.gz -O /opt/php74.tar.gz

解压

1
cd /opt && tar -zxf /opt/php74.tar.gz

查看解压内容

1
cd php74 && ls -lh

安装工作

configure

执行 ./configure

1
./configure -prefix=/usr/local/php  --with-curl  --with-gettext  --with-iconv-dir  --with-kerberos  --with-libdir=lib64  --with-mysqli  --with-openssl  --with-pdo-mysql  --with-pdo-sqlite  --with-pear  --with-xmlrpc  --with-xsl  --with-zlib --enable-fpm  --enable-bcmath  --enable-inline-optimization  --enable-mbregex  --enable-mbstring  --enable-opcache  --enable-pcntl  --enable-shmop  --enable-soap  --enable-sockets  --enable-sysvsem  --enable-xml

处理 configure 报错

报错:
configure: error: no acceptable C compiler found in $PATH

安装 gcc
apt install gcc

报错

1
2
3
configure: error: The pkg-config script could not be found or is too old.  Make sure it
is in your PATH or set the PKG_CONFIG environment variable to the full
path to pkg-config.

安装 pkg-config

1
apt install pkg-config

报错

1
2
configure: error: Package requirements (libxml-2.0 >= 2.7.6) were not met:
No package 'libxml-2.0' found

安装 libxml

1
2
apt install libxml2
apt install libxml2-dev

报错

1
2
3
configure: error: Package requirements (krb5-gssapi krb5) were not met:
No package 'krb5-gssapi' found
No package 'krb5' found

安装 libkrb5

1
apt install libkrb5-dev

报错

1
2
3
4
configure: error: Package requirements (openssl >= 1.0.1) were not met:
No package 'openssl' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you

安装 libssl-dev

1
apt install libssl-dev

报错

1
2
configure: error: Package requirements (sqlite3 > 3.7.4) were not met:
No package 'sqlite3' found

安装 libsqlite3-dev

1
apt install libsqlite3-dev

报错

1
2
configure: error: Package requirements (oniguruma) were not met:
No package 'oniguruma' found

这个包在 apt 里没找到只能自己编译安装了

编译安装 oniguruma

1
2
3
4
5
cd /opt
wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
tar -zxf oniguruma-6.9.4.tar.gz
cd oniguruma-6.9.4
./autogen.sh

报错

1
2
Generating autotools files.
./autogen.sh: 6: ./autogen.sh: autoreconf: not found

安装 libtool automake

1
apt install libtool automake

再来一次就可以了

1
./autogen.sh

执行 ./configure 看到

1
Run ./configure, make, and make install.

说明 configure 成功

编译安装 make && make install

1
make && make install

回到 php74 目录继续安装工作

1
cd /opt/php74

执行 ./configure

1
./configure -prefix=/usr/local/php  --with-curl  --with-gettext  --with-iconv-dir  --with-kerberos  --with-libdir=lib64  --with-mysqli  --with-openssl  --with-pdo-mysql  --with-pdo-sqlite  --with-pear  --with-xmlrpc  --with-xsl  --with-zlib --enable-fpm  --enable-bcmath  --enable-inline-optimization  --enable-mbregex  --enable-mbstring  --enable-opcache  --enable-pcntl  --enable-shmop  --enable-soap  --enable-sockets  --enable-sysvsem  --enable-xml

报错

1
2
configure: error: Package requirements (libxslt >= 1.1.0) were not met:
No package 'libxslt' found

安装 libxslt1-dev

1
apt install libxslt1-dev

终于 configure 通过

1
2
3
4
5
6
7
8
9
10
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+

Thank you for using PHP.

太不容易了

激动人心的 make && make install

1
make && make install
1
2
3
4
5
6
7
8
9
10
11
[PEAR] Archive_Tar    - installed: 1.4.9
[PEAR] Console_Getopt - installed: 1.4.3
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util - installed: 1.4.5
warning: pear/PEAR dependency package "pear/Archive_Tar" installed version 1.4.9 is not the recommended version 1.4.4
[PEAR] PEAR - installed: 1.10.12
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/opt/php74/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin/phar.phar
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers: /usr/local/php/include/php/ext/pdo/

调整配置

查看 php.ini 默认的装载目录

因为在编译阶段没有加入 --with-config-file-path=(path-to-ini-dir),所以目录是源码默认的目录,加入这个参数在 ./configure 可以指定目录;这里可以通过命令来查找出装载的 ini 的位置,当然如果是 cli 模式也可以通过 -c 参数来指定本次执行装载的 php.ini 文件。

1
php -i | grep ini
1
Configuration File (php.ini) Path => /usr/local/php/lib

创建 ini 配置

拷贝一份源码目录下的 php.ini-production 到 etc 目录下

1
cp /opt/php74/php.ini-production /usr/local/php/etc/php.ini

软连接到默认目录

1
ln -s /usr/local/php/etc/php.ini /usr/local/php/lib/php.ini