CentOS 6 + Apache 环境下,PHP 5.2 与 5.4 共存

别问我为什么还要用 5.2 ,我会告诉你是因为 ZendOPT 么。

言归正传。在 Linux 下,要装两个不同版本的 php 可不是下载两个 exe 那么简单。这里采用的方法,是手工编译 php 5.2 并使用 epel 源中的 php 5.4 。前者采用 fastcgi 模式运行,后者采用传统的 mod_php 来运行。

现在我们假设,环境中已经装好了 Apache 2.2 + MySQL 5.5 + PHP 5.4 ,并且希望加装一个 PHP 5.2.17 ,并且能够直接使用 MySQL 5.5。

首先需要安装 epel 。

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

然后安装依赖:

yum install httpd-devel libxml-delvel openssl-devel bzip2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel libmhash-devel libtool-ltdl-devel

然后需要自己编译安装 libiconv,如下。

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar -xvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure
make
make install

然后编译安装 PHP ,记得打开 cgi 选项。PHP 源码包请到官网下载。

./configure --prefix=/usr/local/php52 --with-config-file-path=/usr/local/php52/etc --with-config-file-scan-dir=/usr/local/php52/etc/php.d --with-openssl --with-zlib --with-bz2 --with-gettext --with-mhash --with-mcrypt --with-iconv=/usr/local/libiconv --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-mysql=/usr/bin/mysql --with-pdo-mysql=/usr/bin/mysql --enable-gd-native-ttf --enable-bcmath --enable-mbstring --enable-zip --enable-soap --enable-sockets --enable-ftp --without-pear --enable-fastcgi --enable-force-cgi-redirect
make
make install

这样 PHP 5.2.17 就被安装到了 /usr/local/php52 里了。

/usr/loca/php52/bin/php -v

然后配置 fastcgi 。 找到 ~/public_html/cgi-bin/ 目录,创建 php52.fcgi 文件如下:

#!/bin/sh
export PHP_FCGI_CHILDREN=4
export PHP_FCGI_MAX_REQUESTS=200
export PHPRC="/home/oott123/conf/php.ini"
exec /usr/local/php52/bin/php-cgi

其中 PHPRC 为用户定义的 php.ini 文件。再创建 .htaccess 文件(此处需要 AllowOverrideAll )如下:

<FilesMatch \.php$>
    SetHandler application/x-httpd-php-52
</FilesMatch>
Action application/x-httpd-php-52 /cgi-bin/php52.fcgi

如果需要再加入 php 5.4 的支持,再添加如下代码:

<FilesMatch \.php54$>
    SetHandler application/x-httpd-php
</FilesMatch>

至此已经完成了 PHP 多版本的配置。

评论

发表评论

发表评论代表你授权本网站存储并在必要情况下使用你输入的邮箱地址、连接本站服务器使用的 IP 地址和用户代理字符串 (User Agent) 用于发送评论回复邮件,以及将上述信息分享给 Libravatar Akismet,用于显示头像和反垃圾。