源码编译 PHP 环境

Summary: Author: 张亚飞 | 阅读时间: 4 minute read | Published: 2015-08-08
Filed under Categories: DevOpsTags: Linux, Server, Software, DevOps,


检查系统已安装的 php 软件包

六 6月 16 15:02:52 coam@v.cs.0:~$ yum list installed | grep php
mod_php72w.x86_64              7.2.6-1.w7                       @webtatic
php72w-cli.x86_64              7.2.6-1.w7                       @webtatic
php72w-common.x86_64           7.2.6-1.w7                       @webtatic
php72w-devel.x86_64            7.2.6-1.w7                       @webtatic
php72w-fpm.x86_64              7.2.6-1.w7                       @webtatic
php72w-gd.x86_64               7.2.6-1.w7                       @webtatic
php72w-mbstring.x86_64         7.2.6-1.w7                       @webtatic
php72w-mysqlnd.x86_64          7.2.6-1.w7                       @webtatic
php72w-pdo.x86_64              7.2.6-1.w7                       @webtatic
php72w-pear.noarch             1:1.10.4-1.w7                    @webtatic
php72w-pecl-igbinary.x86_64    2.0.5-1.w7                       @webtatic
php72w-pecl-memcached.x86_64   3.0.4-1.w7                       @webtatic
php72w-process.x86_64          7.2.6-1.w7                       @webtatic
php72w-xml.x86_64              7.2.6-1.w7                       @webtatic

CentOS

sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
sudo yum install -y php72w-common php72w-cli php72w-fpm php72w-devel php72w-pear mod_php72w php72w-gd php72w-mbstring php72w-mcrypt php72w-mysqlnd php72w-xml php72w-pecl-mongodb php72w-pecl-memcached
yum install php-pear
yum install php-devel
sudo yum install gcc
systemctl start php-fpm
systemctl enable php-fpm

#git clone https://github.com/skvadrik/re2c.git re2c
wget https://github.com/skvadrik/re2c/releases/download/1.0.3/re2c-1.0.3.tar.gz
tar -zxvf re2c-1.0.3.tar.gz
cd re2c-1.0.3
./autogen.sh
./configure
make
sudo make install

解决以下警告

configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
#sudo yum install php-mysql gcc libtool pcre-devel

编译安装

sudo git clone --depth=1 git://github.com/phalcon/cphalcon.git
cd cphalcon/build
sudo ./install

安装依赖

yum -y install php72w-pear

编译安装

sudo pecl install mongodb

或源码安装 https://github.com/mongodb/mongo-php-driver 推荐使用以上命令安装解决相关依赖

  • 安装 memcached 扩展
yum install php72w-pecl-memcached
sudo yum install memcached

以下备注,根据需要再另行安装

#sudo apt install libmemcached-dev build-essential
#sudo pecl install memcache

备注:源码编译安装 memcache 扩展方式:

wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar -zxvf memcache-2.2.7.tgz
cd memcache-2.2.7
/usr/local/php/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config
make && make install

安装完成后,会出现 Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/ 将这个地址复制到 php.ini

  • 安装 redis 扩展

源码安装: phpredis/phpredis

sudo wget https://github.com/phpredis/phpredis/archive/4.0.2.tar.gz
tar zxvf 4.0.2.tar.gz
sudo phpize

Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718

sudo ./configure --with-php-config=/usr/bin/php-config
sudo make
sudo make install
  • 以上命令将会生成so文件 /usr/lib/php5/20131226/redis.so

添加到 php.ini 中

extension=redis.so
sudo yum install libzip -y

由于 yum 仓库的 libzip 软件版本较老,导致后续安装 zip 的时候会出现如下错误:

checking for pkg-config... /usr/bin/pkg-config
checking for libzip... not found
configure: error: Please reinstall the libzip distribution

CentOS.7 需要手动编译安装最新版的 libzip

sudo yum remove libzip -y
wget https://libzip.org/download/libzip-1.1.2.tar.gz
tar -zxvf libzip-1.1.2.tar.gz
cd libzip-1.1.2
mkdir build
cd build
sudo cmake ..
make
make install

注意需要升级 cmake

  • 开始安装
wget http://pecl.php.net/get/zip-1.15.2.tgz
tar -zxvf zip-1.15.2.tgz
cd zip-1.15.2
phpize
./configure
make
sudo make install
  • 以上命令将会生成so文件 /usr/lib/php/20151012/zip.so

添加到 php.ini 中

extension=zip.so
  • 注意: 在 CentOS 中通过以上方式安装 zip 死活加载不出来, /usr/lib64/php/modules/zip.so 生成了也没有,后来通过以下方式安装才通过,可能是 zip 与 php7.2 版本不兼容吧.
wget http://php.net/get/php-7.2.6.tar.gz/from/this/mirror
//tar zxvf mirror
tar zxvf php-7.2.6.tar.gz
cd php-7.2.6/ext/zip/
phpize
./configure
make && make install

安装 Swoole 扩展

依赖 hiredis

cd /opt/data
#git clone https://github.com/swoole/swoole-src.git
wget https://github.com/swoole/swoole-src/archive/v2.1.2.zip
unzip v2.1.2.zip
cd swoole-src
sudo phpize
sudo ./configure --enable-coroutine --enable-async-redis=/usr/local/lib
sudo make
sudo make install
  • 注意配置参数 sudo ./configure --enable-coroutine --enable-async-redis 应改为 sudo ./configure --enable-coroutine --enable-async-redis=/usr/local/lib,否则 php 不能加载 swoole 扩展,会出现以下错误:
sudo php-fpm -v
PHP Warning:  PHP Startup: Unable to load dynamic library 'swoole.so' (tried: /usr/lib64/php/modules/swoole.so (libhiredis.so.0.13: cannot open shared object file: No such file or directory), /usr/lib64/php/modules/swoole.so.so (/usr/lib64/php/modules/swoole.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
PHP 7.2.6 (fpm-fcgi) (built: Jun  2 2018 07:52:10)
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

安装 hiredis 后,库文件 libhiredis.so.0.13 安装到了 /usr/local/lib/libhiredis.so.0.13 下,需要将 /usr/local/lib 加到库文件加载路径:

/etc/ld.so.conf

include ld.so.conf.d/*.conf
/usr/local/lib
  • 以上命令将会生成so文件 /usr/lib/php/20151012/swoole.so

添加到 php.ini 中

extension=swoole.so

安装 Xhprof 扩展

/opt/data

git clone https://github.com/longxinH/xhprof.git ./xhprof
cd xhprof/extension/
phpize
./configure --with-php-config=/usr/bin/php-config
make && sudo make install

以上步骤将会在 /usr/lib/php/20151012/ 下生成 xhprof.so 文件

  • 分别添加到两个配置文件
sudo vi /etc/php/7.2/fpm/conf.d/xhprof.ini
extension = xhprof.so;
xhprof.output_dir = /data/home/coam/Server/Run/Xhprof/logs/
  • 注意:output_dir 必须存在且有写权限写

添加到 php.ini 中

extension=xhprof.so

安装 Mosquitto-PHP 扩展

  • 安装依赖
sudo yum install mosquitto-devel
  • 开始安装
git clone https://github.com/mgdm/Mosquitto-PHP.git
cd Mosquitto-PHP/
phpize
# Ubuntu
sudo ./configure --with-mosquitto=/usr/lib/x86_64-linux-gnu/
# CentOS
sudo ./configure
make
make install

添加到 php.ini 中

extension=mosquitto.so

php.ini 其它通用配置

  • 配置 session 会话时间
session.gc_maxlifetime = 86400
  • 配置 php 连接 Mysql 的 sock, 默认留空为 “/var/run/mysqld/mysqld.sock” ,根据实际情况修改如下部分:
* [Pdo_mysql]
...
pdo_mysql.default_socket = /var/run/mysqld/mysqld.sock
...

* [MySQL]
...
#php5.6#mysql.default_socket = /var/run/mysqld/mysqld.sock
...

* [MySQLi]
...
mysqli.default_socket = /var/run/mysqld/mysqld.sock
...

注:php7下 原来的 php5-fpm 进程已改为 php7.1-fpm

参考 * How To Upgrade to PHP 7 on Ubuntu 14.04 * How to install PHP 7 on Ubuntu 14.04 * Upgrade from PHP 5.3 to PHP 5.5 on Ubuntu 12.04 LTS * PPA for PHP 7.0 * ubuntu下搭载LNMP环境,解决 fpm监听失败


Php 常见问题解决

服务器频繁出现502错误问题分析

升级完 php 后,相应的 php 扩展也将要一并安装

Phalcon

git clone --depth=1 git://github.com/phalcon/cphalcon.git
cd cphalcon/build
sudo ./install
  • 以上命令将会生成so文件 /usr/lib/php5/20131226/phalcon.so

添加到 php.ini 中

extension=phalcon.so

apt install 出现依赖项问题

$ sudo apt install php5-fpm
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 php5-fpm : Depends: php5-common (= 5.6.16+dfsg-3+deb.sury.org~trusty+1) but 7.20151230+deb.sury.org~trusty+1 is to be installed
            Depends: php5-cli but it is not going to be installed
            Depends: php5-json but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

使用万能的 apt –purge remove 删掉php的依赖项 php5-common

sudo apt --purge remove php5-common
sudo apt autoremove

再重新执行安装即可

使用 apt install php5-fpm 后出现类似如下错误

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php5/20131226/recode.so' - /usr/lib/php5/20131226/recode.so: cannot open shared object file: No such file or directory in Unknown on line 0

使用 apt install php5-* 依次安装即可

apt install php5-recode

常见问题

Call to undefined function curl_init()

2016/05/14 10:24:06 [error] 32417#32417: *282 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Call to undefined function curl_init() in /data/home/coam/ServerCoam/Home/applications/plugins/WeChat/wechat.class.php on line 1072" 
while reading response header from upstream, client: 58.48.206.108, server: lp.coam.co, request: "GET / HTTP/2.0", upstream: "fastcgi://127.0.0.1:9000", host: "lp.coam.co"
  • 需要安装 php5-curl 扩展
sudo apt install php5-curl

Comments

Cor-Ethan, the beverage → www.iirii.com