CentOS 7.1编译安装PHP7

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


首先创建一个名为php且没有登录权限的用户和一个名为php的用户组,然后去GitHub下载php7源码包。
#######新建php用户和php组
[root@typecodes ~]# groupadd -r php && useradd -r -g php -s /bin/false -d /usr/local/php7 -M php
######从GitHub下载php7安装包
[root@typecodes ~]# wget -c --no-check-certificate -O php7-src-master.zip https://github.com/php/php-src/archive/master.zip
######开始解压php7包
[root@typecodes ~]# unzip -q php7-src-master.zip && cd php-src-master
#####安装编译php7时需要的依赖包
[root@typecodes php-src-master]# yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel


2 PHP7编译参数的配置


准备工作做好后,就开始正式配置php7的安装明细了。注意,操作时一定要先把下面反斜杠“\”后面添加的注释文字去掉!!!
######开始生成配置文件
[root@typecodes php-src-master]# ./buildconf
buildconf: checking installation...
buildconf: autoconf version 2.69 (ok)
rebuilding aclocal.m4
rebuilding configure
rebuilding main/php_config.h.in
######开始配置
[root@typecodes php-src-master]# ./configure \
--prefix=/usr/local/php7 \                              [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 \           [PHP7的配置目录]
--with-mysql-sock=/var/run/mysql/mysql.sock \           [PHP7的Unix socket通信文件]
--with-mcrypt=/usr/include \
--with-mhash \
--with-openssl \            
--with-mysqli=shared,mysqlnd \                          [PHP7依赖mysql库]
--with-pdo-mysql=shared,mysqlnd \                       [PHP7依赖mysql库]
--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 \                                      [允许php会话session]
--with-curl \                                           [允许curl扩展]
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache=no \                                      [使用opcache缓存]
--enable-fpm \
--with-fpm-user=nginx \                                 [php-fpm的用户]
--with-fpm-group=nginx \                                [php-fpm的用户组]
--without-gdbm \
--disable-fileinfo


直接复制下面的运行


./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=no --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --without-gdbm --disable-fileinfo


错误信息:configure: error: mcrypt.h not found. Please reinstall libmcrypt
解决方法:下载 libmcrypt-2.5.8.tar.gz 然后编译安装
下载地址:http://sourceforge.net/projects/mcrypt/files/Libmcrypt/


如果重新编译了libmcrypt  记住安装目录,然后php的编译项 --with-mcrypt=你的libmcrypt安装目录
./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/local/libmcrytp/ --with-mhash --with-openssl --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gd --with-iconv --with-zlib --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


编译时遇到问题:Don't know how to define struct flock on this system, set --enable-opcache=no
解决方法:(我最终用这些方法还是没有解决)
一、yum groupinstall "Development Tools"。
二、编辑 /etc/ld.so.conf 加入/usr/local/lib,在执行ldconfig -v
提示:我在/usr/lib 看到libmysqlclient.so  所以在编辑/etc/ld.so.conf的时候,需要看一下,里面是否存在这个


3 开始编译和安装PHP7


相对编译安装MySQL的大量CPU和内存消耗,PHP7的编译安装轻松多了,整个过程大约1个小时左右。
在编译的时候,把--enable-opcache=no设为no,去掉了--enable-zip,所在安装完成之后,需要单独把这两个作为扩展安装
[root@typecodes php-src-master]# make clean && make && make install
如果遇到错误可参考:http://blog.chinaunix.net/uid-20776139-id-5421615.html
4 选做步骤:执行make test命令进行测试


这是一个非必需的操作步骤,执行make test命令。


有趣的一件事就是:在test期间,会和一个IP地址72.52.91.14建立tcp连接,它对应着php官网http://www.php.net。


5 查看编译成功后的PHP7安装目录


由于需要和MySQL进行通信,所以需要特别查看PHP7安装后的lib扩展库目录(/usr/local/php7/lib/php/extensions/no-debug-non-zts-20141001/)。需要确保至少存在mysqli.so、pdo_mysql.so这两个动态库文件。


6 开始设置PHP7的配置文件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服务控制脚本。


#######方法一:直接使用编译后未经优化处理的配置
[root@typecodes php-src-master]# cp php.ini-production /usr/local/php7/etc/php.ini
[root@typecodes php-src-master]# cp /php-src-master/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@typecodes php-src-master]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@typecodes php-src-master]# 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文中的配置 
[root@typecodes php-src-master]# mv ~/php.ini /usr/local/php7/etc/php.ini && mv ~/php-fpm /etc/init.d/php-fpm
[root@typecodes php-src-master]# mv ~/php-fpm.conf /usr/local/php7/etc/php-fpm.conf && mv ~/www.conf /usr/local/php7/etc/php-fpm.d/www.conf


7 添加php的环境变量


将php编译生成的bin目录添加到当前Linux系统的环境变量中
[root@typecodes ~]# echo -e '\nexport PATH=/usr/local/php7/bin:/usr/local/php7/sbin:$PATH\n' >> /etc/profile && source /etc/profile


8 设置PHP日志目录和php-fpm进程文件(php-fpm.sock)目录


其中,设置php-fpm进程目录的用户和用户组为nginx,并创建php会话session目录。
#######设置PHP日志目录和php-fpm的运行进程ID文件(php-fpm.sock)目录
[root@typecodes ~]# mkdir -p /var/log/php-fpm/ && mkdir -p /var/run/php-fpm && cd /var/run/ && chown -R nginx:nginx php-fpm
#######修改session的目录配置
[root@typecodes etc]# mkdir -p /var/lib/php/session
[root@typecodes etc]# chown -R nginx:nginx /var/lib/php


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


######配置开机自启动,增加到主机sysV服务
[root@typecodes php-src-master]# chmod +x /etc/init.d/php-fpm
[root@typecodes php-src-master]# chkconfig --add php-fpm
[root@typecodes php-src-master]# chkconfig php-fpm on
######测试PHP的配置文件是否正确合法
[root@typecodes sbin]# php-fpm -t
[03-May-2015 17:50:04] NOTICE: configuration file /usr/local/php7/etc/php-fpm.conf test is successful


10 启动php服务


在完成上面的操作后,就可以正式使用php服务了。启动php进程服务的命令如下:


[root@typecodes sbin]# service php-fpm start
Starting php-fpm  done


11 安装opcache
教程:http://blog.jjonline.cn/linux/135.html
教程:http://blog.ich8.com/post/4142
教程:http://my.oschina.net/u/919580/blog/406900  实践证明这个可用


安装:
Linux
安装 Zend Opcache(pecl 版本):
删除 eaccelerator、xcache、apc:
yum remove php-eaccelerator php-xcache php-apcu
没有使用则跳过。
对系统执行升级:
yum update
安装 Zend Opcache(pecl 版本)
yum install php-pecl-zendopcache   PS:但是在centos7上还是没有找到 php-pecl-zendopcache扩展,所以下面无法继续操作
安装时产生的 opcache 的配置文件位于默认的 /etc/php.d 目录中:
opcache-default.blacklist
opcache.ini
这个配置文件采用的基本就是 README 中的推荐设置,只有几个地方需要修改。
vi /etc/php.d/opcache.ini
对照如下推荐配置修改并保存即可:
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
不需要修改 php.ini 配置,重起 Apache 服务使之生效:
service httpd restart


查询一下看看是否正确启动了:
php -v


输出结果类似于:
PHP 7 (cli) (built: Apr 11 2013 11:04:35)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
    with Zend OPcache v7.0.1, Copyright (c) 1999-2013, by Zend Technologies
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值