Reputation: 3575
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).
Upvotes: 285
Views: 431549
Reputation: 17536
With a single xampp installation, you have 4 options:
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.
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.
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.
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"
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:\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>
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:\xampp\htdocs56"
ServerName localhost56
<Directory "C:\xampp\htdocs56">
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.
Upvotes: 347
Reputation: 81
Might i suggest using the Xampp PHP Switcher by JackieDo.
After the very easy installation you can use the command xphp switch [VERSION]
to change the php version.
Upvotes: 1
Reputation: 453
I was also in the same situation recently and I solved it with the following steps:
Step 1: Download the nts (Non Thread Safe) version of the PHP zip archive from php.net. Then extract it in xampp folder, same level as default php folder. The xampp folder will now look like this:
Step 2:
php
folder. Coppy file php.ini-development
to php.ini
and open it with any editor (notepad, notepadd++...). Press key Ctrl+F
combination and find text: extension_dir = "ext"
then uncomment this line by remove the semicolon at the beginning of the line.extension=curl
extension=ftp
extension=fileinfo
extension=mysqli
extension=openssl
extension=pdo_mysql
Step 3:
xampp
folder, open the file httpd-xampp.conf
with the path {disk_name}/xampp/apache/conf/extra/httpd-xampp.conf
( replace {disk name}
with drive disk where you installed xampp).httpd-xampp.conf
will now be D:\xampp\apache\conf\extra\httpd-xampp.conf
.httpd-xampp.conf
).httpd-xampp.conf
file, then change /php8_1
and D:/xampp/php8_1/
to the name and path to your php folder.ScriptAlias /php8_1/ "D:/xampp/php8_1/"
Action application/x-httpd-php8_1-cgi "/php8_1/php-cgi.exe"
<Directory "D:/xampp/php8_1">
AllowOverride None
Options None
Require all denied
<Files "php-cgi.exe">
Require all granted
</Files> SetEnv PHPRC "D:/xampp/php8_1"
</Directory>
php
folder name is php6_5
and folder path is C:/xampp/php6_5/
then the code will be:ScriptAlias /php6_5 "C:/xampp/php6_5/"
Action application/x-httpd-php6_5-cgi "/php6_5/php-cgi.exe"
<Directory "C:/xampp/php6_5">
AllowOverride None
Options None
Require all denied
<Files "php-cgi.exe">
Require all granted
</Files>
SetEnv PHPRC "C:/xampp/php6_5/"
</Directory>
XAMPP
installation following step 1 to 3 if you want.Step 4: Add directories to run a specific PHP version
httpd-xampp.conf
from Step 3) to set directories.<Directory "C:\xampp\htdocs\my_project1">
<FilesMatch "\.php$">
SetHandler application/x-httpd-php5_6-cgi //replace php5_6 with your version php
</FilesMatch>
</Directory>
<Directory "C:\xampp\htdocs\my_project2">
<FilesMatch "\.php$">
SetHandler application/x-httpd-php8_1-cgi //replace php8_1with your version php
</FilesMatch>
</Directory>
Step 5: Save and restart XAMPP
index.php
file and <?php echo phpinfo()
to check the php version.Now, I will show my result
Upvotes: 8
Reputation: 393
Simple and Easiest Steps is
Upvotes: 2
Reputation: 5997
Follow these easy steps. I am currently running XAMPP on PHP 7.2 but need PHP 5.6 to work on old projects.
STEP 1
Download Thread Safe version of PHP on https://windows.php.net/download
Put files on your [Drive]:\xampp\php5.6
STEP 2
Copy [Drive]:\xampp\apache\conf\extra\httpd-xampp.conf
Rename the file depending on the new php version and put it here: [Drive]:\xampp\apache\conf\extra\httpd-xampp5.6.conf
STEP 3
Edit the newly created 'httpd-xampp5.6.conf'
Basically, you need to change All the PHP sources and .dll
Before
LoadFile "C:/xampp/php/php7ts.dll"
LoadFile "C:/xampp/php/libpq.dll"
LoadModule php7_module "C:/xampp/php/php7apache2_4.dll"
After
LoadFile "C:/xampp/php5.6/php5ts.dll"
LoadFile "C:/xampp/php5.6/libpq.dll"
LoadModule php5_module "C:/xampp/php5.6/php5apache2_4.dll"
Here is my file: https://gist.github.com/mpalencia/f8a20c31bffb02fe20d371218c23d1ec
STEP 4
Edit the file [Drive]:\xampp\apache\conf\httpd.conf
Before
# XAMPP settings
Include "conf/extra/httpd-xampp.conf"
After
# XAMPP settings
Include "conf/extra/httpd-xampp5.6.conf"
STEP 5
Edit your PHP 5.6 configuration - php.ini
Add you extension directory: extension_dir = "C:\xampp\php5.6\ext"
STEP 6
Start Apache
STEP 7
Edit PHP environment variable path on Windows
Upvotes: 4
Reputation: 1239
Versions of PHP can be specified in .htaccess files. This means that while most projects can be left to default to the global configuration in the httpd.conf file, specific projects can be set to a different version simply by editing .htaccess.
The advantage of using .htaccess is that different PHP versions can be used for different projects without needing to keep making changes to httpd.conf to specify which PHP is needed by different project directories. Instead, the change can be made directly within the project itself, simply by editing its .htaccess file.
The exact line needed will vary depending on your setup. For me, using Fast CGI, I just add the following line in the .htaccess file of those projects for which I want to use PHP 8 (PHP 7 is the default on my server).
FcgidWrapper "C:/xampp/php-8.0.6-nts-Win32-vs16-x64/php-cgi.exe -c C:/xampp/php-8.0.6-nts-Win32-vs16-x64" .php
The first part of the string references the PHP executable, while the -c
option is for the directory path of the required php.ini file. If this is omitted, the globally configured php.ini will be used.
Upvotes: 1
Reputation: 531
I just want to share my new finding: https://laragon.org/docs/index.html
I just used it for 1 hour and it looks promising.
How to add another PHP version to Laragon
How to add phpMyAdmin to Laragon
I'm not affiliated in any way with Laragon. Just found it on Google looking for "XAMPP Windows alteratives"
Upvotes: 3
Reputation: 129
I do stuck with same problem at time of installing magento2 while it require ~7.3.0 but I have 7.4.1. So, I downgraded php version using this method.
Step 1: Download Php version from here nts version https://windows.php.net/downloads/releases/archives/ and paste this version to c:\xampp\ as named 'php71'
Step 2: Setup Virtual Host Environment and do some other changes. Go to "c:\xampp/\pache\conf\extra\httpd-vhosts.conf" and put code snippet at the end of line
<VirtualHost 127.0.0.1:80>
DocumentRoot "C:/xampp/htdocs/magento/crashcourse/"
ServerName magento2.test
<Directory "C:/xampp/htdocs/magento/crashcourse/">
Require all granted
</Directory>
<FilesMatch "\.php$">
SetHandler application/x-httpd-php71-cgi
</FilesMatch>
</VirtualHost>
GO to "C:\Windows\System32\drivers\etc\hosts" then edit the file with using admin privileges then add the code at end of line.
127.0.0.1 magento2.test
Go to you Apache Config file "c:/xampp/apache/conf/extra/httpd-xampp.conf" and paste below code at end of the line
ScriptAlias /php71 "C:/xampp/php71"
Action application/x-httpd-php71-cgi /php71/php-cgi.exe
<Directory "C:/xampp/php71">
AllowOverride None
Options None
Require all denied
<Files "php-cgi.exe">
Require all granted
</Files>
SetEnv PHPRC "C:/xampp/php71"
</Directory>
Now, all set. Go to url: http://magento2.test all work fine!
Upvotes: 1
Reputation: 586
I know this is the old post but I want to share there is library for still running mysql_connect() in PHP 7. It works by overriding the real function (mysql_connect() is overridden by mysqli_connect() which work on this library).
I found from this video https://www.youtube.com/watch?v=Eqd-jJu4sQ4
hopefully it helps
Upvotes: 0
Reputation: 1213
I am working on a ten years old project at work. So 5.6 is my default. There are also older small projects which only works with 5.2 and I also want to work on new versions for my personal projects with 7.3.8
Here is what I did to add 5.2 and 7.3.8 on default 5.6 version.
1 - Downloaded php TS versions ;
v7.3.8(latest) : https://windows.php.net/download/
2 - Extracted to c:\xampp\php738 and c:\xampp\php52 folder in order
3 - Added these lines to the bottom of httpd-xampp.conf (or httpd.conf) ;
*** IMPORTANT NOTICE : You should NOT use script alias name as your target folder name, they must be different. (for instance /php7x is ok , /php7 is not)
ScriptAlias /php7x "C:/xampp/php738"
Action application/x-httpd-php7-cgi /php7x/php-cgi.exe
<Directory "C:/xampp/php738">
AllowOverride None
Options None
Require all denied
<Files "php-cgi.exe">
Require all granted
</Files>
</Directory>
<Directory "D:\www\php7">
<FilesMatch "\.php$">
SetHandler application/x-httpd-php7-cgi
</FilesMatch>
</Directory>
ScriptAlias /phpold "C:/xampp/php52"
Action application/x-httpd-phpold-cgi /phpold/php-cgi.exe
<Directory "C:/xampp/php52">
AllowOverride None
Options None
Require all denied
<Files "php-cgi.exe">
Require all granted
</Files>
</Directory>
<Directory "D:\www\php52">
<FilesMatch "\.php$">
SetHandler application/x-httpd-phpold-cgi
</FilesMatch>
</Directory>
Some helpful notes :
And as a last bonus note , if you need to set a virtual host with any of versions you can use similar approach. A sample laravel vhost conf for php 7.3.8;
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "D:\www\vhosts\laravel.local\public"
ServerName laravel.local
<FilesMatch "\.php$">
SetHandler application/x-httpd-php7-cgi # escape for default php version
#SetHandler application/x-httpd-phpold-cgi # unescape for php 5.2
</FilesMatch>
ErrorLog "logs/laravel.local.log"
CustomLog "logs/laravel.local.log" combined
Thanks to @Munim Munna for detailed answer, I wanted to share a solid sample with the important notice above.
Upvotes: 9
Reputation: 2410
Use this php switcher
You can control php version to any your project you want via vhost config.
Upvotes: 0
Reputation: 413
This is probably the least technical answer to the question, but it's also the easiest to accomplish.
If you have two drives such as C: and D: you can install a separate instance of XAMPP on both drives with different php versions. This took me less than 10 mins to do and is least error prone.
I just create two desktop shortcuts to both xampp-control.exe and name the shortcuts after their php version. I hope this helps someone like me that prefers a very quick and dirty solution.
Upvotes: 4
Reputation: 69
run this in Command Prompt windows (cmd.exe).
set PATH=C:\xampp\php;%PATH%
change it depending where you put the php 7 installation.
Upvotes: 6
Reputation: 40
I needed to do the same thing, so I googled how and came to stack overflow, where the OP was having the same issue... So my findings.. I tried renaming files from all different directions AND my conclusion was basically it's taking me too long. SOOOO I ended up just installing version 7 from here:
https://www.apachefriends.org/index.html (kill services and quit out of xampp before attempting)
When asked where to put the directory name it like so (give it a different name):
and
DONEZO! Now just make sure to kill services and quit before swapping back and forth and you have 2 sterile XAMPP envs to play in..
Hooray! now I can actually get to work!
Upvotes: 0
Reputation: 21661
I use Uniserver.
It has this feature built in.
It's that simple.
The Uniform Server is a free lightweight WAMP server solution for Windows. Less than 24MB, modular design, includes the latest versions of Apache2, Perl5, PHP (switch between PHP53, PHP54, PHP55 or PHP56), MySQL5 or MariaDB5, phpMyAdmin or Adminer4. No installation required! No registry dust! Just unpack and fire up!
It even runs off a flash drive. Has cron emulation, support for perl, mariaDB, a couple versions of Mysql, filezilla server and a few other things.
Upvotes: 11
Reputation: 534
Maybe a bit late, but I am using a batch to rename the PHP folders (a slightly modified version of the phpswitch I found years ago).
You copy the different folders into your XAMPP installation. Each PHP folder (except for the active one) receives the version number as a suffix (eg. php_5.6.32). In ALL PHP folders, a file (PHP_VERSION) is created, which contains only the respective version number, so the Script can grab this information. But that's all described in the README.
Since PHP7 the httpd-xampp.conf loads the php7ts.dll instead of the php5ts.dll. So i had to extend the script (PHPSwitch.php) to rename these config-files too by following the same approach.
$renameCur = new PHPSwitch_Rename($currInst['path'], $this->_cfg['phpInstallationsPath'] . $this->_cfg['phpDirName'] . '_' . $currInst['version']);
$renameNew = new PHPSwitch_Rename($newInst['path'], $this->_cfg['phpInstallationsPath'] . $this->_cfg['phpDirName']);
$apache_curent = $this->_cfg["phpInstallationsPath"]."apache/conf/extra/httpd-xampp.conf";
$apache_curent_rename = $this->_cfg["phpInstallationsPath"]."apache/conf/extra/httpd-xampp_".$currInst['version'].".conf";
$apache_new = $this->_cfg["phpInstallationsPath"]."apache/conf/extra/httpd-xampp_".$newInst['version'].".conf";
$apache_new_rename = $this->_cfg["phpInstallationsPath"]."apache/conf/extra/httpd-xampp.conf";
$renameCur_apache_conf = new PHPSwitch_Rename($apache_curent, $apache_curent_rename);
$renameNew_apache_conf = new PHPSwitch_Rename($apache_new, $apache_new_rename);
$transaction = new PHPSwitch_Rename_Transaction();
$transaction->add($renameCur);
$transaction->add($renameNew);
$transaction->add($renameCur_apache_conf);
$transaction->add($renameNew_apache_conf);
Upvotes: 0
Reputation: 494
It's possible to have multiple versions of PHP set up with a single XAMPP installation. The instructions below are working for Windows.
C:\xampp\php
become C:\xampp\php-7.1.11
.C:\xampp\php-5.4.45
.Now you need to edit XAMPP and Apache configuration :
C:\xampp\apache\conf\httpd.conf
, locate the XAMPP settings for PHP, you should change it to something such as :Where you have to comment (with #
) the other PHP versions so only one Include
will be interpreted at the time.
#XAMPP settings PHP 7
Include "conf/extra/httpd-xampp.conf.7.1"
#XAMPP settings PHP 5.4.45
#Include "conf/extra/httpd-xampp.conf.5.4.45"
Now in C:\xampp\apache\conf\extra
directory rename httpd-xampp.conf
to httpd-xampp.conf.7.1
and add a new configuration file for httpd-xampp.conf.5.4.45
. In my case, I copied the conf file of another installation of XAMPP for php 5.5 as the syntax may be slightly different for each version.
Edit httpd-xampp.conf.5.4.45
and httpd-xampp.conf.7.1
and replace there all the reference to the php
directory with the new php-X.X
version. There are at least 10 changes to be made here for each file.
You now need to edit php.ini for the two versions. For example for php 7.1, edit C:\xampp\php-7.1.11\php.ini
where you will replace the path of the php directory for include_path
, browscap
, error_log
, extension_dir
..
And that's it. You can now start Apache from XAMPP Control Panel. And to switch from a version to another, you need only to edit C:\xampp\apache\conf\httpd.conf
and change the included PHP version before restarting Apache.
Upvotes: 5
Reputation: 2105
Unless it has to be absolutely and definitely XAMPP, you can try to get what you want with WAMP. WAMP is pretty much the same thing in different package.
Once you have it installed, you can just switch between php versions here:
You can install as many versions of PHP as you need.
Here's how it is done in detail.
Just go here: WAMP download
Then select your base server, e.g. latest with php7.
Then, when WAMP 3 is installed, go to folder: addons and select php version (or versions) you are after. They offer php flavors starting from php 5.3.29, which should work with mysql_connect.
To install addon, during installation (double click exe file) just point to folder where you have your WAMP 3 installed.
Then you can use content of other folders, like: applications etc. to add more functionality
Everything's interfaced, so you can concentrate on coding and not hacking your environment.
Upvotes: 10
Reputation: 69
You can have two different versions of XAMPP.
Upvotes: 6
Reputation: 5395
You can download whatever versions of PHP you need and place them into their own directories, e.g.
c:\php5\
c:\php7\
All you need to do is tell your web server (Apache) which version of PHP to use, which you do by loading the appropriate module. In Apache you can do this by finding the file httpd.conf
and then editing the appropriate line:
LoadModule php7_module c:\php7\libphp7.so
You'll have to figure out the correct paths of course - this is just for illustration.
Save httpd.conf
and restart your server. Note, if you don't restart it, the changes won't take effect.
There's no GUI switch to be able to do this, you need to edit the .conf
file and then restart Apache. Doing this only takes a couple of seconds and you could even comment out a version so "switching" only takes a couple of keystrokes, e.g.
Use PHP 5:
LoadModule php5_module c:\php5\libphp5.so
#LoadModule php7_module c:\php7\libphp7.so
Use PHP 7:
#LoadModule php5_module c:\php5\libphp5.so
LoadModule php7_module c:\php7\libphp7.so
You do not need multiple versions of XAMPP, or to dual boot, or use a different machine, or any of the other "solutions" that have suggested convoluted workarounds. The OP wants to use XAMPP and tell it which version of PHP to use. This is the quickest and most efficient way of doing it, and only requires one XAMPP installation.
Edit 1-Nov-2017: Apparently some people are saying there's no .so
files on Windows. The answer I gave was adapted from how I have things set up on my Mac (which does use .so
files instead of .dll
). The principle of the answer however is still exactly correct. You are using Apache's configuration file, httpd.conf
to specify where the PHP module (.so
or .dll
) is located on your system. So the only difference for Windows would be the file name and/or path location. The answer I've given is also correct for a vanilla installation of Apache/PHP (without XAMPP at all).
Upvotes: 101
Reputation: 57121
I would recommend using Docker, this allows you to split the environment into various components and mix and match the ones you want at any time.
Docker will allow you to run one container with MySQL, another with PHP. As they are separate images you can have two containers, one PHP 5 another PHP 7, you start up which ever one you wish and port 80 can be mapped to both containers.
https://hub.docker.com has a wide range of preconfigured images which you can install and run without much hassle.
I've also added portainer as an image, which allows you to manage the various aspects of your docker setup - from within a docker image (I did start this container on startup to save me having to use the command line). It doesn't do everything for you and sometimes it's easier to configure and launch the images for the first time from the command line, but once setup you can start and stop them through a web interface.
It's also possible to run both containers at the same time and map separate ports to each. So port 80 can be mapped to PHP 5 and 81 to PHP 81 (Or PHP 7 if your watching this in 2017).
There are various tutorials on how to install Docker( https://docs.docker.com/engine/installation/) and loads of other 'how to' type things. Try http://www.masterzendframework.com/docker-development-environment/ for a development environment configuration.
Upvotes: 13
Reputation: 630
Yes you can. I assume you have a xampp already installed. So,
That's how I am working with multiple xampp installed
Upvotes: 11