Dionysopoulos.me https://www.dionysopoulos.me/233-apache-mysql-php-server-on-windows-with-multiple-simultaneous-php-versions.html
I say what I mean and I mean what I say


Apache, MySQL, PHP server on Windows with multiple, simultaneous PHP versions

Written by Nicholas K. Dionysopoulos  Published: 24 August 2014   how-to    windows    server

                                       Glavni izbornik (Home)

 HTML 5, CSS 3, JS, SSL.                                                                      Help  
 1111111111 2222222222 3333333333 4444444444   4444444444      Home refresh
 4444444444 4444444444 4444444444                                                  Home https

 ...

C:\Apache24\htdocs\index.php says: JS says: location.protocol=https:


Apache-MySQL-PHP web server without using a pre-packaged server like XAMPP, WAMPServer.
To have the same sites run under different versions of PHP by just visiting a different URL on my browser.

We'll need to do four steps.

  1. Set up the PHP versions we want to use,
  2. set up MySQL,
  3. set up Apache (and tell it how to run PHP),
  4. create as many virtual hosts per site as PHP versions we want to test with.
It's not very hard to do, but it's not for Joe and Jane Average - you download XAMPP.

Disclaimer: the following configuration is only meant to be used as a local development server.

Setting up PHP

Download the latest PHP 5.3, 5.4 and 5.5 versions from http://windows.php.net/download/
Choose Non Thread Safe releases.

Create a new empty folder C:\PHP

Extract each version of PHP in a directory under C:\PHP, i.e. C:\PHP\5.6 for the latest PHP 5.6 version.

Inside each PHP version's directory copy php.ini-development to php.ini and edit to match your preferences. There is one important change which is mandatory. Find the line

; extension_dir = "ext"

and change it to

extension_dir = "C:\PHP\7.1.9\ext"

remembering to use the correct path per version number. Otherwise PHP won't start.

Press WIN-BREAK on your keyboard. Click on Advanced System Settings. Click on the Environment Variables button. In the top section ("User variables for MyUserName") double-click on the Path variable andto add PHP 7.1.9 to your Windows path append:

;C:\PHP\7.1.9

Setting up MySQL

Download the installer from MySQL community edition and install it following the instructions. Remember to enable TCP/IP networking when asked.

Press WIN-BREAK on your keyboard. Click on Advanced System Settings. Click on the Environment Variables button. In the top section ("User variables for MyUserName") double-click on the Path variable and append:

;C:\Program Files\MySQL\MySQL Server 5.6\bin

to add MySQL command lin programmes to your Windows path.

Setting up Apache

Download from Apache Lounge. Make sure you are downloading the VC15 binares. Download both the Apache package and the respective modules package. You also need to install the Microsoft VC15 redistributable package if it's not already installed on your PC.

Extract the Apache ZIP package. Copy the Apache24 directory to C:\ so that you now have a directory C:\Apache24

Enable PHP

Extract the modules ZIP package. Go into the mod_fcgid-x.y.z (x, y and z are numbers) folder. Go into the mod_fcgid folder. Copy the file mod_fcgid.so into the C:\Apache24\modules folder.

Edit the file C:\Apache24\conf\httpd.conf and change the following lines, removing the # in front of them:

# LoadModule include_module modules/mod_include.so
# LoadModule fcgid_module modules/mod_fcgid.so
# LoadModule vhost_alias_module modules/mod_vhost_alias.so

Edit the file C:\Apache24\conf\extra\httpd-default.conf and add the following lines at the end:

FcgidInitialEnv PATH "c:/php/7.1.9;C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem;"
FcgidInitialEnv SystemRoot "C:/Windows"
FcgidInitialEnv SystemDrive "C:"
FcgidInitialEnv TEMP "C:/WINDOWS/Temp"
FcgidInitialEnv TMP "C:/WINDOWS/Temp"
FcgidInitialEnv windir "C:/WINDOWS"
FcgidIOTimeout 64
FcgidConnectTimeout 16
FcgidMaxRequestsPerProcess 1000
FcgidMaxProcesses 50
FcgidMaxRequestLen 8131072
# Location of php.ini
FcgidInitialEnv PHPRC "c:/php/7.1.9"
FcgidInitialEnv PHP_FCGI_MAX_REQUESTS 1000
<Files ~ "\.php$">
AddHandler fcgid-script .php
FcgidWrapper "c:/php/7.1.9/php-cgi.exe" .php
Options +ExecCGI
order allow,deny
allow from all
deny from none
</Files>

Edit the fle C:\Apache24\conf\httpd.conf and change the line - remove the leading hash sign :

# Include conf/extra/httpd-default.conf

Test PHP

Create a new file C:\Apache24\htdocs\phpinfo.php with the content

<?php phpinfo(); ?>

Open a command prompt (WIN-R, type cmd and press Enter) then

C:
cd C:\Apache24\Bin
httpd.exe

Set the 'ServerName' directive globally to suppress message
AH00558: httpd.exe: Could not reliably determine the server's fully qualified domain name, using fe80::e49b:1aef:6e04:89f6.

On your browser visit http://localhost/phpinfo.php You should see the PHP information page. If not, review all the instructions above, you missed a step.

Back to the command prompt, press CTRL-C to stop Apache. Close this command prompt.

Install Apache as a system service

Press the Windows key, type cmd, right clck on Command Prompt and choose Run as Administrator. This opens an "elevated" command prompt. Type this:

C:
cd C:\Apache24\Bin
httpd.exe -k install

Bonus: autostart Apache Monitor on boot

Open Windows Explorer windows

  1. C:\Apache24\bin
  2. C:\Users\ss\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup where ss is your username on Windows

Holding down the ALT key drag ApacheMonitor from the first window into the second.

This will auto-start the Apache Monitor tray icon on boot which monitors the status of the Apache server and lets you restart it easily.

The Virtual Hosts

Our goal is to have every site on our local server run under a different PHP version depending on the domain name we are using. The domain names we will be using are:

  • local.web for PHP 7.1.9
  • local72.web for PHP 7.2

You need to change your hosts file to have Windows map these fake domains to your local server. Add these lines to the hosts file:

127.0.0.1 local.web www.local.web pro.local.web
127.0.0.1 local72.web www.local72.web pro.local72.web

We will also be implementing dynamic virtual hosts. This means that when you have a directory C:\Apache24\htdocs\foobar you will be able to access this site both as
http://local.web/pro
and http://pro.local.web 
The latter is a better way to develop sites locally since most web scripts store image paths relative to the domain root.

Now the site in the directory C:\Apache24\htdocs\pro will be accessible as http://pro.local.web (PHP 7.1.9), http://pro.local72.web (PHP 7.2) . Cool! Let's make the changes in our server's configuration to make that happen.

Configure Apache for multiple PHP versions

Edit the C:\Apache24\conf\httpd.conf. Find the line and remove the leading hash :

# Include conf/extra/httpd-vhosts.conf

Now edit C:\Apache24\conf\extra\httpd-vhosts.conf file and change its contents with:

# Default PHP virtual host (local.web)
<VirtualHost *:80>
#ServerAdmin webmaster@local.web
ServerAdmin slavkoss22@gmail.com DocumentRoot "c:/Apache24/htdocs" ServerName www.local.web #ErrorLog "logs/dummy-host.example.com-error.log" #CustomLog "logs/dummy-host.example.com-access.log" common </VirtualHost> # Dynamic virtual hosts using vhost_alias, default PHP <VirtualHost *:80> ServerAlias *.local.web UseCanonicalName Off VirtualDocumentRoot "c:/Apache24/htdocs/%1" </VirtualHost> # PHP 7.2 virtual host (local72.web) <VirtualHost *:80> ServerAdmin slavkoss22@gmail.com
DocumentRoot "c:/Apache24/htdocs" DocumentRoot "c:/Apache24/htdocs" ServerName www.local55.web #ErrorLog "logs/dummy-host.example.com-error.log" #CustomLog "logs/dummy-host.example.com-access.log" common <Directory "c:/Apache24/htdocs"> <Files ~ "\.php$"> AddHandler fcgid-script .php FcgidWrapper "c:/php/5.5/php-cgi.exe" .php Options +ExecCGI order allow,deny allow from all deny from none </Files> </Directory> </VirtualHost> # Dynamic virtual hosts using vhost_alias, PHP 5.5 <VirtualHost *:80> ServerAlias *.local55.web UseCanonicalName Off VirtualDocumentRoot "c:/Apache24/htdocs/%1" <Directory "c:/Apache24/htdocs"> <Files ~ "\.php$"> AddHandler fcgid-script .php FcgidWrapper "c:/php/5.5/php-cgi.exe" .php Options +ExecCGI order allow,deny allow from all deny from none </Files> </Directory> </VirtualHost> # PHP 5.3 virtual host (local53.web) <VirtualHost *:80> ServerAdmin webmaster@local.web
DocumentRoot "c:/Apache24/htdocs"
ServerName www.local53.web
#ErrorLog "logs/dummy-host.example.com-error.log"
#CustomLog "logs/dummy-host.example.com-access.log" common

<Directory "c:/Apache24/htdocs">
<Files ~ "\.php$">
AddHandler fcgid-script .php
FcgidWrapper "c:/php/5.3/php-cgi.exe" .php
Options +ExecCGI
order allow,deny
allow from all
deny from none
</Files>
</Directory>
</VirtualHost>

# Dynamic virtual hosts using vhost_alias, PHP 5.3
<VirtualHost *:80>
ServerAlias *.local53.web
UseCanonicalName Off
VirtualDocumentRoot "c:/Apache24/htdocs/%1"

<Directory "c:/Apache24/htdocs">
<Files ~ "\.php$">
AddHandler fcgid-script .php
FcgidWrapper "c:/php/5.3/php-cgi.exe" .php
Options +ExecCGI
order allow,deny
allow from all
deny from none
</Files>
</Directory>
</VirtualHost>

Test the solution

We can now use three different domains to access the same content as different PHP versions. Let's try it:

  • http://www.local.web/phpinfo.php reports PHP 7.1.9
  • http://www.local72.web/phpinfo.php reports PHP 7.2
  • http://sspc1/phpinfo.php

All three URLs load the file C:\Apache24\htdocs\phpinfo.php.

An exercise to the reader

You could even possibly have different sites use different PHP versions without using a special URL, just by adding the relevant  section to their .htaccess file.

Hint: The <Directory> section in a virtual host configuration has the same effect as putting its contents inside a .htaccess file in that directory.

  • Prev
  • Next

Comments (5)

RSS
  • avatar
    Pavel Sunday, 06 August 2017
    Hello. BIG thank you for great article. I tried it on Windows10 with XAMPP and PHP5.6 and PHP7.1 and work! :) There is no need to install mod_include and mod_vhost_alias, just enable second one.

    Just one question, in httpd-default.conf you have set PHPRC as path to folder containing php.ini.
    If I run PHP5 script, it loads php.ini from folder php56 (this one is in PHPRC). I understand that, but if I run PHP7 script, it loads php.ini from folder php71... How is that possible? I never put path to that folder.
    0
    • avatar
      Nicholas K. Dionysopoulos Monday, 07 August 2017
      Both modules are pre-installed in the Apache setup delivered with XAMPP. mod_include is enabled, mod_vhost_alias is not. That's why you only had to enable the latter :)

      Regarding the PHPRC, each version of PHP has its own block in your httpd-vhosts.conf file. Over there you set up the PHPRC per PHP version. Failing to do that, PHP will fall back to the PHPRC environment variable (see the PHP documentation) and if that also fails it will try using the same path as the PHP executable. I suppose what happens in your case is the latter.
Displayed next to your comments.

Not displayed publicly. Gravatar enabled

Type the code that you see in the image
default Security Image
Comments will undergo moderation before they get published.

 
  • You are here:  
  • Home
  • Apache, MySQL, PHP server on Windows with multiple, simultaneous PHP versions

Main Menu

  • The Blog
  • About Me
  • Contact me

Popular tags

joomla opinion how-to security book review developers php hosting software
  • Follow via Twitter
  • Follow via Facebook
  • Follow via Github
  • Follow via LinkedIn
  • Follow via Google+
  • Follow via Youtube
  • Keybase.io

Back to Top

© 2017 Dionysopoulos.me