编译安装本身,应该不存在特别的技术含量,仅以此做个记录。
环境及要求
- 目标运行环境为centos 7 64位,固定 /opt/runtime目录
- PHP-FPM(--enable-fpm激活)
- OpenSSL (--with-openssl[=DIR] )
- PDO(--with-pdo-mysql[=DIR])
- Mbstring(--enable-mbstring 激活)
- Tokenizer(默认激活)
- XML(默认激活)
- Zlib(--with-zlib)
- Zip (-enable-zip)
- ImageMagick扩展
- Memcached扩展
下载PHP
wget http://cn2.php.net/get/php-7.1.6.tar.gz/from/this/mirror -O php-7.1.6.tar.gz
编译软件安装
sudo yum install gcc -y
sudo yum install autoconf -y
sudo yum install gcc-c++ -y
安装过程
测试一下先
tar xvfz php-7.1.6.tar.gz
cd php-7.1.6
./configure --prefix=/opt/runtime/php --enable-mbstring --with-openssl --with-zlib
检测到libxml2未安装,停止了。
这一点也挺好,检测到错误,立即停止。而非检测所有错误,一次性返回。
这个检测到安装可能要重复好多次,缺啥补啥,没啥好说的。
sudo yum install libxml2-devel -y 安装 libxml2-config
再次测试已通过,看到 Thank you for using PHP.
字样
需要yum安装的程序集合
sudo yum install libxml2-devel -y
sudo yum install openssl-devel -y
sudo yum install curl-devel -y
sudo yum install ncurses-devel -y
sudo yum install libtool-ltdl-devel -y
sudo yum install libpng-devel -y
sudo yum install libjpeg-turbo-devel -y
到这里,基本可以编译通过了
./configure --prefix=/opt/runtime/php --enable-mbstring --with-openssl --with-zlib --with-pdo-mysql --enable-fpm --enable-soap --with-curl --enable-soap --enable-zip && make && make install
安装PHP完成后,Memcached和ImageMagick使用PECL安装
安装Memcached
先把环境变量加一下
export PATH=$PATH:/opt/runtime/php/bin
使用pecl install memcached
尝试安装,发现需要libmemcached
,其实这是基本常识了。
所以先安装libmemcached
,同样也是安装到 /opt/runtime
目录下
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar xvfz libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure --prefix=/opt/runtime/libmemcached
make && make install
由于再次使用pecl install memcached
安装提示一个memcached-sasl
错误,所以动手安装吧,命令如下:
pecl download memcached
tar xvz memcached-3.0.3.tgz
cd memcached-3.0.3
phpize
./configure --with-libmemcached-dir=/opt/runtime/libmemcached --disable-memcached-sasl
make && make install
最终提示Installing shared extensions: /opt/runtime/php/lib/php/extensions/no-debug-non-zts-20160303/
,扩展安装成功,配置一下php.ini
应该就完成了。
不过PHP的安装目录,并没有ini文件,需要从源码目录里复制。
cd php-7.1.6
cp php.ini-production /opt/runtime/php/lib/php.ini
添加如下配置至ini文件
extension=memcached.so
检查扩展:
$ php -i | grep memcached
memcached
memcached support => enabled
libmemcached version => 1.0.18
memcached.compression_factor => 1.3 => 1.3
memcached.compression_threshold => 2000 => 2000
memcached.compression_type => fastlz => fastlz
memcached.default_binary_protocol => 0 => 0
memcached.default_connect_timeout => 0 => 0
memcached.default_consistent_hash => 0 => 0
memcached.serializer => php => php
memcached.sess_binary_protocol => 1 => 1
memcached.sess_connect_timeout => 0 => 0
memcached.sess_consistent_hash => 1 => 1
memcached.sess_lock_expire => 0 => 0
OK,告一段落
安装ImageMagick
$ pecl install imagick
...
checking Testing /usr/bin/MagickWand-config... Doesn't exist
checking Testing /usr/sbin/bin/MagickWand-config... Doesn't exist
checking Testing /opt/bin/MagickWand-config... Doesn't exist
checking Testing /opt/local/bin/MagickWand-config... Doesn't exist
configure: error: not found. Please provide a path to MagickWand-config or Wand-config program.
ERROR: `/tmp/pear/temp/imagick/configure --with-php-config=/opt/runtime/php/bin/php-config --with-imagick' failed
大概意思就是需要指定imageick程序目录吧,ok,安装imagemagick先吧,依然是采用源码安装,一路绿灯,命令如下:
wget https://www.imagemagick.org/download/ImageMagick.tar.gz
tar xvfz ImageMagick.tar.gz
cd ImageMagick-7.0.6-0
./configure --prefix=/opt/runtime/ImageMagick
make && make install
再次使用pecl命令安装,需要手动输入ImageMagick的安装目录,同时,屏幕输出Build process completed successfully
字样,即安装成功,再配置php.ini
即可
$ pecl install imagick
WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
downloading imagick-3.4.3.tgz ...
Starting to download imagick-3.4.3.tgz (245,410 bytes)
........................................done: 245,410 bytes
19 source files, building
running: phpize
Configuring for:
PHP Api Version: 20160303
Zend Module Api No: 20160303
Zend Extension Api No: 320160303
Please provide the prefix of Imagemagick installation [autodetect] : /opt/runtime/ImageMagick
...
...
...
Build process completed successfully
Installing '/opt/runtime/php/lib/php/extensions/no-debug-non-zts-20160303/imagick.so'
Installing '/opt/runtime/php/include/php/ext/imagick/php_imagick_shared.h'
install ok: channel://pecl.php.net/imagick-3.4.3
configuration option "php_ini" is not set to php.ini location
You should add "extension=imagick.so" to php.ini
配置后的``php.ini```
...
extension=memcached.so
extension=imagick.so
zend_extension=opcache.so
...
完成
后记1
回应文章开头,整个过程没有任何难点,缺库缺程序有各类提示,搜索一下,基本可以解决。也可能是因为本人是第二次手动安装吧。
整个过程,其实应该可以通过一个脚本,自动化的去完成。如果自己维护部署环境,环境也是需要升级的嘛。
后记2 postgresql
php configure的时候,--with-pdo-mysql 可以直接通过,但是--with-pdo-pgsql却会报错,难道是让我先安装postgresql?好吧,让我来试试,毕竟postgresql被神话了好多年,看到在选择的问题上,有人回复『用PG ,理由你现在无法理解,只有你真正去做了, 才能体会到』
wget https://ftp.postgresql.org/pub/source/v9.6.3/postgresql-9.6.3.tar.gz
源码下载的好慢啊,deng...然后依然是老套路
tar xvfz postgresql-9.6.3.tar.gz
cd postgresql-9.6.3
./configure --prefix=/opt/runtime/postgresql --without-readline
为什么要加without-readline? 不加报错啊
然后重新编一下php喽
./configure --prefix=/opt/runtime/php --enable-mbstring --with-openssl --with-zlib --with-pdo-mysql --enable-fpm --enable-soap --with-curl --enable-soap --enable-zip --with-pdo-pgsql=/opt/runtime/postgresql
make && make install
再把刚才编译完成的imagick.so和memcached.so复制到新的php扩展目录,即/opt/runtime/php/lib/php/extensions/no-debug-non-zts-20160303/
。 当然,你要先备份出来,才能复制。
发现一个令人发指的事情,pgsql程序,才占用24M的磁盘空间。相比较mysql(包括mariadb)动辙几百MB的空间占用,突然有想转向pgsql的意向,你们呢~~
$ du -sh /opt/runtime/*
75M /opt/runtime/ImageMagick
7.3M /opt/runtime/libmemcached
117M /opt/runtime/php
24M /opt/runtime/postgresql