源码安装php
首先,源码安装php,如果只是单纯的安装php,那么应该非常简单。本文,只是简单的叙述一下。如果需要使用到pthtread扩展,需要编译php时,添加参数--enable-maintainer-zts,没有使用该参数编译,则需要重新编译才能使用。
参考资源
仓库主要将如何编译安装php-ngx,本文从发点,也是安装该仓库。但是,实际上,并未安装完,而且到了安装nginx阶段,还报了一个错误,暂时搁浅。
文章指出了,安装php需要安装扩展,针对centos,则
yum install libxml2-devel
如果不安装(实际上系统里面安装有libxml2扩展),则会提示
configure: error: libxml2 not found. Please check your libxml2 installation.
编译安装
1、安装php
需要先安装前面讲的扩展。再执行下面。
wget 'http://php.net/distributions/php-7.3.10.tar.gz'
tar xf php-7.3.10.tar.gz
cd php-7.3.10
./configure --prefix=/path/to/php --enable-embed
make && make install
说明:
直接下载php的源码,比较慢,我使用了国外的主机进行下载,下载完成后,使用该命令完成复制。
scp -r -P 62222 root@远程主机IP:/root/php .
上述命令,由于拷贝整个文件夹,所以,就加了 -r参数,如果单个文件,则不需要添加该命令。另外-P 是指定远程的ssh端口。
实际上,configure命令,要跟上一大堆的配置,方便自己使用。
常用的配置选项
PHP 选项
–prefix[=PREFIX]:安装路径的前缀,可以自定义,例如 /tdata/soft/php。指定prefix,可执行文件默认放在/usr/local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr/local/share,很凌乱。指定 prefix,直接删掉一个文件夹就够了。
–with-config-file-path=PATH:设置 php.ini 的搜索路径。默认为 PREFIX/lib。
–disable-short-tags:默认禁用短形式的开始标签 <? 。PHP 扩展
每个扩展对应的选项,需要在具体的扩展文档里找。大部分可以安装 PHP 之后再安装。
–enable-mbstring:开启 mbstring 多字节扩展
–with-gd[=DIR]:激活 GD 支持,可以指定扩展位置。编译 GD 库需要libpng 和 libjpeg。建议通过 sudo apt-get install php7.2-gd 或 yum install php72w-gt 安装。
–with-pear:安装 PEAR 扩展。
–with-zip[=DIR]:提供 zip 支持,[DIR]是 ZZIPlib 库安装路径。建议通过 通过 PECL 安装。
进程控制扩展:
–enable-pcntl:开启 PCNTL 进程控制扩展,只能编译安装。
网络相关扩展:
–with-openssl[=DIR]:开启 OpenSSL 扩展,可以指定扩展位置
–enable-ftp:开启 FTP 扩展
–with-curl:支持 cURL
–enable-sockets:开启 socket 扩展
–enable-soap:支持 SOAP
–enable-fpm:激活 FPM 支持
数据库扩展:
–with-mysql=mysqlnd
–with-mysqli=mysqlnd
–with-pdo-mysql=mysqlnd
–with-mysql=/usr/bin/mysql_config
–with-mysqli=mysqlnd
–with-pdo-mysql=mysqlnd
韵筹脚本服务器
版本7.3
版本7.3,因于老版本共存,所以目录就安装到了/usr/local/php73。否则,可以将php73替换成php
./configure \
--prefix=/usr/local/php73 \
--with-config-file-path=/usr/local/php73/etc \
--with-config-file-scan-dir=/usr/local/php73/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir=/usr/local/freetype \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-ftp \
--with-gd \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--without-libzip \
--enable-soap \
--with-gettext \
--enable-opcache
备注,在一台机器上,下面的依赖不用安装,也能成功。但是,在某台生产上的机器上,需要自己安装以下依赖,才行。
依赖总结:
yum install -y libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel
依赖一:
If configure fails try --with-webp-dir=<DIR>
configure: error: jpeglib.h not found.
# 安装如下依赖
yum install libjpeg libjpeg-devel
依赖二:
checking for jpeg_read_header in -ljpeg... yes
configure: error: png.h not found.
# 安装如下依赖
yum install -y libpng libpng-devel
依赖三:
checking for png_write_image in -lpng... yes
If configure fails try --with-xpm-dir=<DIR>
configure: error: freetype-config not found.
yum install -y freetype freetype-devel
默认安装的php5.6
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir=/usr/local/freetype \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--enable-ftp \
--with-gd \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--without-libzip \
--enable-soap \
--with-gettext \
--enable-opcache \
--with-xsl \
--with-pear \
phpstudy的配置
cscript /nologo configure.js "--enable-snapshot-build" "--enable-debug-pack" "--disable-zts" "--with-pdo-oci=c:\php-snap-build\deps_aux\oracle\x64\instantclient_12_1\sdk,shared" "--with-oci8-12c=c:\php-snap-build\deps_aux\oracle\x64\instantclient_12_1\sdk,shared" "--enable-object-out-dir=../obj/" "--enable-com-dotnet=shared" "--without-analyzer" "--with-pgo"
扩展安装
经常需要安装php的各种扩展,其实,扩展的编写是严格按照php规范来写的,否则,它也无法运行。所以,所有的扩展的安装基本上都一个样,没有差别。
套路如下:
# 进入到插件源码根目录
cd /src
/usr/local/php/bin/phpize
# 如果有其他参数,则加上即可。
./configure --with-php-config=/usr/local/php/bin/php-config
pdo_pgsql
该扩展安装也不复杂,可能需要提前安装
postgresql-devel扩展。剩下的就是常规操作。另外,该扩展其实是php源码提供的,可能是在安装php的时候,并没有一道安装,所以,从php源码,就能提取到pdo_pgsql.zip 安装包。
报如下错误报错:
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... gawk
checking for PostgreSQL support for PDO... yes, shared
checking for pg_config... not found
configure: error: Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path
则执行下面的语句:
yum -y install postgresql-devel
再正常安装:
# 上传到临时目录,解压。
unzip pdo_pgsql.zip
cd pdo_pgsql/
# 确定php版本及安装目录
php -v
ls /usr/local/php73/bin/
# 执行以下命令安装,假入上面ls执行,有结果,而且php版本为7.3
/usr/local/php73/bin/phpize
./configure --with-php-config=/usr/local/php73/bin/php-config
make && make install
在php.ini中开启该扩展
假入上一步安装成功后,记住提示的目录:
vi /usr/local/php73/etc/php.ini
加入如下内容:
extension=上一步安装成功的目录/pdo_pgsql.so
清理安装文件:
cd ..
rm -rf pdo_pgsql/
rm pdo_pgsql.zip
pthreads
到pecl下下载,源码安装,没有什么特别的。
问题
安装php8(2020-11-30 15:14)遇到了下列各种问题,解决如下:
一台机器卡在无法yum install oniguruma-devel,(按抢占模式购买的阿里云服务器centos8.2)。
另外一台机器,卡在configure: error: iconv does not support errno。真是郁闷。(腾讯云服务器)
所以,lnmp一键脚本真的是牛b。
后来,在自己的vps(centos8.2),成功的.configure成功,基本上等于要安装成功了,结果,可能由于vps只有500M内存的原因 ,结果在make 过程中报错。
另外,按php7.3的configure命令配置,结果提示有好几个命令,不识别,即不需要再配置了。
- 错误1:
configure: error: Package requirements (libcurl >= 7.29.0) were not met:
Package 'libcurl', required by 'virtual:world', not found
安装
yum install -y libcurl-devel
- 错误2 :
描述类似于错误1,缺失sqlite3
yum install sqlite-devel
# 或
sudo apt-get install libsqlite3-dev
- 错误3:
configure: error: Package requirements (icu-uc >= 50.1 icu-io icu-i18n) were not met:
Package 'icu-uc', required by 'virtual:world', not found
Package 'icu-io', required by 'virtual:world', not found
Package 'icu-i18n', required by 'virtual:world', not found
安装
yum install libicu-devel
- 错误4:
configure: error: C++ preprocessor "/lib/cpp" fails sanity check
安装:
yum -y install gcc-c++
- 错误5:
configure: error: Package requirements (oniguruma) were not met:
Package 'oniguruma', required by 'virtual:world', not found
安装:
yum install oniguruma oniguruma-devel
大概还会遇到无法安装oniguruma-devel的错误,针对centos8,则可以使用
yum config-manager --set-enabled PowerTools
yum -y install oniguruma oniguruma-devel
然后,即可安装成功。
https://www.24kplus.com/linux/1614.html 参见文章。
oniguruma这个是一个正则的库。
错误6:
安装的vps内存太小,出如下报错:
compilation terminated.
make: *** [Makefile:904: ext/fileinfo/libmagic/apprentice.lo] Error 1
是因为服务器内存不足1G。
只需要在配置命令中添加 –disable-fileinfo即可 。
总结,先安装常用扩展:
yum config-manager --set-enabled PowerTools
yum install -y \
libxml2-devel \
libcurl-devel \
sqlite-devel \
libicu-devel \
gcc-c++ \
oniguruma oniguruma-devel
常见错误
解决 error: the HTTP XSLT module requires the libxml2/libxslt 错误
yum -y install libxml2 libxml2-dev
yum -y install libxslt-devel
解决 error: the HTTP image filter module requires the GD library. 错误
yum -y install gd-devel
解决 error: the GeoIP module requires the GeoIP library. 错误
yum -y install GeoIP GeoIP-devel GeoIP-data
解决 error: the Google perftools module requires the Google perftools 错误
yum -y install gperftools
解决 error: libatomic_ops library was not found. 错误
yum -y install libuuid-devel libblkid-devel libudev-devel fuse-devel libedit-devel libatomic_ops-devel
解决 error trying to exec 'cc1plus': execvp: No such file or directory 错误
yum -y install gcc-c++
解决 error: [pool www] cannot get uid for user 'www-data' 错误
groupadd www-data
useradd -g www-data www-data
解决configure: error: mbed TLS libraries not found. 错误。
需要安装mbedtls,教程:https://www.24kplus.com/linux/281.html
解决 error: Cannot find OpenSSL's 错误
yum install openssl openssl-devel
ln -s /usr/lib64/libssl.so /usr/lib/
解决 error: Libtool library used but 'LIBTOOL' is undefined 错误
yum install libtool
解决 exec: g++: not found 错误
yum -y update gcc
yum -y install gcc+ gcc-c++
解决 configure: error: tss lib not found: libtspi.so 错误
yum install trousers-devel
解决 Can't exec "autopoint": No such file or directory 错误
yum install gettext gettext-devel gettext-common-devel
解决 configure: error: libcrypto not found. 错误
yum remove openssl-devel
yum -y install openssl-devel
解决 configure: error: Package requirements (libffi >= 3.0.0) were not met: No package 'libffi' found 错误
yum install libffi-devel
解决 fatal error: uuid.h: No such file or directory 错误
yum install e2fsprogs-devel uuid-devel libuuid-devel
解决 configure: error: openssl lib not found: libcrypto.so 错误
yum install openssl-devel
解决 tar (child): lbzip2: Cannot exec: No such file or directory 错误
yum -y install bzip2
解决 configure: error: C++ preprocessor "/lib/cpp" fails sanity check 错误
yum install gcc-c++
解决 configure: error: Please reinstall the BZip2 distribution 错误
yum install bzip2 bzip2-devel
解决 configure: error: cURL version 7.15.5 or later is required to compile php with cURL support 错误
yum install curl-devel
解决 configure: error: not found. Please provide a path to MagickWand-config or Wand-config program 错误
yum install ImageMagick-devel
解决 configure: error: no acceptable C compiler found in $PATH 错误
yum install gcc
解决 configure: error: Package requirements (icu-uc >= 50.1 icu-io icu-i18n) were not met: 错误
yum install libicu-devel
解决 configure: error: Package requirements (sqlite3 > 3.7.4) were not met: No package 'sqlite3' found 错误
yum install sqlite-devel
解决 configure: error: Package requirements (oniguruma) were not met: No package 'oniguruma' found 错误
yum install oniguruma oniguruma-devel