CentOS 7.0编译安装PHP7

PHP7已经进入7.1的时期了,在测试版本时就曾编译过很多次,中间也出现过很多问题,一直没有机会写篇文章,这次编译香港ECS就一点点记录下来。

一、创建php用户和用户组,并在github下载php7源码

首先创建一个名为php且 没有登录权限 的用户和一个名为php的用户组,然后去GitHub下载php7源码包。

#######新建php用户和php组
groupadd -r php && useradd -r -g php -s /bin/false -d /usr/local/php7 -M php
######从GitHub下载php7安装包
wget -c --no-check-certificate -O php7-src-master.zip https://github.com/php/php-src/archive/master.zip
######开始解压php7包
unzip -q php7-src-master.zip && cd php-src-master
#####安装编译php7时需要的依赖包
yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel

二、 编译

1、生产编译文件

./buildconf

2、配置编译参数

./configure --prefix=/usr/local/php7 \
--exec-prefix=/usr/local/php7 \
--bindir=/usr/local/php7/bin \
--sbindir=/usr/local/php7/sbin \
--includedir=/usr/local/php7/include \
--libdir=/usr/local/php7/lib/php \
--mandir=/usr/local/php7/php/man \
--with-config-file-path=/usr/local/php7/etc \
--with-mysql-sock=/var/run/mysql/mysql.sock \
--with-mcrypt=/usr/include \
--with-mhash \
--with-openssl \
--with-mysqli=shared,mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--with-gd \
--with-iconv \
--with-zlib \
--enable-zip \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--without-gdbm \
--disable-fileinfo

如果提示:

configure: WARNING: This bison version is not supported for regeneration of the Zend/PHP parsers (found: none, min:
204, excluded: ).checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
configure: error: bison is required to build PHP/Zend when building a GIT checkout!

则因没有安装

bison

,或者版本过低,请安装或者更新。
yum install -y bison
然后再次运行上面的编译参数配置。

下面的提示,则表示配置成功:

Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php7.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

3、编译安装

make clean && make && make install

当然你也可以分步执行,并在make后执行make test进行测试。
make的过程是十分漫长的,你可以去冲杯咖啡。

如果出现:

Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
clicommand.inc
invertedregexiterator.inc
directorytreeiterator.inc
pharcommand.inc
directorygraphiterator.inc
phar.inc

Build complete.
Don’t forget to run ‘make test’.

则表示编译成功。

make test的过程也是十分漫长的。。。。目测提示,会测试很多bug。

中间会提示:

=====================================================================
WARNED TEST SUMMARY
---------------------------------------------------------------------
Bug #70172 - Use After Free Vulnerability in unserialize() [ext/standard/tests/serialize/bug70172_2.phpt] (warn: XF
AIL section but test passes)Bug #72663: Create an Unexpected Object and Don't Invoke __wakeup() in Deserialization [ext/standard/tests/strings/
bug72663_3.phpt] (warn: XFAIL section but test passes)=====================================================================

You may have found a problem in PHP.
This report can be automatically sent to the PHP QA team at
http://qa.php.net/reports and http://news.php.net/php.qa.reports
This gives us a better understanding of PHP’s behavior.
If you don’t want to send the report immediately you can choose
option “s” to save it. You can then email it to qa-reports@lists.php.net later.
Do you want to send this report now? [Yns]:

询问你是否许可吧相关报告发给PHP组。或者保存。

Please enter your email address.
(Your address will be mangled so that it will not go out on any
mailinglist in plain text): 614422099@qq.com

Posting to http://qa.php.net/buildtest-process.php

Thank you for helping to make PHP better.

输入Y填写你的邮箱,将数据发送到PHP官方。

执行 make install 进行安装


Installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20160303/
Installing PHP CLI binary: /usr/local/php7/bin/
Installing PHP CLI man page: /usr/local/php7/php/man/man1/
Installing PHP FPM binary: /usr/local/php7/sbin/
Installing PHP FPM config: /usr/local/php7/etc/
Installing PHP FPM man page: /usr/local/php7/php/man/man8/
Installing PHP FPM status page: /usr/local/php7/php/php/fpm/
Installing phpdbg binary: /usr/local/php7/bin/
Installing phpdbg man page: /usr/local/php7/php/man/man1/
Installing PHP CGI binary: /usr/local/php7/bin/
Installing PHP CGI man page: /usr/local/php7/php/man/man1/
Installing build environment: /usr/local/php7/lib/php/build/
Installing header files: /usr/local/php7/include/php/
Installing helper programs: /usr/local/php7/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/php7/php/man/man1/
page: phpize.1
page: php-config.1
/data/source/php-src-master/build/shtool install -c ext/phar/phar.phar /usr/local/php7/bin
ln -s -f phar.phar /usr/local/php7/bin/phar
Installing PDO headers: /usr/local/php7/include/php/ext/pdo/

提示成功,并提示相关路径。

4、配置相关配置文件

php.ini、php-fpm.conf、www.conf和php-fpm

可以用编译后的配置文件复制到PHP7的配置目录(/usr/local/php7/etc/),推荐使用 《PHP7中php.ini、php-fpm和www.conf的配置》 文中整理的3个PHP7配置文件和php-fpm服务控制脚本。


#######方法一:直接使用编译后未经优化处理的配置
cp php.ini-production /usr/local/php7/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
#######方法二:使用https://typecodes.com/web/php7configure.html文中的配置
mv ~/php.ini /usr/local/php7/etc/php.ini && mv ~/php-fpm /etc/init.d/php-fpm
mv ~/php-fpm.conf /usr/local/php7/etc/php-fpm.conf && mv ~/www.conf /usr/local/php7/etc/php-fpm.d/www.conf

5、添加环境变量

echo -e '\nexport PATH=/usr/local/php7/bin:/usr/local/php7/sbin:$PATH\n' >> /etc/profile && source /etc/profile

6、创建相关路径

在Linux主机上创建一个目录 /mydata/php/data ,用于存放php的数据库文件。同时设置其用户和用户组为之前创建的 php ,权限为777。这样其它用户是无法进行读写的,尽量保证数据库的安全。


#######设置PHP日志目录和php-fpm的运行进程ID文件(php-fpm.sock)目录
mkdir -p /var/log/php-fpm/ && mkdir -p /var/run/php-fpm && cd /var/run/ && chown -R nginx:nginx php-fpm
#######修改session的目录配置
mkdir -p /var/lib/php/session
chown -R nginx:nginx /var/lib/php

三、设置PHP开机启动以及测试配置文件是否正确


######配置开机自启动,增加到主机sysV服务
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
######测试PHP的配置文件是否正确合法
php-fpm -t
[24-Aug-2016 15:40:42] NOTICE: configuration file /usr/local/php7/etc/php-fpm.conf test is successful

四、启动PHP

service php-fpm start
Starting php-fpm done

然后可以通过命令 ps -aux|grep php 查看是否成功(图中的php-fpm进程数和进程用户nginx都是由www.conf中pm.start_servers和user的值分别决定的)

五、查看PHP7版本信息

最后,可以通过命令 php -v 查看当前PHP版本信息,图中可以看到当前PHP7还使用了Zend OPcache缓存,是因为在php.ini文件中添加了 zend_extension=opcache.so 配置。

Comments are closed.