一个蛋疼的需求是,使用ZendGuard5.5.0加密的程序,需要PHP5.3版本才能运行,但使用Zend旧版本加密的程序,却需要PHP5.2才能运行。
而其中比较蛋疼的程序就有鼎鼎大名的ShopEX,作为用户,你完全可以放弃一款蛋疼的程序而选择同类程序。但作为主机商,就没有办法让所有用户都换掉网站程序了。
有一种可行的方案是,在服务器上进行一些设置,然后用户可以使用.htaccess文件切换PHP版本。
我们有两种方案可选:
默认PHP5.2 CLI模式,用户通过.htaccess切换到PHP5.3 CGI模式;
默认PHP5.3 CGI模式,用户通过.htaccess切换到PHP5.2 CLI模式;
其实我更希望两个PHP版本都运行在CGI模式,但没有找到更好的方法来实现。
默认PHP5.2 CLI模式方案
首先进入custombuild目录:
cd /usr/local/directadmin/custombuild
设置custombuild:
./build set custombuild 1.2
更新程序:
./build update
创建suphp文件夹:
mkdir -p custom/suphp
复制configure.php5文件到刚创建的目录,并重命名为configure.php6:
cp -pf configure/suphp/configure.php5 custom/suphp/configure.php6
替换versions.txt文件中的一些信息:
perl -pi -e 's/php53/phprep/' versions.txt
perl -pi -e 's/php6/php53/' versions.txt
perl -pi -e 's/phprep/php6/' versions.txt
设置PHP版本:
./build set php5_ver 5.2
./build set php5_cli yes
./build set php5_cgi no
./build set php6_cgi yes
./build set php6_cli no
编译PHP:
./build php n
移动php6目录到php6_old目录,主要是备份一下:
mv -f /usr/local/php6 /usr/local/php6_old
建立php6目录与php5目录的连接:
ln -s /usr/local/php5 /usr/local/php6
重启Apache:
service httpd restart
如果启动不了,请执行:
./build rewrite_confs
用户在public_html目录下添加.htaccess文件即可完成PHP版本切换:
<FilesMatch "\.php$">
AddHandler x-httpd-php6 .php
</FilesMatch>
默认PHP5.3 CGI模式方案
首先进入custombuild目录:
cd /usr/local/directadmin/custombuild
设置custombuild:
./build set custombuild 1.2
更新程序:
./build update
创建suphp文件夹:
mkdir -p custom/suphp
复制configure.php5文件到刚创建的目录,并重命名为configure.php6:
cp -pf configure/suphp/configure.php5 custom/suphp/configure.php6
替换versions.txt文件中的一些信息:
perl -pi -e 's/php53/phprep/' versions.txt
perl -pi -e 's/php6/php53/' versions.txt
perl -pi -e 's/phprep/php6/' versions.txt
设置PHP版本:
./build set php5_ver 5.3
./build set php5_cli no
./build set php5_cgi yes
./build set php6_cgi no
./build set php6_cli yes
编译PHP:
./build php n
移动php6目录到php6_old目录,主要是备份一下:
mv -f /usr/local/php6 /usr/local/php6_old
建立php6目录与php5目录的连接:
ln -s /usr/local/php5 /usr/local/php6
修改httpd-phpmodules.conf文件:
sed -i 's/php6/php5/g' /etc/httpd/conf/extra/httpd-phpmodules.conf
重启Apache:
service httpd restart
用户在public_html目录添加.htaccess文件即可切换PHP版本:
<FilesMatch "\.(inc|php|php3|php4|php5|php6|phtml|phps)$">
SetHandler application/x-httpd-php
</FilesMatch>
常见错误处理
在编译PHP时(也就是执行./build php n命令时),提示以下错误:
checking for icu-config… no
checking for location of ICU headers and libraries… not found
configure: error: Unable to detect ICU prefix or no failed. Please verify ICU install prefix and make sure icu-config works.
解决方法:编辑configuration.php6文件:
vim custom/suphp/configure.php6
添加以下行:
"--with-icu-dir=/usr/local/icu" \
还有可能提示以下错误:
checking for external libmbfl… no
configure: error: multibyte regex support must be disabled, use –disable-mbregex
解决方法:编辑configuration.php6文件:
vim custom/suphp/configure.php6
添加以下行:
"--disable-mbregex" \
然后重新编译就可以了:
./build php n
如果一切都没有错,但使用phpinfo查看到的php版本不对应,请执行以下命令:
./build update_versions