编译安装php7

引用:http://www.hello1010.com/php7

php7发布已经有一段时间了,运行性能与HHVM不相上下,甚至在某些环境下超越了HHVM。鸟哥是php7的主要开发者之一,关于php7的来龙去脉大家可以看这篇文章:鸟哥:写在PHP7发布之际一些话。php7的新特性看这篇文章:migration70.new-features

准备工作

安装一些依赖库文件

1
2
yum groupinstall "Development tools"
yum install libxml2-devel gd-devel libmcrypt-devel libcurl-devel openssl-devel

安装php7

可以在php的官网上找到不同的版本下载链接。

1
2
3
4
5
//下载
//解压
tar -zxvf php-7.0.5.tar.gz
cd php-7.0.5

配置,安装在/usr/local/php7这个路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
./configure --prefix=/usr/local/php7
 --with-curl
 --with-freetype-dir
 --with-gd
 --with-gettext
 --with-iconv-dir
 --with-kerberos
 --with-libdir=lib64
 --with-libxml-dir
 --with-mysqli
 --with-mysql
 --with-openssl
 --with-pcre-regex
 --with-pdo-mysql
 --with-pdo-sqlite
 --with-pear
 --with-png-dir
 --with-xmlrpc
 --with-xsl
 --with-zlib
 --with-apxs2
 --enable-fpm
 --enable-bcmath
 --enable-libxml
 --enable-inline-optimization
 --enable-gd-native-ttf
 --enable-mbregex
 --enable-mbstring
 --enable-opcache
 --enable-pcntl
 --enable-shmop
 --enable-soap
 --enable-sockets
 --enable-sysvsem
 --enable-xml
 --enable-zip
 --enable-mysqlnd

编译

1
make

安装 make install

安装成功后,可以看到类似下面的输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/
Installing PHP CLI binary:        /usr/local/php/bin/
Installing PHP CLI man page:      /usr/local/php/php/man/man1/
Installing PHP FPM binary:        /usr/local/php/sbin/
Installing PHP FPM config:        /usr/local/php/etc/
Installing PHP FPM man page:      /usr/local/php/php/man/man8/
Installing PHP FPM status page:   /usr/local/php/php/php/fpm/
Installing phpdbg binary:         /usr/local/php/bin/
Installing phpdbg man page:       /usr/local/php/php/man/man1/
Installing PHP CGI binary:        /usr/local/php/bin/
Installing PHP CGI man page:      /usr/local/php/php/man/man1/
Installing build environment:     /usr/local/php/lib/php/build/
Installing header files:           /usr/local/php/include/php/
Installing helper programs:       /usr/local/php/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.0
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.3.0
[PEAR] PEAR           - installed: 1.10.1
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/tmp/php-7.0.5/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers:           /usr/local/php/include/php/ext/pdo/

可以查找一下libphp7.so文件,看看是否生成

1
2
3
4
5
6
7
[root@hellojammy ~]# find / -name *php7*.so
/usr/lib64/httpd/modules/libphp7.so
[root@hellojammy ~]# php -version
PHP 7.0.5 (cli) (built: Apr 12 2016 13:30:14) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

可见已经生成了libphp7.so。查看版本也是php7.0.5。

配置

配置文件。在php的编译目录,执行以下命令。

1
cp php.ini-development /usr/local/php7/lib/php.ini

查看apache的配置文件是否已经开启关联(一般情况下,安装完php后,会开启,假如没有开启则开启)

1
2
vim /usr/local/apache/conf/httpd.conf
LoadModule php5_module /usr/lib64/httpd/modules/libphp7.so

加入以下代码:

1
2
3
<FilesMatch .php$>
SetHandler application/x-httpd-php
</FilesMatch>

更改一下代码:

1
DirectoryIndex index.html index.shtml index.cgi index.php index.phtml index.php3

找到AddType处,并添加以下2行:

1
2
AddType application/x-httpd-php .php .php3 .phtml .inc
AddType application/x-httpd-php-source .phps

注意事项

编译php7可能遇到的问题之一是无法生成libphp7.so文件,这可以做以下检查:

1、检查configure是不是缺少一个 --with-apxs2 ,添加一下 2、然后确认apache配置文件httpd.conf中是否含有以下配置: SetHandler application/x-httpd-php 如果有,则进行第3步,不然添加一下 3、重新 ./configure + 参数 make make install,重新安装安装一下,记得加--with-apxs2。

无法生成libphp7.so的解决方案,参考了知乎的这篇文章

参考文章

1、Linux环境PHP7.0安装

2、php7编译后没有libphp7.so

原文地址:https://www.cnblogs.com/shuangxinye/p/13653365.html