源码编译 PHP 环境

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

sudo apt remove php*
sudo apt autoremove

PHP7 升级

首先确保已安装 python-software-properties 才能添加 PPA ,执行以下命令:

  • 准备
sudo apt update
sudo apt install software-properties-common
sudo apt install python-software-properties

添加 php 的最新 ppa

sudo apt install -y language-pack-en-base
sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php
sudo apt update
#sudo apt dist-upgrade // 这步为系统全部更新,不推荐
sudo apt install php7.2
sudo apt install php7.2-common
sudo apt install php7.2-cli
sudo apt install php7.2-fpm
sudo service php7.2-fpm restart
php -v

PHP 7.2.4-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Apr  5 2018 08:53:57) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.4-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Nginx php 代理相关配置

  • 如果是默认的使用 /var/run/php5-fpm.sock

则修改 /etc/nginx/sites-enabled/default/var/run/php5-fpm.sock/var/run/php/php7.1-fpm.sock

  • 如果 nginx 监听 9000 端口,修改 /etc/php/7.0/fpm/pool.d/www.conf 为以下,并重启 服务器
;listen = /run/php/php7.1-fpm.sock
listen= 127.0.0.1:9000

安装 php7.1 相关扩展

使用以下命令查看已安装的 php 扩展

php -m
sudo apt install php7.2-curl
sudo apt install php7.2-json
sudo apt install php7.2-tidy
sudo apt install php7.2-mysql

sudo apt install php7.2-dev
sudo apt install php7.2-mbstring
sudo apt install php7.2-imagick
sudo apt install php7.2-gd
sudo apt install php7.2-intl
sudo apt install php7.2-gmp // [PHP-GMP库 —— 一个专门进行大数运算的扩展库](https://wp.iter-cc.com/?p=426)
...
sudo apt install -y php7.2-common php7.2-cli php7.2-fpm php7.2-curl php7.2-json php7.2-tidy php7.2-mysql php7.2-dev php7.2-mbstring php7.2-imagick php7.2-gd php7.2-intl php7.2-gmp

安装依赖

sudo apt install gcc libpcre3-dev

编译安装

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

安装依赖

sudo apt install pkg-config
sudo apt install php-pear

编译安装

sudo pecl install mongodb

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

  • 安装 memcached 扩展
sudo apt install php7.2-memcached

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

#sudo apt install libmemcached-dev build-essential
#sudo apt install php-pear
#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.0.tar.gz
tar zxvf 4.0.0.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

安装依赖

Ubuntu

sudo apt install libzip-dev

开始安装

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

安装 Swoole 扩展

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
sudo make
sudo make install
  • 以上命令将会生成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 apt install libmosquitto-dev
  • 开始安装
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 连接 Mysqlsock, 默认留空为 /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