现在 PHP 其中一个很大的问题, 是很多个不同的 PHP 版本都有人使用, 对于开发者需要顾及程式码是否可以在不同版本执行, 而对系统管理员, 就要面对不同程式可能需要不同 PHP 版本。
以下记录一下在系统安装不同 PHP 版本, 并可以按需要设定不同 Virtualhost 或目录, 可以使用不同 PHP 版本的方法, 以下会以 CentOS 7 及 Apache 作为安装环境。
首先在系统用 YUM 安装好 Apache 及 PHP, 可以参考本站另一篇文章: RHEL 及 CentOS 7 安装 Apache, MariaDB, PHP(LAMP)
上面安装的 PHP 是 Redhat 7 默认的 PHP 5.4, 并透过 mod_php 执行。下面会安透过 mod_fcgid 执行不同 PHP 版本, 先安装 mod_fcgid 及其他所需套件:
curl-devel libmcrypt-devel libjpeg-devel \
libpng-devel gd-devel mysql-devel mod_fcgid
现在下载及编译 PHP 源始码, 以下会以 PHP 5.6 为例子:
# wget http://hk1.php.net/get/php-5.6.29.tar.gz/from/this/mirror
# tar zxvf mirror
# cd php-5.6.29
以下是 PHP 编译选择
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
./configure --prefix=/usr/local/php56 \ --with-config-file-path=/etc/php56 \ --with-config-file-scan-dir=/etc/php56/php.d \ --with-libdir=lib64 \ --with-mysql \ --with-mysqli \ --enable-mbstring \ --disable-debug \ --disable-rpath \ --with-bz2 \ --with-curl \ --with-gettext \ --with-iconv \ --with-openssl \ --with-gd \ --with-mcrypt \ --with-pcre-regex \ --with-zlib \ --enable-cgi |
执行以下指令编译:
# make install
# mkdir /etc/php56
# cp php.ini-production /etc/php56/php.ini
开启 PHP 5.6 的 php.ini:
在最后的位置加入这行:
|
1 |
cgi.fix_pathinfo = 1 |
建立执行 PHP 5.6 的帐号及群组, 以下用 web1 作为例子:
# useradd -s /bin/false -d /var/www/web1 -m -g web1 web1
# chmod 755 /var/www/web1
然后建立储存 html 的位置, 及设定 owner:
# chown web1:web1 /var/www/web1/html
在一个 wrapper script 在 /var/www/php-fcgi-scripts
# vi /var/www/php-fcgi-scripts/web1/php-fcgi-starter
输入以下内容:
|
1 2 3 4 5 6 |
#!/bin/sh PHPRC=/etc/ export PHPRC export PHP_FCGI_MAX_REQUESTS=5000 export PHP_FCGI_CHILDREN=8 exec /usr/local/php56/bin/php-cgi |
储存盘案后离开编辑器, 现在给 php-fcgi-starter 执行权限:
# chown -R web1:web1 /var/www/php-fcgi-scripts/web1
现在设定 Virtualhost, 开启档案 /etc/httpd/conf/httpd.conf:
在档案最后加上这段:
|
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 |
<VirtualHost *:80> ServerName web1.example.com DocumentRoot /var/www/web1/html/ <IfModule mod_fcgid.c> SuexecUserGroup web1 web1 <Directory /var/www/web1/html/> Options +ExecCGI AllowOverride All Order allow,deny Allow from all </Directory> </IfModule> </VirtualHost> <Directory /var/www/web1/html/php56> <FilesMatch \.php$> SetHandler None ForceType text/plain </FilesMatch> <FilesMatch \.phps$> SetHandler application/x-httpd-php-source </FilesMatch> AddHandler fcgid-script .php FCGIWrapper /var/www/php-fcgi-scripts/web1/php-fcgi-starter .php </Directory> |
重新启动 Apache:
放一个简单的 PHP 测试程式到 /var/www/web1/html:
加入以下内容:
|
1 2 3 |
<?php phpinfo(); ?> |
用浏览器查看, 应该看到默认 PHP 5.4 的 phpinfo 页面。将同一个 info.php 放到 /var/www/web1/html/php56/ 目录下, 应该会看到 PHP 5.6 的 phpinfo 页面.
在 VirtualHost 部份如果没有设定 FCGIWrapper, 会以默认的 PHP 5.4 执行, 要安装其他 PHP 版本, 只要重复以下编译的步骤即可。
您好, 请问要如何切换2个站台, 我试着用网址好像不行.., 也可以正常启用, 现在变成 /var/www/web1/html/php56/ 加一个info.php , 变成是PHP5.4的资讯, 🙁