1)前期准备
#按需求自行删减
cc gcc-c++ make cmake automake autoconf gd file bison patch mlocate flex
diffutils zlib zlib-devel pcre pcre-devel
libjpeg libjpeg-devel libpng libpng-devel libxml2 libxml2-devel
freetype freetype-devel
glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel
ncurses ncurses-devel
curl curl-devel libcurl libcurl-devel
e2fsprogs e2fsprogs-devel
krb5 krb5-devel
openssl openssl-devel openldap openldap-devel openldap-clients
openldap-servers openldap-devellibxslt-devel nss_ldap
kernel-devel libtool-libs
readline-devel gettext-devel libcap-devel
php-mcrypt libmcrypt libmcrypt-devel recode-devel
icu gmp-devel libxslt libxslt-devel
yum install -y gcc gcc-c++ make cmake automake autoconf gd file bison patch mlocate flex diffutils zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel libcurl libcurl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers openldap-devellibxslt-devel kernel-devel libtool-libs readline-devel gettext-devel libcap-devel php-mcrypt libmcrypt libmcrypt-devel recode-devel gmp-devel icu libxslt libxslt-devel
2)安装 libmcrypt mhash mcrypt
注:mcrypt依赖前两者
wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz"
wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz"
wget "https://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz"
注:configure: error: *** libmcrypt was not found
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
或者
ldconfig
3)编译安装PHP7
说明:PHP7 移除了已经被废弃的函数,如 mysql_ 系列函数在 PHP5.5 被废弃,在 PHP7被删除,并且 php-fpm 也已经被 php 纳入,直接在选项中开启即可,--enable-cgi 是默认配置且 --enable-fast-cgi 已经被废除,--enable-cgi 即可让 fpm 模式调用。
ladp在64的系统里可能存在写库关联问题,执行下面的命令
cp -frp /usr/lib64/libldap* /usr/lib/
#部分配置释义
./configure \--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-mcrypt \ # mcrypt 加密算法
--with-mhash \ # mhash 加密算法
--with-mysqli=mysqlnd \ # 推荐使用 mysqlnd 驱动 mysql已被废弃
--with-pdo-mysql=mysqlnd \ # 推荐使用 mysqlnd 驱动 mysql已被废弃
--with-gd \ # gd库
--with-iconv \
--with-zlib \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--with-apxs2=/usr/bin/apxs \ # 让 php 直接编译到 apache 的模块中去
--with-apache=/etc/httpd \ # 静态编译apache的模块 编译完后去httpd.conf中加载so
--with-openssl \
--with-xmlrpc \
--with-bz2 \
--with-gettext \ # 多语言应用开发
--with-readline \ # readline只能在linux下win使stream_get_line
--with-recode \
--with-ldap \ # 目录协议
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-fpm \ # php-fpm 进程管理器
--enable-cgi \ # 默认开启php的cgi sapi
--enable-xml \
--enable-bcmath \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--enable-pcntl \ #pcntl 多进程扩展模块
--enable-sockets \
--enable-sysvmsg \ #消息队列
--enable-sysvshm \ #内存共享模块 linux
--enable-shmop \ #内存共享模块 linux/windows
--enable-zip \
--enable-soap \
--enable-session \
--enable-opcache \ # opcache 缓存优化
--enable-cli # cli 其实默认就是开启的 若禁用同时要
--without-pear
编译参数
./configure --prefix=/usr/local/php \
--with-mysql-sock \
--with-mysqli \
--with-libxml-dir \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-zlib \
--with-iconv \
--with-bz2 \
--with-curl \
--with-cdb \
--with-pcre-dir \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--with-gettext \
--with-gmp \
--with-mhash \
--with-libmbfl \
--with-onig \
--with-pdo-mysql \
--with-zlib-dir \
--with-readline \
--with-libxml-dir \
--with-xsl \
--with-pear \
--enable-fpm \
--enable-soap \
--enable-bcmath \
--enable-calendar \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--enable-ftp \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--enable-pdo \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--enable-zip \
--enable-mysqlnd-compression-support \
--enable-intl
--enable-cgi --disable-cgi :
在5.3.0后 enable-cgi 等同于--enable-fastcgi 也就是说此后不需要在额外声明 --enable-fastcgi,且 --enable-force-cgi-redirect 也默认有效且被废弃在编译选项中,要禁用需修改php.ini的cgi.force_redirect为0
--with-apxs2 --with-apache:
--with-apxs2是直接把php编译到apache的模块中去 --with-apache是置顶apache的serverRoot路径,生成apache的php模块 修改apache配置文件手动加载:
AddType application/x-httpd-php .php .html .htm
php7.0废除了--with-mysql编译选项,且推荐使用mysqlnd驱动
-bash: --with-openssl: command not found:
which openssl 查看目录 添加环境变量
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
4)php-fpm 服务脚本
#! /bin/sh
# Comments to support chkconfig on CentOS
# chkconfig: 2345 65 37
#
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="php-fpm daemon"
NAME=php-fpm
#php-fpm命令的路径
DAEMON=/usr/local/php/sbin/$NAME
#php-fpm的配置文件
CONFIGFILE=/usr/local/php/etc/php-fpm.conf
#php-fpm的pid文件
PIDFILE=/usr/local/php/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
d_start() {
$DAEMON -y $CONFIGFILE || echo -n " already running"
}
d_stop() {
kill -QUIT `cat $PIDFILE` || echo -n " not running"
}
d_reload() {
kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC is success"
d_start
echo "." ;;
stop)
echo -n "Stopping $DESC is success"
d_stop
echo "." ;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded." ;;
restart)
echo -n "Restarting $DESC is success"
d_stop
sleep 1
d_start
echo "." ;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3 ;;
esac
内置php-fpm
cd /usr/local/php7/etc
mv php-fpm.conf.default php-fpm.conf
mv php-fpm.d/www.conf.default php-fpm.d/www.conf
#进入源码安装包目录
cd /data/php-7.0.11
#copy php配置文件 得到php.ini 配置这里不作说明
cp php.ini-production /usr/local/php/etc/php.ini
cd sapi/fpm #进到源码包里的sapi/fpm目录
cp init.d.php-fpm /etc/init.d/php-fpm #copy cgi管理器 得到php-fpm
chmod +x /etc/init.d/php-fpm #附上可执行权限
chkconfig --add php-fpm #加入开机启动项
chkconfig php-fpm on
service php-fpm start #启动php-fpm
ps -ef | grep php-fpm #查看php-fpm启动状态
测试
vi /var/www/html/index.php
$con = new mysqli('127.0.0.1', 'root', '123456', '', 3306);
$res = $con->query('show databases;');
var_dump($res->fetch_assoc());
?>
/usr/local/php/sbin/php-fpm -v
PHP 7.0.11 (fpm-fcgi) (built: Aug 27 2016 20:46:44)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
service php-fpm start 启动