php 编译安装配置

环境信息

  • Centos 7
  • Php 7.2

编译安装

安装依赖包

yum install -y gcc libxml2-devel libjpeg-turbo-devel libpng-devel libxslt libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel  bzip2  bzip2-devel  libxslt libxslt-devel  libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel perl perl-devel httpd-devel readline-devel autoconf

下载安装包,编译安装

wget http://am1.php.net/distributions/php-7.2.0.tar.bz2
tar -xf php-7.2.0.tar.bz2
cd php-7.2.0

./configure --prefix=/usr/local/php7/ \
--with-fpm-user=nginx \
--with-curl --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 \
--with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql \
--with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib \
--enable-fpm --enable-bcmath -enable-inline-optimization --enable-mbregex --enable-mbstring \
--enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem \
--enable-xml --enable-zip --enable-pcntl --with-curl --enable-ftp --enable-session --enable-xml \
--with-freetype-dir --with-jpeg-dir --enable-calendar --enable-exif --with-readline --enable-sysvshm --enable-wddx

make

make install

安装完成后,拷贝配置文件:

cp php.ini-production /usr/local/php7/etc/php.ini
cd /usr/local/php7/etc
mv php-fpm.conf.default php-fpm.conf

php编译好之后,提供了一个php-fpm的程序作为服务管理程序,位置在编译后的源码目录:php-7.2.0/sapi/fpm/

cp php-fpm.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start php-fpm

php 常用扩展安装方法

php 扩展优先选择使用 pecl 命令安装,需要特殊配置时再选择编译安装, pecl 安装 redis 扩展示例

pecl install redis

yac 扩展安装

git clone https://github.com/laruence/yac
cd yac
phpize
./configure --with-php-config=/usr/local/php7/bin/php-config
make && make install

composer 安装

php -r "readfile('https://getcomposer.org/installer');" | php
mv composer.phar /usr/local/bin/composer

inotify 安装

扩展下载地址
比如安装2.0.0版本

wget http://pecl.php.net/get/inotify-2.0.0.tgz
tar -xf inotify-2.0.0.tgz
phpize
./configure --with-php-config=/usr/local/php7/bin/php-config --enable-inotify
make && make install

memcache 扩展安装

yum -y install libmemcached libmemcached-devel     ##先按照依赖,否则make报错
git clone https://github.com/php-memcached-dev/php-memcached.git php-memcache
cd php-memcache/
git checkout php7
phpize
./configure --enable-memcached --with-php-config=/usr/local/php7/bin/php-config
make
make install

php.ini 中添加配置 extension=memcached.so

gd 扩展安装

进入到php的源码目录下的ext/gd目录

cd php-7.2.0/ext/gd
yum -y install libjpeglibjpeg-devel libpng libpng-devel freetype freetype-devel
make clean
phpize
./configure --with-png-dir --with-freetype-dir --with-jpeg-dir --with-gd --with-php-config=/usr/local/php7/bin/php-config
make && make install

php.ini 中添加配置 extension=gd.so

mongodb 扩展安装

扩展下载地址

wget http://pecl.php.net/get/mongodb-1.14.0.tgz
tar -xf mongodb-1.14.0.tgz
cd mongodb-1.14.0
/usr/local/php7/bin/phpize
./configure --with-php-config=/usr/local/php7/bin/php-config
make
make install

imagick 扩展安装

首先安装ImageMagick

wget https://imagemagick.org/download/ImageMagick.tar.gz
tar -xf ImageMagick.tar.gz
cd ImageMagick
./configure
make
make install

安装 imagick 扩展

wget http://pecl.php.net/get/imagick-3.4.4.tgz
/usr/local/php7/bin/phpize
./configure --with-php-config=/usr/local/php7/bin/php-config
make
make install

trie_filter 扩展安装

安装依赖 libdatrie

curl -O ftp://linux.thai.net/pub/ThaiLinux/software/libthai/libdatrie-0.2.4.tar.gz
tar zxvf libdatrie-0.2.4.tar.gz
cd libdatrie-0.2.4
make clean
./configure --prefix=/usr/local/libdatrie
make
make install

安装扩展 trie_filter

wget https://github.com/jiopuud/trie_filter/archive/master.zip
unzip master.zip
cd trie_filter-master
phpize
./configure --with-php-config=/usr/local/php7/bin/php-config --with-trie_filter=/usr/local/libdatrie
make
make install

swoole 扩展安装

安装依赖 hiredis(可选操作)
下载链接

make
make install

安装 woole 扩展
下载地址

tar -xf swoole-src-1.9.15.tar.gz
cd swoole-src-1.9.15
phpize
./configure --enable-openssl --enable-async-redis --with-php-config=/usr/local/php7/bin/php-config
make
make install

--enable-async-redis 依赖hiredis, 需要先安装 hiredis
编译安装过程中,可能出现找不到动态链接库的问题,可将 /usr/local/lib 添加到共享的动态库,
编辑 ldconfig 配置文件 /etc/ld.so.conf,追加以下内容

/etc/ld.so.conf
/usr/local/lib

修改 ldconfig 配置后,执行以下命令使新配置生效

ldconfig

phpredis 扩展安装

下载地址

cd phpredis/
phpize
./configure --with-php-config=/usr/local/php7/bin/php-config
make
make install

php-fpm 常见配置

php-fpm 日志相关配置

php-fpm 全局错误日志配置默认位于 etc/php-fpm.conf

etc/php-fpm.conf
;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;

[global]
error_log = /var/log/php-fpm/error.log

; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice
;log_level = notice

php-fpm 默认相关 pool 日志配置主要位于 etc/php-fpm.d/www.conf。默认情况下,pool 中的日志(stdout and stderr) 会被重定向到 /dev/null

如果需要将对应 pool 中的日志输出到全局错误日志配置的路径,需要配置 catch_workers_output = yes

access.log = /var/log/php/www.access.log

# 慢日志
slowlog = /var/log/php/www.log.slow
request_slowlog_timeout = 10

; Redirect worker stdout and stderr into main error log. If not set, stdout and
; stderr will be redirected to /dev/null according to FastCGI specs.
; Note: on highloaded environement, this can cause some delay in the page
; process time (several ms).
; Default Value: no
;catch_workers_output = yes

# 以下配置是对 php.ini 的补充,会在定义的 pool (比如 www) 中覆盖 php.ini 中的配置。
# 否则为了记录日志,需要修改 php.ini 文件,增加以下配置
## log_errors = On
## error_log = "/usr/local/php/var/log/error_log"
## error_reporting=E_ALL&~E_NOTICE

php_flag[display_errors] = on
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on