0%

php扩展库安装

本文演示编译安装php7.2.5,并在此基础上演示使用peclphpize安装扩展库。

编译安装php

请先下载好源码包,复制以下代码为install.sh,直接执行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
yum install -y zlib-devel ncurses-devel libxml2-devel libstdc++-devel libxml2-devel libmcrypt mhash libevent openssl-devel libcurl-devel mhash-devel libmcrypt-devel

### libiconv-1.16, need by php
cd $dir
tar -xzf libiconv-1.16.tar.gz
cd libiconv-1.16
./configure --prefix=/usr/local/libiconv-1.16
make -j `grep 'processor' /proc/cpuinfo | wc -l`
make install

# add ld.so.conf
echo "" > /etc/ld.so.conf.d/libiconv-1.16.conf
[ -d /usr/local/libiconv-1.16/lib/ ] && echo "/usr/local/libiconv-1.16/lib/" >> /etc/ld.so.conf.d/libiconv-1.16.conf
[ -d /usr/local/libiconv-1.16/lib64/ ] && echo "/usr/local/libiconv-1.16/lib64/" >> /etc/ld.so.conf.d/libiconv-1.16.conf
ldconfig

### freetype-2.10.0, need by php and gd
cd $dir
tar -xjf freetype-2.10.0.tar.bz2
cd freetype-2.10.0
./configure --prefix=/usr/local/freetype-2.10.0 --enable-freetype-config
make -j `grep 'processor' /proc/cpuinfo | wc -l`
make install

### jpegsrc.v9c, need by php and gd
cd $dir
tar -xzf jpegsrc.v9c.tar.gz
cd jpeg-9c/
./configure --prefix=/usr/local/jpeg-9c
make -j `grep 'processor' /proc/cpuinfo | wc -l`
make install

### libpng-1.6.3, need by php and gd
cd $dir
tar -xJf libpng-1.6.37.tar.xz
cd libpng-1.6.37
./configure --prefix=/usr/local/libpng-1.6.37
make -j `grep 'processor' /proc/cpuinfo | wc -l`
make install

### php7.2.5
cd $dir
tar -xzf php-7.2.5.tar.gz
cd php-7.2.5
./configure --prefix=/usr/local/webserver/php-7.2.5 --with-config-file-path=/usr/local/webserver/php-7.2.5/etc --with-config-file-scan-dir=/usr/local/webserver/php-7.2.5/etc/php.d/ --with-freetype-dir=/usr/local/freetype-2.10.0 --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib --enable-shared --with-curl --with-jpeg-dir=/usr/local/jpeg-9c --with-png-dir=/usr/local/libpng-1.6.37 --enable-mbstring --enable-fpm --with-libxml-dir --with-openssl --enable-sockets --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-pcntl --with-iconv-dir=/usr/local/libiconv-1.16 --with-mhash
make ZEND_EXTRA_LIBS='-liconv' -j `grep 'processor' /proc/cpuinfo | wc -l`
make install

# copy service script
if [ -f '/usr/bin/systemctl' ];then
cp -a sapi/fpm/php-fpm.service /usr/lib/systemd/system/
systemctl daemon-reload
else
cp -a sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
fi

需要注意的是,编译安装后,在源码目录的sapi/fpm子目录下,会自动生成配置管理文件,init.d.php-fpm php-fpm.service和默认配置文件。

php扩展库

  • 静态
  • 扩展库
    • pecl安装
    • phpize安装

静态库指的是在源码包里面ext目录下的库。实际上可以将扩展库放置到ext目录,强制编译为静态库,详细请查看文章末尾参考地址。

1
2
3
4
5
6
7
8
# ls php-7.2.5/ext/
bcmath dba filter imap mysqli pcre pdo_pgsql recode soap sysvsem xmlrpc
bz2 dom ftp interbase mysqlnd pdo pdo_sqlite reflection sockets sysvshm xmlwriter
calendar enchant gd intl oci8 pdo_dblib pgsql session sodium tidy xsl
com_dotnet exif gettext json odbc pdo_firebird phar shmop spl tokenizer zend_test
ctype ext_skel gmp ldap opcache pdo_mysql posix simplexml sqlite3 wddx zip
curl ext_skel_win32.php hash libxml openssl pdo_oci pspell skeleton standard xml zlib
date fileinfo iconv mbstring pcntl pdo_odbc readline snmp sysvmsg xmlreader

扩展库必须经过编译、安装,然后加载。

编译php扩展库时,通常需要各种工具(autoconfautomakelibtool等),php与各工具的依赖说明如下:

  • autoconf
    • PHP 5.4 - 7.1: 2.59+
    • PHP 7.2: 2.64+
    • PHP 7.3: 2.68+
  • libtool: 1.4.x+ (except 1.4.2)
  • re2c: 0.13.4+
  • bison
    • PHP 5.4: 1.28, 1.35, 1.75, 2.0 to 2.6.4
    • PHP 5.5 and 5.6: 2.4 to 2.7
    • PHP 7.0 - 7.3: 2.4+
    • PHP 7.4: 3.0.0+

最新依赖说明请参考https://www.php.net/git.php

pear与pecl

pecl安装扩展库

peclpear命令使用方法几乎一模一样。

1
2
3
4
5
6
pecl help  #获取帮助
pecl install extname #在线安装extname扩展库
pecl install extname-version #安装特定版本extname扩展库
pecl list-channels #获取远程频道列表,即获取扩展库源码的地址
pecl config-show #打印配置列表
pecl config-get default_channel #打印当前使用的频道,默认为pecl.php.net

下面演示安装redis扩展库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# ./pecl install redis
WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
downloading redis-5.0.2.tgz ...
Starting to download redis-5.0.2.tgz (243,270 bytes)
..................................................done: 243,270 bytes
25 source files, building
running: phpize
Configuring for:
PHP Api Version: 20170718
Zend Module Api No: 20170718
Zend Extension Api No: 320170718
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

ERROR: `phpize' failed

缺少autoconf工具:

1
# yum install m4 autoconf

重新在线安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# ./pecl install redis
WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
downloading redis-5.0.2.tgz ...
Starting to download redis-5.0.2.tgz (243,270 bytes)
..................................................done: 243,270 bytes
25 source files, building
running: phpize
Configuring for:
PHP Api Version: 20170718
Zend Module Api No: 20170718
Zend Extension Api No: 320170718
enable igbinary serializer support? [no] :
enable lzf compression support? [no] :
...
Build process completed successfully
Installing '/usr/local/webserver/php-7.2.5/lib/php/extensions/no-debug-non-zts-20170718/redis.so'
install ok: channel://pecl.php.net/redis-5.0.2
configuration option "php_ini" is not set to php.ini location
You should add "extension=redis.so" to php.ini

php.ini添加extension=redis.so,重启php-fpm服务生效。

phpize安装扩展库

phpize可能是我们最常用的扩展库安装方法,当服务器无法出外网时,这种安装方式很有用。

1
2
3
4
5
$ cd extname
$ phpize
$ ./configure #多数情况下,需要跟上--with-php-config=选项
$ make
# make install

下面演示phpize安装redis扩展,扩展库包可以在pecl站点获取。

1
2
3
4
5
6
tar -xzf redis-5.0.0.tgz
cd redis-5.0.0
/usr/local/webserver/php-7.2.5/bin/phpize
./configure --with-php-config=/usr/local/webserver/php-7.2.5/bin/php-config
make -j `grep 'processor' /proc/cpuinfo | wc -l`
make install

php.ini添加extension=redis.so,重启php-fpm服务生效。

有价值的技巧

php启动时一定会读取php.ini吗?

配置文件(php.ini)会在php启动时被读取。对于服务器模块版本的php,仅在web服务器启动时读取一次;而CGI CLI版本,每次调用都会读取。

注意,如果存在php-SAPI.ini(SAPI为当前所用的SAPI名称,如php-cli.ini),则会替代php.ini SAPI的名称可以通过php_sapi_name()函数来获取。

php.ini配置列表

https://www.php.net/manual/zh/ini.list.php

PHP_INI_ALL表示可以在任何地方被设定,甚至可以在代码中使用ini_set()函数来设定。

参考:https://www.php.net/manual/zh/install.pecl.php
php编译安装请参考我的githubhttps://github.com/fredme/shell_scripts/tree/master/php7