如何让多个PHP版本在同一个apache切换php版本服务下并存

(window.slotbydup=window.slotbydup || []).push({
id: '3284507',
container: s,
size: '0,0',
display: 'inlay-fix'
PHP多版本共存解决方案图解
我这里说的php多版本共存不仅仅是php4、php5两个大版本号程序的共存,而是php的很多小版本号程序的共存,例如php4.3.1、php4.4.5、php5.2.0、php5.2.9等等,不仅如此,由于测试的需求,我还需要做到php的版本可以随时灵活切换。
目前网上很多的解决方案是在本机安装IIS和Apache或安装多个apache服务,每个服务器安装一个版本的php来达到版本共存的要求,但是要按照我的要求,这么做既麻烦又复杂(想想要调试10个版本的php怎么办)。既然网上没有,我就只好自己来研究,下面就是我摸索出的完美解决方案:
实现思路:Apache + .htaccess + PHP CGI
众所周知,php cgi的配置代码如下
ScriptAlias /php/ &D:/WebService/php-5.2.8-Win32/&
AddType application/x-httpd-php .php
Action application/x-httpd-php &/php/php-cgi.exe&
ScriptAlias的解释
映射一个URL到文件系统并视之为CGI脚本
The ScriptAlias directive has the same behavior as the Alias directive, except that in addition it marks the target directory as containing CGI scripts that will be processed by mod_cgi's cgi-script handler. URLs with a case-sensitive (%-decoded) path beginning with URL-path will be mapped to scripts beginning with the second argument, which is a full pathname in the local filesystem.
Alias指令使文档可以被存储在DocumentRoot以外的本地文件系统中。以(%已解码的)url-path路径开头的URL可以被映射到以directory-path开头的本地文件
那这里,我们就可以从ScriptAlias下手,来映射不同版本php文件夹
在httpd.conf文件中我们可以这样写
ScriptAlias /php5.2.8/ &D:/WebService/php-5.2.8-Win32/&
ScriptAlias /php5.2.7/ &D:/WebService/php-5.2.7-Win32/&
ScriptAlias /php5.2.0/ &D:/WebService/php-5.2.0-Win32/&
ScriptAlias /php4.3.1/ &D:/WebService/php-4.3.1-Win32/&
ScriptAlias /php4.4.6/ &D:/WebService/php-4.4.6-Win32/&
这样我们就映射了许多的虚拟脚本路径,如果我们需要使用某个版本的php 就可以修改 Action application/x-httpd-php 的虚拟路径来实现了。
例如,我们想使用4.3.1版本的php,修改Action application/x-httpd-php为
Action application/x-httpd-php &/php4.3.1/php.exe&
但是直接把配置代码写在配置文件中,每次修改PHP版本都要重启Apache才可以,而且每次只能使用一个版本的PHP,使用起来还是非常的不方便。
这个时候我想到了apache的.htaccess文件,在Apache里面.htaccess文件提供了针对每个目录改变配置的方法,可以让同一个服务器下不同目录使用不同的配置方案。
我们在服务器目录下放置一个.htaccess文件,内容写上
Action application/x-httpd-php &/php5.2.8/php-cgi.exe&
那么当前目录Apache就会采用php5.2.8来解析php程序
同理,我们在服务器上为每一个版本的php建立一个文件夹或者虚拟主机,然后在目录中放置一个设定好php版本的.htaccess文件即可实现同一服务器运行不同版本php程序。
下面是配置步骤
1、下载自己需要版本的php程序文件,配置好每个版本php的php.ini文件,将它们放在各自文件夹的根目录即可。
然后把这些文件统一的放置在一个文件夹里(放哪其实无所谓,你找得到就行),由于我们配置的php是以cgi形式运行的,所以我们不需要拷贝任何文件到系统目录下面。
然后在Apache的配置文件中按照以下格式填写,有几个版本就写几行
ScriptAlias /php版本号/ &php版本程序文件绝对物理路径&
ScriptAlias /php5.2.8/ &D:/WebService/php-5.2.8-Win32/&
ScriptAlias /php5.2.7/ &D:/WebService/php-5.2.7-Win32/&
ScriptAlias /php5.2.0/ &D:/WebService/php-5.2.0-Win32/&
ScriptAlias /php4.3.1/ &D:/WebService/php-4.3.1-Win32/&
ScriptAlias /php4.4.6/ &D:/WebService/php-4.4.6-Win32/&
写完之后,再增加以下代码
AddType application/x-httpd-php .php
2、为了.htaccess文件起作用,我们需要在httpd.conf中找到 &Directory /& &/Directory& 节点将里面的 AllowOverride 属性修改为 AllowOverride All.
这样设置后apache下所有的文件目录默认情况下就会支持.htaccess的配置了。
如果你的网站目录有使用&Directory 路径/& &/Directory&标签来单独设置,请自行查找该目录的 AllowOverride 属性设置并修改。
设定完毕后请重启Apache服务。
3.到你想指定php版本的网站目录中放置一个.htaccess文件,在文件中写入以下代码
如果是php4,需要这么写
Action application/x-httpd-php &/php版本号/php.exe&
如果是php5,需要这么写
Action application/x-httpd-php &/php版本号/php-cgi.exe&
例如,我需要php4.3.1来解析,那么就这么写
Action application/x-httpd-php &/php4.3.1/php.exe&
写一个phpinfo看看当前版本,然后可以尝试修改.htaccess中php的版本映射路径后刷新页面,怎么样,版本信息立马变化,不需要再次重启apache了
标签(Tag):
------分隔线----------------------------
------分隔线----------------------------如何在Ubuntu上在多个PHP版本之间切换 - ThinkPHP框架
在您的系统上,如果您已安装了多个版本的PHP(例如PHP 7.1和PHP 5.6两者)。 PHP 7.1作为Apache和CLI的默认PHP运行。对于任何需求,您需要使用PHP 5.6。然后你不需要删除php 7.1。你可以简单地切换php版本为默认用于Apache和命令行。
例如,您的服务器安装了PHP 7.1和PHP 5.6两个版本。现在下面的例子将帮助你在两个版本之间切换。
从PHP 5.6 =& PHP 7.1
默认PHP 5.6在您的系统上设置,您需要切换到PHP 7.1。
Apache: –
$ sudo a2dismod php5.6
$ sudo a2enmod php7.1
$ sudo service apache2 restart
$ update-alternatives --set php /usr/bin/php7.1
从PHP 7.1 =& PHP 5.6
默认PHP 7.1在您的系统上设置,您需要切换到PHP 5.6。
Apache: –
$ sudo a2dismod php7.1
$ sudo a2enmod php5.6
$ sudo service apache2 restart
$ sudo update-alternatives --set php /usr/bin/php5.6
ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。Apache搭建多个站点方法详解-apache-Php教程-壹聚教程网Apache搭建多个站点方法详解
Apache的虚拟主机是一种允许在同一台机器上配置多个不同站点的web服务器环境的,就是iis一样可以创建多站点了,但是apache需要在编辑状态操作,不能像windows iis直接点击几下就好了,下面我来给各位介绍配置方法。
最平常的大概有3种方法。
第一种:单IP不同端口
第二种:多IP同端口(独立IP的虚拟空间)
第三种:域名绑定根目录的方式(共享IP的虚拟空间)
Apache的核心配置文件名是&httpd.conf&,其所存放的路径在Apache目录下的conf文件夹下。修改它只需要使用记事本(建议使用其他编辑器,带行数的那种,方便修改),生效的话只需要保存httpd.conf,重启即可。
下面多站点支持的话,修改httpd.conf的第187~264行(不同的httpd.conf可能有差异),也就是在ServerAdmin和ServerName那里,大部分是注释。下面是主要修改的地方。
注意:如果是服务器请备份httpd.conf后再修改文件。
# 'Main' server configuration
# The directives in this section set up the values used by the 'main'
# server, which responds to any s that aren't handled by a
# &VirtualHost& definition.& These values also provide defaults for
# any &VirtualHost& containers you may define later in the file.
# All of these directives may appear inside &VirtualHost& containers,
# in which case these default settings will be overridden for the
# virtual host being defined.
# ServerAdmin: Your address, where problems with the server should be
# e-mailed.& This address appears on some server-generated pages, such
# as error documents.& e.g. admin@
ServerAdmin
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
# If your host doesn't have a registered DNS name, enter its IP address here.
ServerName :80
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# &Directory& blocks below.
&Directory /&
&&& AllowOverride All
&&& Require all denied
&/Directory&
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
DocumentRoot &g:/www&
&Directory &g:/www&&
&&& # Possible values for the Options directive are &None&, &All&,
&&& # or any combination of:
&&& #&& Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
&&& # Note that &MultiViews& must be named *explicitly* --- &Options All&
&&& # doesn't give it to you.
&&& # The Options directive is both complicated and important.& Please see
&&& # http://httpd.apache.org/docs/2.4/mod/core.html#options
&&& # for more information.
&&& Options Indexes FollowSymLinks
&&& # AllowOverride controls what directives may be placed in .htaccess files.
&&& # It can be &All&, &None&, or any combination of the keys:
&&& #&& Options FileInfo AuthConfig Limit
&&& AllowOverride All
&&& # Controls who can get stuff from this server.
&&& Require all granted
&/Directory&
第一种一般是测试环境,毕竟加了端口,如何绑定域名,访问的时候域名后面也需加端口。
例子分别通过80和8080访问不同的根目录。
大概在50几行有个Listen 80,在下面添加8080端口。
Listen 8080&VirtualHost *:80&
&&& ServerAdmin
&&& ServerName localhost:80
&&& DocumentRoot &g:/www1&
&&&& &Directory &g:/www1&&
&&&& Options& Indexes FollowSymLinks
&&&& AllowOverride All
&&&& Require all granted
&& &/Directory&&&
&/VirtualHost&
&VirtualHost *:8080&
&&& ServerAdmin
&&& ServerName localhost:8080&
&&& DocumentRoot &g:/www2&
&& &Directory &g:/www2&&
&&&& Options Indexes FollowSymLinks
&&&& AllowOverride All
&&&& Require all granted
&& &/Directory&&&&&&&
&/VirtualHost&
第二种多IP同端口。
IP地址1:192.168.2.2
IP地址2:192.168.1.68
端口同是80端口。
&VirtualHost 192.168.1.68:80&
&&& ServerAdmin
&&& ServerName localhost:80
&&& DocumentRoot &g:/www1&
&&&& &Directory &g:/www1&&
&&&& Options FollowSymLinks
&&&& AllowOverride All
&&&& Require all granted
&& &/Directory&&&
&/VirtualHost&
&VirtualHost 192.168.2.2:80&
&&& ServerAdmin
&&& ServerName localhost:80
&&& DocumentRoot &g:/www2&
&& &Directory &g:/www2&&
&&&& Options FollowSymLinks
&&&& AllowOverride All
&&&& Require all granted
&& &/Directory&&&&&&&
&/VirtualHost&
第三种同IP不同域名和根目录(域名的话修改本地host演示)。
&VirtualHost 192.168.2.2:80&
&&& ServerAdmin
&&& ServerName www.111cn.net
&&& DocumentRoot &g:/www1&
&&&& &Directory &g:/www1&&
&&&& Options FollowSymLinks
&&&& AllowOverride All
&&&& Require all granted
&& &/Directory&&&
&/VirtualHost&
&VirtualHost 192.168.2.2:80&
&&& ServerAdmin
&&& ServerName www.111cn.net
&&& DocumentRoot &g:/www2&
&& &Directory &g:/www2&&
&&&& Options FollowSymLinks
&&&& AllowOverride All
&&&& Require all granted
&& &/Directory&&&&&&&
&/VirtualHost&
上一页: &&&&&下一页:相关内容

我要回帖

更多关于 apache切换php版本 的文章

 

随机推荐