有没有办法在 XAMPP 中使用两个 PHP 版本?

2022-01-14 00:00:00 xampp php

我们使用 PHP 7.0 运行 XAMPP,因为我们的新产品需要 PHP 7.

但是有一些旧项目使用像 mysql_connect 等函数.这些在 PHP 7.0 中被删除了.

那么,有没有办法在 XAMPP 中轻松更改 PHP 版本?

<块引用>

注意:请不要建议升级旧项目以兼容新版本,因为我无法这样做作为一名开发人员(只是一名员工),我无法做出决定.

解决方案

单个 xampp 安装可以同时使用多个 PHP 版本,为什么要在 PHP 版本之间切换em>?

使用单个 xampp 安装,您有 2 个选项:

  1. 仅为旧项目的目录运行较旧的 PHP 版本:这将在大多数情况下达到目的.您可能有一个或两个旧项目打算使用较旧的 PHP 版本运行.只需将 xampp 配置为仅针对这些项目目录运行较旧的 PHP 版本.

  2. 在单独的 xampp 端口上运行较旧的 PHP 版本: 有时您可能正在将旧项目升级到最新的 PHP 版本,同时您需要运行同一个项目在新的 PHP 版本和旧的 PHP 版本之间来回切换.为此,您可以在不同的端口(例如 8056)上设置较旧的 PHP 版本,因此当您转到 http://localhost/any_project/ 时,xampp 运行 PHP 7,而当您转到 http://localhost:8056/any_project/ xampp 运行 PHP 5.6.

  3. 在虚拟主机上运行较旧的 PHP 版本:您可以创建一个类似 localhost56 的虚拟主机来运行 PHP 5.6,同时您可以在 localhost 上使用 PHP 7.

让我们设置一下

第 1 步:下载 PHP

所以你有 PHP 7 在 xampp 下运行,你想向它添加一个旧的 PHP 版本(比如 PHP 5.6).从

We are running XAMPP with PHP 7.0 because our new products requires PHP 7.

But there are old projects which use functions like mysql_connect, etc. Those are removed in PHP 7.0.

So, is there a way to easily change PHP versions in XAMPP?

Note: Please don't suggest to upgrade old project to compatible with new versions because I am not in a position to do it because of that decisions I can't get as a developer (just an employee).

解决方案

Why switch between PHP versions when you can use multiple PHP versions at the same time with a single xampp installation?

With a single xampp installation, you have 2 options:

  1. Run an older PHP version for only the directory of your old project: This will serve the purpose most of the time. You may have one or two old projects that you intend to run with an older PHP version. Just configure xampp to run an older PHP version for only those project directories.

  2. Run an older PHP version on a separate port of xampp: Sometimes you may be upgrading an old project to the latest PHP version and at the same time you need to run the same project back and forth between the new PHP version and the old PHP version. To do this you can set an older PHP version on a different port (say 8056) so when you go to http://localhost/any_project/, xampp runs PHP 7 and when you go to http://localhost:8056/any_project/ xampp runs PHP 5.6.

  3. Run an older PHP version on a virtualhost: You can create a virtualhost like localhost56 to run PHP 5.6 while you can use PHP 7 on localhost.

Lets set it up

Step 1: Download PHP

So you have PHP 7 running under xampp, you want to add an older PHP version to it (say PHP 5.6). Download the nts (Non Thread Safe) version of the PHP zip archive from php.net (see archive for older versions) and extract the files under c:xamppphp56. The thread safe version does not include php-cgi.exe.

Step 2: Configure php.ini

Open the file c:xamppphp56php.ini in notepad. If the file does not exist, copy php.ini-development to php.ini and open it in notepad. Then uncomment the following line:

extension_dir = "ext"

Also if the following line exists in Apache config httpd-xampp.conf

SetEnv PHPRC "\path\to\xampp\php"

comment it out with with a leading # (hash character).

Step 3: Configure apache

Open xampp control panel, click the config button for apache, and click Apache (httpd-xampp.conf). A text file will open. Put the following settings at the bottom of the file:

ScriptAlias /php56 "C:/xampp/php56"
Action application/x-httpd-php56-cgi /php56/php-cgi.exe
<Directory "C:/xampp/php56">
    AllowOverride None
    Options None
    Require all denied
    <Files "php-cgi.exe">
        Require all granted
    </Files>
</Directory>

Note: You can add more versions of PHP to your xampp installation following step 1 to 3 if you want.

Step 4 (option 1): [Add Directories to run a specific PHP version]

Now you can set directories that will run in PHP 5.6. Just add the following at the bottom of the config file (httpd-xampp.conf from Step 3) to set directories.

<Directory "C:xampphtdocsmy_old_project1">
    <FilesMatch ".php$">
        SetHandler application/x-httpd-php56-cgi
    </FilesMatch>
</Directory>

<Directory "C:xampphtdocsmy_old_project2">
    <FilesMatch ".php$">
        SetHandler application/x-httpd-php56-cgi
    </FilesMatch>
</Directory>

Step 4 (option 2): [Run an older PHP version on a separate port]

Now to to set PHP v5.6 on port 8056, add the following code to the bottom of the config file (httpd-xampp.conf from Step 3).

Listen 8056
<VirtualHost *:8056>
    <FilesMatch ".php$">
        SetHandler application/x-httpd-php56-cgi
    </FilesMatch>
</VirtualHost>

Step 4 (option 3): [Run an older PHP version on a virtualhost]

To create a virtualhost (localhost56) on a directory (htdocs56) to use PHP v5.6 on http://localhost56, create directory htdocs56 at your desired location and add localhost56 to your hosts file (see how), then add the following code to the bottom of the config file (httpd-xampp.conf from Step 3).

<VirtualHost localhost56:80>
    DocumentRoot "C:xampphtdocs56"
    ServerName localhost56
    <Directory "C:xampphtdocs56">
        Require all granted    
    </Directory>
    <FilesMatch ".php$">
        SetHandler application/x-httpd-php56-cgi
    </FilesMatch>
</VirtualHost>

Finish: Save and Restart Apache

Save and close the config file. Restart apache from the xampp control panel. If you went for option 2, you can see the additional port(8056) listed in your xampp control panel.

相关文章