1. 程式人生 > 實用技巧 >編譯安裝php7

編譯安裝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 //下載 wget http://cn2.php.net/distributions/php-7.0.5.tar.gz //解壓 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