Is there way to use two PHP versions in XAMPP? Ask Question

Is there way to use two PHP versions in XAMPP? Ask Question

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).

ベストアンサー1

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 4 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:\xampp\php56. The thread safe version does not include php-cgi.exe.

Step 2: Configure php.ini

Open the file c:\xampp\php56\php.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"

また、Apache設定に次の行が存在する場合httpd-xampp.conf

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

先頭に # (ハッシュ文字) を付けてコメントアウトします。

ステップ3: Apacheを設定する

xampp コントロール パネルを開き、apache の config ボタンをクリックし、 をクリックしますApache (httpd-xampp.conf)。テキスト ファイルが開きます。ファイルの下部に次の設定を記述します。

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>

注: 必要に応じて、手順 1 ~ 3 に従って、xampp インストールに PHP の他のバージョンを追加できます。

ステップ 4 (オプション 1): [特定の PHP バージョンを実行するためのディレクトリを追加する]

これで、PHP 5.6 で実行されるディレクトリを設定できます。ディレクトリを設定するには、設定ファイルの下部 (httpd-xampp.conf手順 3) に次のコードを追加するだけです。

<Directory "C:\xampp\htdocs\my_old_project1">
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php56-cgi
    </FilesMatch>
</Directory>

<Directory "C:\xampp\htdocs\my_old_project2">
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php56-cgi
    </FilesMatch>
</Directory>

ステップ 4 (オプション 2): [別のポートで古いバージョンの PHP を実行する]

次に、ポート 8056 で PHP v5.6 を設定するには、設定ファイルの下部に次のコードを追加します (httpd-xampp.conf手順 3 から)。

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

ステップ 4 (オプション 3): [仮想ホストで古いバージョンの PHP を実行する]

http://localhost56 で PHP v5.6 を使用するためにディレクトリ (htdocs56) に仮想ホスト (localhost56) を作成するには、目的の場所にディレクトリ htdocs56 を作成し、hosts ファイルに localhost56 を追加し (方法を参照)、次のコードを構成ファイルの下部に追加します (httpd-xampp.conf手順 3 を参照)。

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

終了:保存してApacheを再起動

設定ファイルを保存して閉じます。xampp コントロール パネルから Apache を再起動します。オプション 2 を選択した場合は、xampp コントロール パネルに追加のポート (8056) が表示されます。

設定されたポートで実行中の Apache を示す xampp コントロール パネル

おすすめ記事