Chinaunix首页 | 论坛 | 博客
  • 博客访问: 219416
  • 博文数量: 70
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 440
  • 用 户 组: 普通用户
  • 注册时间: 2015-08-09 09:16
个人简介

淡泊以明志,宁静以致远。

文章分类

全部博文(70)

分类: PHP

2017-06-21 17:39:33

在本地开发的时候,由于项目的需求,有些项目是基于PHP 5.2,而有些项目可能是基于PHP 5.4,又或者其他小版本号都对应,所以经常会在多个PHP版本中来回切换,对于本地开发还是稍有些麻烦的。

我要怎么样才能在Apache上支持多个版本的PHP同时运行,而不用每次使用都去更改配置重启Apache呢;通过分析网上的资料和自己的实践,发现可以使用Fastcgi方式来实现这个想法。

本文将讲解如何在Linux或Windows下安装支持多版本PHP(5.[2|3|4|5].x)同时运行,Mac OS X下以前使用过Nginx加PHP-FPM端口映射实现,Apache的方案未经实际测试,理论上与Linux操作类似。

Windows (测试环境Windows 7)

1. 下载所需软件

Win7 64位系统

Win7 32位系统

2. 安装类库

安装Apache与PHP之前先安装.Net Framework及以上几个Visual C++的类库。

3. 安装MySQL

双击MySQL的安装文件,按照提示一步一步安装完成。

4. 安装Apache

双击Apache的安装文件,按照提示一步一步安装完成,安装时请设置将Apache安装到C:/web/apache-2.2目录下。

5. 安装PHP

将已经下载的各PHP版本的zip直接解压到C:/web/php目录下,如php 5.2.17版本的,解压到C:/web/php/5217目录下,其他版本类似。安装完成后,应该会有以下一些目录:

  • C:/web/php/5217/
  • C:/web/php/5328/
  • C:/web/php/5430/
  • C:/web/php/5514/

6. 配置Apache

启用mod_rewrite模块与加载默认的virtual hosts文件。编辑文件C:/web/apache-2.2/httpd.conf,取消如下几行的注释后保存文件。

LoadModule rewrite_module modules/mod_rewrite.so
Include conf/extra/httpd-vhosts.conf

添加PHP支持与虚拟主机配置。编辑文件C:/web/apache-2.2/extra/httpd-vhosts.conf,文件内容如下:

# 设置php后缀的文件类型为application/x-httpd-php
AddType application/x-httpd-php .php
# ScriptAlias可以映射一个URL到文件系统并视之为CGI脚本,我们就可以使用ScriptAlias来映射不同版本php文件夹。
ScriptAlias /php-5217/ "C:/web/php/5217/"
ScriptAlias /php-5328/ "C:/web/php/5328/"
ScriptAlias /php-5430/ "C:/web/php/5430/"
ScriptAlias /php-5514/ "C:/web/php/5514/"
# 配置目录允许访问

 Order allow,deny
 Allow from all

# 配置虚拟主机站点目录

 DirectoryIndex index.php index.html
 Options Indexes FollowSymLinks Includes ExecCGI
 AllowOverride All
 Order allow,deny
 Allow from all

# 定义C:/web/sites/5217下的站点使用/php-5217/php-cgi.exe来执行。

 Action application/x-httpd-php "/php-5217/php-cgi.exe"

# 定义C:/web/sites/5328下的站点使用/php-5328/php-cgi.exe来执行。

 Action application/x-httpd-php "/php-5328/php-cgi.exe"

# 定义C:/web/sites/5430下的站点使用/php-5430/php-cgi.exe来执行。

 Action application/x-httpd-php "/php-5430/php-cgi.exe"

# 定义C:/web/sites/5514下的站点使用/php-5514/php-cgi.exe来执行。

 Action application/x-httpd-php "/php-5514/php-cgi.exe"

# 创建虚拟主机配置
NameVirtualHost *:80
# PHP 5.2.17测试站点

   ServerAdmin webmaster@php5217.local
   DocumentRoot "C:/web/sites/5217/test.local"
   ServerName php5217.local
   ErrorLog "logs/php5217.local-error.log"
   CustomLog "logs/php5217.local-access.log" common

# PHP 5.3.28测试站点

   ServerAdmin webmaster@php5328.local
   DocumentRoot "C:/web/sites/5328/test.local"
   ServerName php5328.local
   ErrorLog "logs/php5328.local-error.log"
   CustomLog "logs/php5328.local-access.log" common

# PHP 5.4.30测试站点

   ServerAdmin webmaster@php5430.local
   DocumentRoot "C:/web/sites/5430/test.local"
   ServerName php5430.local
   ErrorLog "logs/php5430.local-error.log"
   CustomLog "logs/php5430.local-access.log" common

# PHP 5.5.14测试站点

   ServerAdmin webmaster@php5514.local
   DocumentRoot "C:/web/sites/5514/test.local"
   ServerName php5514.local
   ErrorLog "logs/php5514.local-error.log"
   CustomLog "logs/php5514.local-access.log" common

保存配置后,创建各站点的DocumentRoot目录,再往每个目录放置一个phpinfo的测试文件,完成后重启Apache服务器并在本地hosts文件加入域名解析,现在就可以访问各站点来测试多版本PHP共存了。

Linux (测试环境 Ubuntu 12.04 Server X86_64)

1. 安装编译工具及所需类库

$ sudo apt-get install build-essential gcc g++ autoconf libjpeg62 libjpeg62-dev libpng12-0 libpng12-dev libfreetype6 libfreetype6-dev libxml2 libxml2-dev zlib1g zlib1g-dev bzip2 libbz2-dev openssl libssl-dev curl libcurl4-openssl-dev libpcre3 libpcre3-dev libevent-1.4-2 libevent-dev libmcrypt4 libmcrypt-dev mcrypt libltdl-dev libldap2-dev libsasl2-dev libmhash-dev libc-client2007e libc-client2007e-dev

2. 安装MySQL

$ sudo apt-get install mysql-server libmysqlclient-dev

3. 安装PHP

Linux下多版本PHP共存需要自己手工编译安装。

下载PHP源文件到/opt/src目录

$ mkdir /opt/src
$ cd /opt/src
$ wget  -O php-5.2.17.tar.bz2
$ wget  -O php-5.3.28.tar.bz2
$ wget  -O php-5.4.29.tar.bz2
$ wget  -O php-5.5.14.tar.bz2

创建PHP各版本安装目录

$ mkdir -p /opt/php/{5217,5328,5429,5514}

安装PHP 5.2.17

$ cd /opt/src
$ tar -xvjf php-5.2.17.tar.bz2
$ cd php-5.2.17
$ sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/libjpeg.so
$ sudo ln -s /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/libpng.so
$ sudo ln -s /usr/lib/x86_64-linux-gnu/libkrb5.so /usr/lib/libkrb5.so
$ wget -O debian_patches_disable_SSLv2_for_openssl_1_0_0.patch “
$ patch -p1 < debian_patches_disable_SSLv2_for_openssl_1_0_0.patch
$ ./configure --prefix=/opt/php/5217 --with-config-file-scan-dir=/opt/php/5217/etc/php.d --with-mysql --with-pdo-mysql --with-mysqli --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-ftp --disable-debug --disable-ipv6 --disable-short-tags --enable-calendar --with-mime-magic --with-imap --with-imap-ssl --with-kerberos
$ make
$ sudo make install
$ cp php.ini-recommended /opt/php/5217/lib/php.ini

安装PHP 5.3.28

$ cd /opt/src
$ tar -xvjf php-5.3.28.tar.bz2
$ cd php-5.3.28
$ ./configure --prefix=/opt/php/5328 --with-config-file-scan-dir=/opt/php/5328/etc/php.d --with-mysql --with-pdo-mysql --with-mysqli --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-ftp --disable-debug --disable-ipv6 --disable-short-tags --enable-calendar --with-imap --with-imap-ssl --with-kerberos
$ make
$ sudo make install
$ cp php.ini-development /opt/php/5328/lib/php.ini

安装PHP 5.4.29

$ cd /opt/src
$ tar -xvjf php-5.4.29.tar.bz2
$ cd php-5.4.29
$ ./configure --prefix=/opt/php/5429 --with-config-file-scan-dir=/opt/php/5429/etc/php.d --with-mysql --with-pdo-mysql --with-mysqli --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-ftp --disable-debug --disable-ipv6 --disable-short-tags --enable-calendar --with-imap --with-imap-ssl --with-kerberos
$ make
$ sudo make install
$ cp php.ini-development /opt/php/5429/lib/php.ini

安装PHP 5.5.14

$ cd /opt/src
$ tar -xvjf php-5.5.14.tar.bz2
$ cd php-5.5.14
$ ./configure --prefix=/opt/php/5514 --with-config-file-scan-dir=/opt/php/5514/etc/php.d --with-mysql --with-pdo-mysql --with-mysqli --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-ftp --disable-debug --disable-ipv6 --disable-short-tags --enable-calendar --with-imap --with-imap-ssl --with-kerberos
$ make
$ sudo make install
$ cp php.ini-development /opt/php/5514/lib/php.ini

4. 安装Apache

$ sudo apt-get install apache2

启用相应模块

$ a2enmod headers
$ a2enmod expires
$ a2enmod actions
$ a2enmod rewrite

5. 配置Apache

$ sudo vi /etc/apache2/httpd.conf

追加如下脚本映射和虚拟主机配置,原理同Windows的配置说明。

ServerName localhost
AddType application/x-httpd-php .php
ScriptAlias /php-5217/ "/opt/php/5217/bin/"
ScriptAlias /php-5328/ "/opt/php/5328/bin/"
ScriptAlias /php-5429/ "/opt/php/5429/bin/"
ScriptAlias /php-5514/ "/opt/php/5514/bin/"

  Options Indexes FollowSymLinks Includes ExecCGI
  DirectoryIndex index.php index.html
  AllowOverride All
  Order allow,deny
  Allow from all


  Action application/x-httpd-php "/php-5217/php-cgi"


  Action application/x-httpd-php "/php-5328/php-cgi"


  Action application/x-httpd-php "/php-5429/php-cgi"


  Action application/x-httpd-php "/php-5514/php-cgi"

# Virtualhosts

    ServerAdmin webmaster@php5217.local
    DocumentRoot "/var/www/sites/5217/test.local"
    ServerName php5217.local
    ErrorLog "/var/log/apache2/php5217.local-error.log"
    CustomLog "/var/log/apache2/php5217.local-access.log" common


    ServerAdmin webmaster@php5328.local
    DocumentRoot "/var/www/sites/5328/test.local"
    ServerName php5328.local
    ErrorLog "/var/log/apache2/php5328.local-error.log"
    CustomLog "/var/log/apache2/php5328.local-access.log" common


    ServerAdmin webmaster@php5429.local
    DocumentRoot "/var/www/sites/5429/test.local"
    ServerName php5429.local
    ErrorLog "/var/log/apache2/php5429.local-error.log"
    CustomLog "/var/log/apache2/php5429.local-access.log" common


    ServerAdmin webmaster@php5514.local
    DocumentRoot "/var/www/sites/5514/test.local"
    ServerName php5514.local
    ErrorLog "/var/log/apache2/php5514.local-error.log"
    CustomLog "/var/log/apache2/php5514.local-access.log" common

保存配置后,创建各站点的DocumentRoot目录,再往每个目录放置一个phpinfo的测试文件,完成后重启Apache服务器并在本地hosts文件加入域名解析,现在就可以访问各站点来测试多版本PHP共存了。

图像

好了,基本的多版本PHP共存解决方案已经完成,如果还需要添加其他的PHP类库支持,后续自己再调用对应php目录下的pecl, php_config等脚本编译安装就可以了。


原文:http://www.ciandt.com.cn/blog/php-multiple-version-configuration
阅读(2282) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~