## 場景
LNMP
當前版本:PHP 5.5.7
為了體驗PHP7的速度和性能,加上對新技術的熱衷。但是直接切換是不明智的,可能會存在代碼不兼容等等問題,因此準備編譯安裝PHP7,Nginx根據PHP-FastCGI監聽端口啟用哪個PHP版本。
## 什么是FastCGI
FastCGI是一個可伸縮地、高速地在HTTP server和動態腳本語言間通信的接口。
大多數流行的HTTP server都支持FastCGI,包括Apache,Nginx和lighttpd等
當然PHP也在其中之列了。
> FastCGI接口方式采用C/S結構,可以將HTTP服務器和腳本解析服務器分開,同時在腳本解析服務器上啟動一個或多個腳本解析守護進程。當HTTP服務器每次遇到動態程序時,可以將其直接交付給FastCGI進程來執行,然后將其得到的結果返回給瀏覽器。
## PHP版本共存思路
Nginx是通過PHP-FastCGI與PHP進行交互的。而PHP-FastCGI運行后會通過文件、或本地端口兩種方式進行監聽,在Nginx中配置相應的FastCGI監聽端口或文件即實現Nginx請求對PHP的解釋。
既然PHP-FastCGI是監聽端口和文件的,那就可以讓不同版本的PHP-FastCGI同時運行,監聽不同的端口或文件,Nginx中根據需求配置調用不同的PHP-FastCGI端口或文件,即可實現不同版本PHP共存了。
## 編譯安裝過程
### 下載解壓PHP7
~~~
wget -c --no-check-certificate -O php7-src-master.zip https://github.com/php/php-src/archive/master.zip
unzip -q php7-src-master.zip && cd php-src-master
~~~
### 編譯安裝
注意:路徑換成自己的。
我是編譯到server/php7下面
~~~
./buildconf –force
./configure \
–prefix=/alidata/server/php7 \
–exec-prefix=/alidata/server/php7 \
–bindir=/alidata/server/php7/bin \
–sbindir=/alidata/server/php7/sbin \
–includedir=/alidata/server/php7/include \
–libdir=/alidata/server/php7/lib/php \
–mandir=/alidata/server/php7/php/man \
–with-config-file-path=/alidata/server/php7/etc \
–with-mysql-sock=/tmp/mysql.sock \
–with-mcrypt=/usr/include \
–with-mhash \
–with-openssl \
–with-mysql=shared,mysqlnd \
–with-mysqli=shared,mysqlnd \
–with-pdo-mysql=shared,mysqlnd \
–with-gd \
–with-iconv \
–with-zlib \
–enable-zip \
–enable-inline-optimization \
–enable-sockets \
–enable-soap \
–enable-session \
–with-curl \
–enable-opcache \
–enable-fpm \
–enable-fastcgi \
–with-fpm-user=www \
–with-fpm-group=www \
–disable-fileinfo
~~~
執行后結果如下(懶,就不截圖了。哈哈):
注:因為之前安裝過PHP5版本,所以依賴關系就不需要了。
> 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
configure: WARNING: unrecognized options: –with-mysql, –enable-fastcgi
### 編譯&&安裝
~~~
make clean && make && make install
~~~
執行結果如下:
> nstalling shared extensions: /alidata/server/php7/lib/php/extensions/no-debug-non-zts-20151012/
Installing PHP CLI binary: /alidata/server/php7/bin/
Installing PHP CLI man page: /alidata/server/php7/php/man/man1/
Installing PHP FPM binary: /alidata/server/php7/sbin/
Installing PHP FPM config: /alidata/server/php7/etc/
Installing PHP FPM man page: /alidata/server/php7/php/man/man8/
Installing PHP FPM status page: /alidata/server/php7/php/php/fpm/
Installing phpdbg binary: /alidata/server/php7/bin/
Installing phpdbg man page: /alidata/server/php7/php/man/man1/
Installing PHP CGI binary: /alidata/server/php7/bin/
Installing PHP CGI man page: /alidata/server/php7/php/man/man1/
Installing build environment: /alidata/server/php7/lib/php/build/
Installing header files: /alidata/server/php7/include/php/
Installing helper programs: /alidata/server/php7/bin/
program: phpize
program: php-config
Installing man pages: /alidata/server/php7/php/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /alidata/server/php7/lib/php/php/
### 設置PHP7的配置文件
php.ini、php-fpm.conf、www.conf和php-fpm腳本
~~~
cp php.ini-production /路徑/php7/etc/php.ini #復制php.ini配置
cp sapi/fpm/init.d.php-fpm /etc/init.d/php7-fpm #復制php-fpm啟動腳本
##其他配置都在php7/etc/目錄下,自行修改配置
~~~
啟動php7-fpm
~~~
chmod 755 /etc/init.d/php-fpm
/etc/init.d/php7-fpm start
~~~
php-fpm支持的操作:
- start,啟動PHP的FastCGI進程。
- stop,強制終止PHP的FastCGI進程。
- quit,平滑終止PHP的FastCGI進程。
- restart, 重啟PHP的FastCGI進程。
- reload, 重新加載PHP的php.ini。
- logrotate, 重新啟用log文件。
### 添加PHP環境變量
~~~
echo -e '\nexport PATH=/路徑/php7/bin:/路徑/php7/sbin:$PATH\n' >> /etc/profile && source /etc/profile
~~~
### 修改Nginx配置,使用PHP7
~~~
location ~ .*.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9001; #注意,因為已經有個9000端口服務于PHP5版本了,所以PHP7就用9001了。
fastcgi_index index.php;
include fcgi.conf;
}
~~~

- 前言
- PHP生成對象之設計模式—單例模式
- PHP生成對象之設計模式—工廠方法模式
- PHP之設計模式—適配器模式
- PHP之設計模式—建造者模式(通過選擇mysql,mongo數據庫鏈接類型做說明)
- PHP之設計模式—委托模式
- PHP面向對象學習一:對象基礎實踐
- PHP面向對象學習之二:深入了解面向對象高級特性
- PHP面向對象學習之三:抽象類和接口類的實際作用
- PHP解決問題進化論(整理筆記)
- PHP7新特性整理介紹篇
- php-fpm 與 Nginx優化總結
- Centos+Nginx+PHP7.0編譯安裝(和PHP5.6老版本共存)
- PHP7:Mongodb API使用
- PHP之include/require深入了解
- PHP內核了解:生命周期及運行模式