「PHP」-安装

在 GNU/Linux 下,使用PHP源码编译安装

这里以php-5.6.31为例

#!/bin/bash
./configure   \
    --prefix=/usr/local/php-5.6    \
    --with-config-file-path=/usr/local/php-5.6/etc   \
    --with-config-file-scan-dir=/usr/local/php-5.6/etc/php.ini.d   \
    --localstatedir=/var    \
    --enable-fpm   \
    --with-zlib                \
    --enable-bcmath            \
    --with-bz2                \
    --enable-calendar        \
    --enable-dba=shared        \
    --with-gdbm                \
    --with-gettext        \
    --with-gd                \
    --with-freetype-dir=/usr    \
    --with-jpeg-dir=/usr    \
    --with-png-dir=/usr    \
    --enable-gd-native-ttf  \
    --with-pdo-mysql \
    --with-mysql \
    --with-mysqli   \
    --with-curl

#
# 这只是一个示例,有些扩展单独安装也可以,比如curl、mysql等。
#
# --with-config-file-scan-dir: 指定配置文件的扫描目录,这样可以将ini的配置分模块进行配置,从而可以不、维护一个很长的php.ini文件。
#
# --with-config-file-path: 指定php.ini文件的查找路径,默认查找路径为PREFIX/lib(参考configure --help)
#

在macOS中,编译安装PHP 5.6.35

安装目录为/gnu,PHP 5.6的编译选项如下:

#!/bin/bash
./configure   \
    --prefix=/gnu \
    --localstatedir=/gnu/var \
    --sysconfdir=/gnu/etc/php/5.6 \
    --with-config-file-path=/gnu/etc/php/5.6 \
    --with-config-file-scan-dir=/gnu/etc/php/5.6/conf.d \
    --with-pear=/gnu/share/php/5.6/pear \
    --enable-fpm   \
    --with-zlib                \
    --enable-bcmath            \
    --with-bz2                \
    --enable-calendar        \
    --enable-dba=shared        \
    --with-gdbm                \
    --with-gd                \
    --with-freetype-dir=/usr    \
    --with-jpeg-dir=/usr    \
    --with-png-dir=/usr    \
    --enable-gd-native-ttf  \
    --with-pdo-mysql \
    --with-mysql \
    --with-mysqli   \
    --with-curl

#
# 这只是一个示例,有些扩展单独安装也可以,比如curl、mysql等。
#
# --with-config-file-scan-dir: 指定配置文件的扫描目录,这样可以将ini的配置分模块进行配置,从而可以不、维护一个很长的php.ini文件。
#
# --with-config-file-path: 指定php.ini文件的查找路径,默认查找路径为PREFIX/lib(参考configure --help)
#

下面是Brew中的PHP5.6的配置选项:

#!/bin/bash
./configure                                             \
    --prefix=/usr/local/Cellar/php@5.6/5.6.35_1 \
    --localstatedir=/usr/local/var \
    --sysconfdir=/usr/local/etc/php/5.6 \
    --with-config-file-path=/usr/local/etc/php/5.6 \
    --with-config-file-scan-dir=/usr/local/etc/php/5.6/conf.d \
    --with-pear=/usr/local/Cellar/php@5.6/5.6.35_1/share/php@5.6/pear \
    --enable-bcmath \
    --enable-calendar \
    --enable-dba \
    --enable-exif \
    --enable-ftp \
    --enable-fpm \
    --enable-intl \
    --enable-mbregex \
    --enable-mbstring \
    --enable-mysqlnd \
    --enable-opcache-file \
    --enable-pcntl \
    --enable-phpdbg \
    --enable-phpdbg-webhelper \
    --enable-shmop \
    --enable-soap \
    --enable-sockets \
    --enable-sysvmsg \
    --enable-sysvsem \
    --enable-sysvshm \
    --enable-wddx \
    --enable-zip \
    --with-apxs2=/usr/local/opt/httpd/bin/apxs \
    --with-bz2 \
    --with-fpm-user=_www \
    --with-fpm-group=_www \
    --with-freetype-dir=/usr/local/opt/freetype \
    --with-gd \
    --with-gettext=/usr/local/opt/gettext \
    --with-gmp=/usr/local/opt/gmp \
    --with-icu-dir=/usr/local/opt/icu4c \
    --with-jpeg-dir=/usr/local/opt/jpeg \
    --with-kerberos \
    --with-layout=GNU \
    --with-ldap \
    --with-ldap-sasl \
    --with-libedit \
    --with-libzip \
    --with-mhash \
    --with-mysql-sock=/tmp/mysql.sock \
    --with-mysqli=mysqlnd \
    --with-mysql=mysqlnd \
    --with-mcrypt=/usr/local/opt/mcrypt \
    --with-ndbm \
    --with-openssl=/usr/local/opt/openssl \
    --with-pdo-dblib=/usr/local/opt/freetds \
    --with-pdo-mysql=mysqlnd \
    --with-pdo-odbc=unixODBC,/usr/local/opt/unixodbc \
    --with-pdo-pgsql=/usr/local/opt/libpq \
    --with-pgsql=/usr/local/opt/libpq \
    --with-pic \
    --with-png-dir=/usr/local/opt/libpng \
    --with-pspell=/usr/local/opt/aspell \
    --with-unixODBC=/usr/local/opt/unixodbc \
    --with-webp-dir=/usr/local/opt/webp \
    --with-xmlrpc \
    --with-xsl \
    --with-zlib \
    --with-curl CC=clang CPPFLAGS=-DU_USING_ICU_NAMESPACE=1 CXX=clang++

# 注意事项
# 1. 该编译选项参考自从brew中安装的php-5.6的php-config命令的输出。
# 2. 某些选项进行了调整。

安装的可执行程序

分为两部分,以部分位于bin目录下,一部分位于sbin目录下。

安装的可执行程序

分为两部分,以部分位于bin目录下,一部分位于sbin目录下:

bin
├── pear				# PEAR扩展安装工具。这是一个SHELL脚本。
├── peardev				# pear命令的包装,除去了普通配置的内存限制。这是一个SHELL脚本。
├── pecl				# PECL扩展下载及开发工具。这是一个SHELL脚本。
├── phar -> phar.phar
├── phar.phar
├── php					#
├── php-cgi
├── php-config				# SHELL脚本,用户获取已经安装的PHP的配置信息。
├── phpdbg
└── phpize				# phpize命令用于准备PHP扩展的构建环境。

sbin
└── php-fpm				# 用于PHP的简单又强大的FastCGI进程管理器。

pear | PEAR扩展安装工具。这是一个SHELL脚本。
peardev | pear命令的包装,除去了普通配置的内存限制。这是一个SHELL脚本。
pecl | PECL扩展下载及开发工具。这是一个SHELL脚本。
phar -> phar.phar
phar.phar
php #
php-cgi
php-config | SHELL脚本,用户获取已经安装的PHP的配置信息。
phpdbg
phpize | phpize命令用于准备PHP扩展的构建环境。
php-fpm | 用于PHP的简单又强大的FastCGI进程管理器。

编译中的注意事项

#1 关于–with-config-file-path

编译的时候如果未加入

–with-config-file-path=/x/xx/x

启动php-fpm时,需要使用-c指定php.ini文件的位置;(02/23/2017,目前的php版本在7.0.0以下,7.0.0+未测试)

#2 关于编译

最好可以在源码目录外创建build文件夹来编译,不要在源码目录内直接configure。

# 关于OpenSSL

编译时如果指定了–with-openssl选项时,可以将OpenSSL编译到PHP中。或者源码目录下的ext目录中单独以模块的方式编译安装OpenSSL。我一般选择后者以模块的方式安装OpenSSL,注意原因是因为使用–with-openssl时会出现各种问题。

常见的编译及安装错误

#1 undefined reference to `libiconv’

这个问题出现在php-5.4.45的编译中。
推荐解决办法:make ZEND_EXTRA_LIBS=’-liconv’
解决办法:修改Makefile, 在EXTRA_LIBS中添加-liconv

EXTRA_LIBS = -lcrypt -lz -lcrypt -lrt -lmysqlclient -lmcrypt -lldap -llber -lfreetype -lpng -lz -ljpeg -lcurl -lz -lrt \
	-lm -ldl -lnsl -lrt -lxml2 -lz -lm -lssl -lcrypto -ldl -lz -lcurl -ldl -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err \
	-lidn -lssl -lcrypto -lz -lxml2 -lz -lm -lssl -lcrypto -ldl -lz -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt -lxml2 -lz \
	-lm -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt -liconv

#2 configure: error: DBA: Could not find necessary header file(s)

问题原因:没有安装GDBM
解决办法:安装GDBM,或者头文件

#!/bin/bash

# Kali GNU/Linux Rolling
apt-get install libgdbm-dev

#3 configure: error: Please reinstall the libcurl distribution

checking for cURL in default path… not found
configure: error: Please reinstall the libcurl distribution –

easy.h should be in <curl-dir>/include/curl/

OS: Kali GNU/Linux Rolling

问题原因:Debian中,cURL库的头文件位于/usr/include/x86_64-linux-gnu/curl;还有的原因可能是没有安装libcurl库的头文件。

解决办法:建立到include下的curl软连接:

#!/bin/bash

ln -s /usr/include/x86_64-linux-gnu/curl /usr/include/curl

#4 configure: error: Cannot find OpenSSL’s <evp.h>

问题原因:SSL头文件版本和SSL库版本不一致。我使用的发行版中,提供了两个版本的libssl:libssl1.0.2libssl1.1;头文件也是两套:libssl1.0-dev、libssl-dev
解决办法:统一库和头文件的版本

#!/bin/bash

apt-get install libssl1.1 libssl-dev

#5 easy.h should be in <curl-dir>/include/curl/

checking for cURL support… yes
checking for cURL in default path… not found
configure: error: Please reinstall the libcurl distribution –

easy.h should be in <curl-dir>/include/curl/

系统环境:Kali GNU/Linux Rolling
问题原因:在系统中,curl头文件的路径位于/usr/include/x86_64-linux-gnu/curl,而编译时要求在/usr/include/curl中。
解决办法方案:ln -s /usr/include/x86_64-linux-gnu/curl /usr/include/curl