Creating a development environment on Windows with Apache, MySQL and PHP is very easy. We have repeated the setup task so many times that documenting the steps might be worthy of virtue to newcomers.

We will setup Apache in a way that will allow us to create virtual hosts for imaginary domains like:
  • foo.earth
  • foo.mars
You could say, why bother creating imaginary domains? First of all you could create these domains on your own development machine for real-world emulation and testing. You might have 100s of development sites and creating them all as sub-folders under http://localhost might not be a good idea.

Then you could say, why bother with different top-level domains? Well, it could be that on your development machine you want both a stable and testing version of the same site. So, for project Foo you could assign foo.earth as the stable version and foo.mars as the testing version of the same site. Yeah, things sometimes do get ugly with development and it always helps having a stable and testing version of the same site ... well err, almost everything in real life too :)

Getting the software

Get Apache, MySQL and PHP from the following locations. We are not providing the exact file URL as new versions will make them obsolute. Get the installer versions of Apache and MySQL and zipped version of PHP. For complete new comers: we only need binaries and not source files. Don't install anything yet, just get the software and tuck them away neatly in a folder. We will maintain the sequence of installation and configuration. The sequence will always be Apache, MySQL and lastly PHP.

Apache

Run the installer and go through the default values to install Apache. Some notable configs are servername which will just be localhost or 127.0.0.1 and make sure you install Apache as service on port 80.

After installation make sure Apache has been started. Fire up your browser and type tye URL http://localhost/ in the address bar. You should get "It Works!" on your screen.

Creating Virtual Hosts

Now let's create a few virtual hosts.
  • First of all designate a folder where all your virtual hosts will live. This is the virtual host root folder. For example, in our My Documents folder, we have created a folder "vhosts".
  • In our virtual host root folder "vhosts", let's create the sites. For example, we will create 3 sites:
    • sprix.earth
    • foo.earth
    • foo.mars
    So, under "vhosts" we would create 3 sub-folders "sprix.earth", "foo.earth" and "foo.mars" and under each of them we would create a "www" folder.

    The final folder structure would look like the image below:



  • Create 3 index.html files for each of the "www" folders above. Put any content you like. Our content looks like below:

    C:/Documents and Settings/sanjib/My Documents/vhosts/sprix.earth/www/index.html
    This is the default virtual host folder. It matches 
    not only the domain srpix.earth but any domain that 
    is not listed in the virtual host entry but is 
    pointed to this server.
    

    C:/Documents and Settings/sanjib/My Documents/vhosts/foo.earth/www/index.html
    This is the foo.earth index file. The site living here is stable and green.
    

    C:/Documents and Settings/sanjib/My Documents/vhosts/foo.mars/www/index.html
    This is the foo.mars index file. The site living here 
    is a testing site and conditions are volatile :)
    
  • Edit conf/httpd.conf. It should be at: C:/Program Files/Apache Software Foundation/Apache2.2/conf/httpd.conf. Enable the following line:
    Include conf/extra/httpd-vhosts.conf
    
  • Edit conf/extra/httpd-vhosts.conf. It should be under: C:/Program Files/Apache Software Foundation/Apache2.2/conf/extra/httpd-vhosts.conf

    Make sure the following line is enabled:
    NameVirtualHost *:80
    
    Before we move on to the virtual host entries, let's create a minimal generic entry for all virtual hosts as below:
    
        DirectoryIndex index.html index.php 
        AllowOverride All
        Order allow,deny
        Allow from all 
    
    
    Now, on to the 3 site specific virtual entries:
    
        DocumentRoot "C:/Documents and Settings/sanjib/My Documents/vhosts/sprix.earth/www"
        ServerName sprix.earth
    
    
    
        DocumentRoot "C:/Documents and Settings/sanjib/My Documents/vhosts/foo.earth/www"
        ServerName foo.earth
    
    
    
        DocumentRoot "C:/Documents and Settings/sanjib/My Documents/vhosts/foo.mars/www"
        ServerName foo.mars
    
    
  • Finally (yes, last step for virtual hosts :), let's edit the Windows hosts file so that all the 3 imaginary hosts point to localhost, that is 127.0.0.1. The location of the hosts file is at: C:/WINDOWS/system32/drivers/etc/hosts.
    127.0.0.1       sprix.earth
    127.0.0.1       foo.earth
    127.0.0.1       foo.mars
    
In case you run into problems, check the Apache error logs at: C:/Program Files/Apache Software Foundation/Apache2.2/logs/error.log

MySQL

Installing MySQL is very straight-forward. Double-click the installer and go with all the default values. Some notable points of interest are mentioned below.

  • Include the bin folder in Windows path. This makes it easier to run mysql related commmands like mysql, mysqladmin, mysqldump from the command line. Also allow the MySQL service to start when Windows starts.
  • This is a development server, so it's best to choose that option in contrast to a production server.
  • We don't provide a root password. Some would argue this as a security breach but for us this is by intention since it's a development machine and no outside (non-localhost) connection is allowed anyway. Besides, many a times the local database configuration file was mistakenly uploaded to remote with the root passowrd for local development machine.
  • Make sure your Windows firewall or Antivirus firewall is not blocking the MySQL port which is 3306
Once MySQL is installed we should test if it's running. Click Start -> All Programs -> Program Files -> MySQL Server -> MySQL Command Line Client. Just hit Enter when prompted for password.

PHP

The final step is to install and configure PHP. We will do that now.
  • Unzip the contents of the downloaded PHP folder and move them under C:/php
  • Edit C:Program FilesApache Software FoundationApache2.2confhttpd.conf and add the following lines:
    LoadModule php5_module "C:/php/php5apache2_2.dll"
    AddType application/x-httpd-php .php
    
    Note the "C:/php/php5apache2_2.dll" in line above. This will vary depending on the Apache server version. We have Apache version 2.2.x so we use that dll file. Inside the C:/php/ folder there are 3 php apache related dll files:
    • php5apache.dll - to be used with Apache 1.x
    • php5apache2.dll - to be used with Apache 2.0.x
    • php5apache2_2.dll - to be used with Apache 2.2.x
  • Copy the file C:/php/php.ini-recommended and rename it to php.ini. Then move php.ini to C:/WINDOWS
  • Edit C:/WINDOWS/php.ini and make sure the following lines are specified:
    extension_dir = "C:\php\ext"
    
    Continue down the file and enable the following line:
    extension=php_mysql.dll
    
    This serves 2 purposes. It species the directory where the PHP extensions are, and in the 2nd line the MySQL extension for PHP is enabled.
  • Create a file under C:/Documents and Settings/sanjib/My Documents/vhosts/sprix.earth/www/ and name it phpinfo.php
  • Edit phpinfo.php and add the following lines:
    
    
  • Finally restart the Apache server
Point your browser to http://sprix.earth/phpinfo.php. You should get the following page filled with PHP variables, environment specifications, loaded extensions, etc.

Do a quick search on the page for mysql and you should get information about MySQL similar to the following:

If you run into problems getting MySQL enabled properly, the first place to look is ensuring that the correct MySQL driver is running. Check if you have MySQL path to bin in environment and if the MySQL provided driver is conflicting with the PHP version of the MySQL driver. Try toggling with the different MySQL drivers to see which one works correctly.

Try enabling various extensions in php.ini file and refresh the phpinfo.php page to verify if they got enabled. Always restart the Apache server after each update of php.ini file. Then refresh the php.info page to see your changes. Enjoy!



Write a Comment


Your Name:
Your Email:
Comment: