编译php

1. 准备

不看原因可直接看最后命令
i, 编译php前先安装以下类库(括号为不装的后果):
libxml2-dev
(configure: error: libxml2 not found. Please check your libxml2 installation.)

libssl-dev (–with-openssl需要)
(configure: error: Cannot find OpenSSL’s )

64位系统可能把so文件放在了/usr/lib/x86_64-linux-gnu/libssl.so路径下,需要建立软链接到上一层目录
ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib/
(configure: error: Cannot find OpenSSL’s libraries)

libcurl4-gnutls-dev(–with-curl需要)
同样的,64位系统可能把curl文件夹放在了/usr/include/x86_64-linux-gnu/curl路径下,需要建立软链接到上一层目录
ln -s /usr/include/x86_64-linux-gnu/curl /usr/include/
(configure: error: Please reinstall the libcurl distribution – easy.h should be in /include/curl/)

libpng-dev
(configure: error: png.h not found.)

libfreetype6-dev(–with-freetype-dir需要)
安装后仍然报错
(configure: error: freetype-config not found.)
这是因为libfreetype6-dev在新版本中,freetype-config 被替代成 pkg-config。
php 7.4 以上不用自己编译
不能偷懒就先去编译freetype。
下载源代码(https://download.savannah.gnu.org/releases/freetype/)
./configure –prefix=/opt/freetype-2.10.0 –enable-freetype-config
make && make install
安装完成后,编译php时指定目录(–with-freetype-dir=/opt/freetype-2.10.0)

apt-get install -y libxml2-dev libssl-dev libcurl4-gnutls-dev libpng-dev
centos系统包名不一样:
yum install -y libxml2-devel openssl-devel libcurl-devel libpng-devel freetype-devel
(centos系统不用自己编译freetype,所以编译php时也无需指定目录)

ii, 下载php源码:
https://www.php.net/downloads.php

iii, php7.2以上新版本的其他报错
(The pkg-config script could not be found or is too old)
解决:apt-get install pkg-config
(Package requirements (sqlite3 > 3.7.4) were not met: No package ‘sqlite3’ found)
解决:apt-get install libsqlite3-dev
(Package requirements (oniguruma) were not met: No package ‘oniguruma’ found)
解决:apt-get install libonig-dev

2. 编译
php常用选项:
--enable-fpm            Enable building of the fpm SAPI executable
--enable-bcmath         Enable bc style precision math functions
--with-gd=DIR           Include GD support.  DIR is the GD library base
                        install directory BUNDLED
--with-freetype-dir=DIR GD: Set the path to FreeType 2 install prefix
--enable-mbstring       Enable multibyte string support
--with-mysqli=FILE      Include MySQLi support.  FILE is the path
                        to mysql_config.  If no value or mysqlnd is passed
--with-pdo-mysql=DIR    PDO: MySQL support. DIR is the MySQL base directory
                        If no value or mysqlnd is passed as DIR, the
                        MySQL native driver will be used
--enable-mysqlnd        Enable mysqlnd explicitly, will be done implicitly
                        when required by other extensions
--with-zlib=DIR         Include ZLIB support (requires zlib >= 1.2.0.4) (可加速composer)


(有用到curl)
--with-openssl=DIR      Include OpenSSL support (requires OpenSSL >= 1.0.1)
--with-curl=DIR         Include cURL support

(swoole方向)
--enable-sockets        Enable sockets support
--enable-pcntl          Enable pcntl support (CLI/CGI only)

全部用上:
kali@kali:/opt/source/php-7.2.34$ ./configure --prefix=/opt/php-7.2.34 \
> --enable-fpm \
> --enable-bcmath \
> --with-gd \
> --with-freetype-dir \
> --enable-mbstring \
> --with-mysqli \
> --with-pdo-mysql \
> --enable-mysqlnd \
> --with-zlib \
> --with-openssl \
> --with-curl \
> --enable-sockets \
> --enable-pcntl

./configure --prefix=/opt/php-7.2.34 --enable-fpm --enable-bcmath --with-gd --with-freetype-dir=/opt/freetype-2.10.0 --enable-mbstring --with-mysqli --with-pdo-mysql --enable-mysqlnd --with-zlib --with-openssl --with-curl --enable-sockets --enable-pcntl

make && make install

php7.4 configure 参数改变:
–with-gd => –enable-gd
–with-freetype-dir => –with-freetype

php7.4 的 configure 命令:
./configure --prefix=/opt/php-7.4.28 --enable-fpm --enable-bcmath --enable-gd --with-freetype --enable-mbstring --with-mysqli --with-pdo-mysql --enable-mysqlnd --with-zlib --with-openssl --with-curl --enable-sockets --enable-pcntl

安装好之后,复制配置文件到正确位置。
cp /opt/source/php-7.2.34/php.ini-production /opt/php-7.2.34/lib/php.ini
mv /opt/php-7.2.34/etc/php-fpm.conf.default /opt/php-7.2.34/etc/php-fpm.conf
mv /opt/php-7.2.34/etc/php-fpm.d/www.conf.default /opt/php-7.2.34/etc/php-fpm.d/www.conf

将 php.ini 文件中的配置项 cgi.fix_pathinfo 设置为 0 。

修改 /opt/php-7.2.34/etc/php-fpm.d/www.conf 配置文件,确保 php-fpm 模块使用 www-data 用户和 www-data 用户组的身份运行。

编译phpredis扩展

使用 phpize 需要安装 autoconf
apt-get install autoconf
下载源代码(https://github.com/phpredis/phpredis/releases)
注意,phpredis 5 版本弃用了 Redis::delete(),检查代码中是否兼容。不兼容安装5以下版本。
/opt/php-7.2.34/bin/phpize
./configure –with-php-config=/opt/php-7.2.34/bin/php-config
make && make install
php.ini文件开启:extension=redis.so
检查是否开启:/opt/php-7.2.34/bin/php –ri redis

编译swoole扩展

下载源代码(https://github.com/swoole/swoole-src/releases)
/opt/php-7.2.34/bin/phpize
./configure –with-php-config=/opt/php-7.2.34/bin/php-config
make && make install
php.ini文件开启:extension=swoole.so
检查是否开启:/opt/php-7.2.34/bin/php –ri swoole

其他东西

开启log:
php-fpm.conf/www.conf:
access.log = /data/logs/php/$pool.access.log

坑1:
php返回空白页面,检查nginx配置
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

其他:
加到PATH:
vim /etc/profile
export PATH=”/opt/php-7.2.34/bin:$PATH”
source /etc/profile
安装composer:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

留下评论

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