Windows 7, Apache, Mysql and PHP

7AMP – Creating a Development Environment

Running a local development web server is one of the best ways of learning AJAX; reading up on it is one thing, but being able to pass the raw data back and forth between a browser and a server is really the only way to truly understand what is happening at a fundamental level. To create the dynamic and interactive apps and sites that we’ve come to know and love, you need a development server.

On Windows systems we really have only a few decent options available; we can use Microsoft’s Internet Information Services (IIS), which is usually bundled with Ultimate or Business versions of Windows, or we can use Apache, the extremely popular open-source alternative. Remember when Microsoft enjoyed a 90% market share of the browser market? Apache is the MS of the web server world and at some points in its illustrious history has enjoyed almost total domination in its respective field.

IIS is generally quite easy to configure as it uses a graphical interface and is fairly intuitive, however IIS is geared towards development with the .net framework; .net is a proprietary language and generally you need something like Visual Studio to succeed in building web applications with it. Visual Studio does not come cheap (although free express versions are available and if you’re really hardcore you could use notepad to write the code) and many people prefer the open-source alternative PHP.

Similarly, MSSql is a perfectly adequate database solution made by Microsoft, but like its other offerings, is also a proprietary technology. Mysql is free, open-source, and very, very popular. It’s easy to use, robust and scalable and that’s why many developers prefer it. To create development environment we really want to spend as little as possible so really our choices here are clear – Apache as the platform, PHP as the server-side language, and Mysql as the storage technology. But getting all these technologies talking to each other is not quite as straight-forward as running a few installers.

Getting Started

First of all, we need to download the installers for Apache and Mysql and the files required to run PHP. The installers can be found at the following locations:

  • http://httpd.apache.org/download.cgi
  • http://dev.mysql.com/downloads/mysql/5.1.html#downloads

On the above pages choose the appropriate MSI packages for your platform (e.g. x64 or x32) and requirements (you may as well choose the full SSL version of Apache). With PHP however, we don’t want the installer, we want the zip file that contains all of the PHP files as there is more in this package than you get with the standard installer. It can be found at the following URL:

  • http://uk2.php.net/get/php-5.2.11-Win32.zip/from/a/mirror

There are two different zip files for Windows on the PHP site, make sure you do not get the one with NTS (non thread-safe) in the name as this will not work with Apache (which is thread-safe). Before running the installers or unpacking the zip file we just need to do a couple of minor system tasks; we should stop any instant messenger applications temporarily as they can interfere with the Apache installation, and we should disable Windows User Account Control (UAC) as it interferes with the Mysql configuration utility. To disable UAC visit the User Accounts application in the Control Panel:

User Accounts

In the applet set the slider to the bottom setting:

UAC

Click the OK button and confirm the very last UAC notification you should ever receive (w00t!), then restart your machine as directed.

Installing Apache

The first thing we need to install is the Apache web server which serves web pages to browsers following HTTP requests, and forms the foundation of our development environment. Run the installer, click the next button to get started and accept the license terms. Click next again and you should then see the following screen:

Apache Installer 1

Complete the dialog as shown above and click next again; on the following screen choose the Typical option:

Apache Installer 2

We can now just keep clicking next until the installation occurs. Once finished you should see the Apache icon in the notification area; it should have a green play symbol to indicate that it is running:

Apache Icon

As a consequence of Apache running successfully, we should be able to open a browser, type http://localhost in the address bar and see the following message:

It works!

Configuring Apache

The web page we’re seeing is being served from Apache’s default content-serving directory which is probably located somewhere like this:

C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs

That’s fine, but it will be a bit of a chore having to dig that deep when we want to add or remove files. We can easily configure Apache to server content from a folder that is closer to hand; create a new directory on your C drive and call it apachesite.

In the Start menu group for Apache there is an option to Edit the Apache httpd.conf Configuration File, choose this and a text file will be opened. This is Apache’s main configuration file; unlike IIS, Apache does not have a GUI for configuration, instead we must edit this text file to make changes to the server. Scroll down to the Main Server Configuration section, which begins on line 144. On line 177 there should be the DocumentRoot directive, which will be pointing at the directory mentioned above. Change this line so that it points to the directory we created on the C drive:

DocumentRoot "C:/apachesite"

Just below this directive are several Directory directives; you’ll need to set the second one so that it points to the same path as the DocumentRoot:

<Directory "C:/apachesite">

Save the file and restart Apache which you can do by left-clicking the icon in the notification area and choosing Apache2.2 → Restart. To veryify that it works create a new HTML file called index.html in the new directory and request localhost from the browser again:

It still works!

Installing PHP

Next we can install PHP so that Apache can run PHP files when necessary; create another new directory on the C drive and call it php, then open the PHP zip that we downloaded and drag the entire contents into the php folder. That’s all we need to do as far as ‘installation’ is concerned; all we need to do now is configure Apache to use it.

Configuring Apache to use PHP

Edit the httpd.conf file again; after all of the AddModule directives near start of the file add the following new code:

####### PHP Config ###########
LoadModule php5_module "C:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/php"
##############################

Save the file, but don’t worry about restarting Apache yet as we need to make a couple more changes and restart the computer anyway.

Configuring PHP

Like Apache, PHP relies on file-based configuration; in the C:\php folder rename the file called php.ini-recommended to php.ini. Now we need to add a Class Variable to Windows so that it knows where the PHP files reside. You’ll need to go back to the Control Panel and open the System applet. On the Advanced tab, near the bottom of the dialog is a button called Environment Variables – click this button and a new dialog will open:

Environment Variables

The new dialog is divided into 2 sections; in the bottom section select the line that has Path as the Variable name (you’ll need to scroll down a bit) and then click the Edit button below the second section to open the editor:

Edit

Go to the end of the Variable value line and add the following text to the exsting value:

;C:\php\;

This will map to the php folder we created on the C drive and which we unpacked the PHP files from the zip file into. It is very important that you don’t remove any of the existing text in the value (or other programs on your machine, or your entire machine, may stop working) and that you enter the new text exactly as it appears above. Once this is done click OK on the three dialog boxes and restart your computer.

Once your computer has restarted, the Apache icon should still have the green play symbol on it and PHP should be configured successfully. To test it create a page in your text editor and add the following code to it:

<?php phpinfo() ?>

Save the new file as phpinfo.php in the C:\apachesite folder and then request the page by typing the following address in the browser’s address bar:

http://localhost/phpinfo.php

Your browser should display the PHP information page:

PHP Info

Success! Now we just need to install Mysql and everything is ready.

Installing Mysql

Run the Mysql installer that we downloaded and keep clicking Next until you get to the configuration wizard:

Mysql Installer 1

Uncheck the Register box and then click the Finish button. Click next again and then on the following screen choose the default Detailed Configuration option:

Mysql Installer 2

On the next screen choose the Developer Machine option:

Mysql Installer 3

After clicking Next on the above screen choose the default option again on the following screen:

Mysql Installer 4

Go with the defaults that are selected on the next screen too:

Mysql Installer 5

And again, just go with the default option on the next page:

Mysql Installer 6

The next screen has both options checked, just keep them checked and move along:

Mysql Installer 7

Don’t worry about checking the Firewall Exception box, whether this is required will vary depending on your system and firewall so you can do this in a minute manually if need be. Provided you just want the standard Latin character set you can again just choose the default and click next:

Mysql Installer 8

On the next screen keep the defaults, but also check the box to add the executions path to the Windows Path variable (we did this manually when configuring PHP):

Mysql Installer 9

Enter a new password for the Root account and then click Next again:

Mysql Installer 9

On the final screen click the Execute button and the configuration changes will be applied:

Mysql Installer 10

Once the wizard has completed you should see confirmation:

Mysql Installer 11

At this point you should restart your computer again. You aren’t prompted to but Windows is fickle and the installation may not run correctly if you don’t do it. So ensure you do.

Testing Mysql

Ok, so you’re back after doing the reboot right? Good. Let’s just check Mysql is running correctly. In the start menu there should be a Mysql Command Line Client application, choose this and enter the password you set when running the Mysql configuration wizard. You should see the following screen:

Mysql CLI

Enter the following command at the prompt:

show databases;

The databases in use should be shown; a test database is installed by default:

Databases

Type the command

use test;

The test database will be selected:

Select Database

Let’s create a basic table; type the following command:

create table users(name varchar(20), age int);

This will create a new table called users and add two columns to it, one to hold name data consisting of up to 20 variable characters (alphanumeric) and the second to hold age data as an integer. Hit enter and you should get the Query OK message to confirm the table was created:

Create Table

To populate the table with some dummy data use the following command:

insert into users values('Dan', 31);

You should get the success message again after you hit enter:

Populate Table

As a final test we can check that the data has been inserted into the table corectly using the select command:

select * from users;

Which should show the table and the data we inserted:

Table

Configuring PHP to talk to Mysql

All we need to do now is configure PHP to talk to Mysql; earlier on we renamed a file to php.ini in the C:\php folder, open this file now in a text editor. First of all, scroll down to the Paths and Directories section and find the extension_dir directive on line 536; change it so that it appears as follows:

extension_dir = "./ext"

Then scroll down to the Dynamic Extensions section which begins on line 628. In the Windows extensions section remove the semi-colon from in front of the following lines:

  • extension=php_mysql.dll
  • extension=php_mysqli.dll

That’s all we need to do; save the file and once again restart your machine. After restarting you can check for Mysql support in the phpinfo.php page again:

PHP Mysql success

This is pretty much a guarantee of success, but really we should create one more PHP file so that we can test that we can read the data from our database; in a text editor create the following file:

<?php

  $user = 'root';
  $password = your_password_here;
  $database = 'test';
  $server = 'localhost';
  
  $connect = mysql_connect($server, $user, $password);
  $select = mysql_select_db($database, $connect);
  
  $query = mysql_query('select * from users');
  $data = mysql_fetch_assoc($query);
  
  echo 'Hi ' .$data['name']. ' you are ' .$data['age'];
  
  mysql_close($connect);
  
?>

Save this as phpmysql.php in the C:\apachesite and request it using your browser; you should see the following message:

Complete

If this doesn’t work, try putting your firewall into training mode and seeing if you get a notification asking whether to allow the application when you run the page.

Summary

We have now truly succeeded and have the perfect development environment for creating dynamic AJAX-powered pages. Sure, there may be various programs that we can run which will do some or all of the configuration for us, but which may or may not work on the latest version of Windows, but where is the fun in that?! Getting Apache, Mysql and PHP configured manually is an achievement and it gives us the opportunity to learn more about the platforms we’re using when creating modern web applications.


Tags: apache
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • teebee

    I like Server2Go (http://www.server2go-web.de/) because it’s a portable app and you can get it in different flavors (read versions) of Apache, MySQL and PHP.

  • Megan

    Uniform Server is just so darn easy to use and already has php, mysql, etc
    http://www.uniformserver.com/

  • Eric Culus

    Wamp, Xamp or even better Zend Server CE is will make setting up your proper dev environment more easily.

  • http://twitter.com/cbgreenwood cbgreenwood

    good tips for newbies, now how about setting up cvs or svn with that? ;)

  • Juan Felipe Alvarez Saldarriaga

    I use Zend Server Community Edition (http://www.zend.com/en/products/server-ce/), works on windows, linux and mac, lots of features, you can use iis on windows if you want, you can use deb packages or rpm packages and it comes with bytecode caching! it’s great :)

  • http://www.erkasoft.com erkasoft web tasarim

    I ‘m using LAMP. Also IIS7 for .net apps. Php, Apache, Mysql are very popular and simple. I prefery symfony mvc framework. ‘Simple’ is important for maintenance.

    Thanks for this article Dan.

  • chris

    wamp > this post

  • http://newcochin.in dev

    wamp, easyphp and xampp! way more easier than doing this…

  • Jim D

    I know this is an older article but I had to comment. If you are are running a windows server that already has IIS there really is no reason to install and run Apache on windows. PHP runs just fine on IIS. Its a myth that it needs to run on Apache. Ganted if you are running a linux box then yes use LAMP. I can only think someone would run Apache on windows because they are more familiar with Apache or they have this fear that IIS is insecure (which is also a fallacy these days). For a windows server whats the point in installing Apache? None.

    In fact with IIS7 MS even fully supports PHP environment with an easy install
    “Windows Server 2008 with IIS 7.0 provides an open platform for hosting PHP applications from a single server”

    More info.
    http://www.microsoft.com/web/platform/phponwindows.aspx

    I had to comment because of all the people that mentioned they use WAMP and the fact that the author suggests that you should run PHP on Apache if you are using a windows server. There is no reason to do that. When I first read this article I thought it was in regards to setting up Apache on Linux. When I noticed it was in regards to windows I thought to myself what?

  • http://www.ultravioletdesign.co.uk Gareth Foote

    Hi there,
    Very helpful tutorial. This has already proved much better than xampp for windows and other windows/apache solutions.
    I am however having an issue with .htaccess. When I put it into my root file of a virtual bhost directory it gives me an error.
    Any thoughts or suggestions would be gratefully received.
    Gareth

  • Deepa

    Hi ,

    I have been using CodeIgniter with MAMP on my mac machine. Now the machine is set up as a server , and I want to use the codeigniter with the already installed apache server and php framework on the mac. Also , the mysql is installed on other server and not locally . Can you help me with the steps to configure CodeIgniter so that I could use it without MAMP.

    Thanks a lot .

  • http://richard-orton.co.uk Richard Orton

    Hi,

    Just wanted to stop by and thank you for a great article that has helped me to full get to grips with setting up my own development server.

    One thing I wanted to note was.. For anyone running the “phpmysql.php” test file and receiving an error on line 4, it is because there’s a syntax error in the code.

    $password = your_password_here;

    should be…

    $password = ‘your_password_here’;

    Notice the inclusion of the apostrophes around your_password_here.

    Hope that makes sense! Once again – Thanks for the article Dan.

    Richard

  • chuck

    If you’re capable of web programming, I wouldn’t think that it would be difficult to setup a virtualized LAMP (Linux/Apache/MySQL/PHP) server. All you would really need is Ubuntu Server and VirtualBox, both free downgrades. If you want life to be even easier, get yourself Expandrive so you can mount FTP/SFTP from your vbox.

    The best thing about virtualization with VirtualBox is that if somewhere along the lines, you blow up your LAMP, it is fully sandbox’d and you can just delete everything to start from scratch. You won’t mess up your Windows configuration at all.

    http://www.virtualbox.org
    http://www.ubuntu.org
    http://www.expandrive.com (not free)

  • TheRaven

    I personally utilize the services of Uniform Server and Zend Server CE both and find this tutorial to be enlightening none the less. Knowing how to properly configure an environment including Apache, MySQL and PHP manually helps a maintainer better understand what is going on with their tools and systems on a basic level; additionally, having basic knowledge may even allow a server environment user to perform more stable upgrades to isolated components without disrupting overall operations, stability and security.

    Everyone has their favorite stack, I’m sure, but this knowledge and the quality of this instruction is fundamental and should be required by the individual regardless of their preference. We need only stay focused on the facts and apply what we learn to what we have and let knowledge work for us instead of falling back on automation and calling it a day after that. Automation is good, but we should still make it a point to learn the what, where and why of the systems we use; ignorance to these points generally leave someone stranded on a bulletin board somewhere asking for help when the answer might have been in front of them the whole time.

    Not to mention, the more you know about the systems you employ to construct a stack the more knowledgeable you become thus becoming an asset opening up gratifying opportunities in your future professionally and personally. Take it easy and remember that automation is good to get you up and running so that you can have more free time to spend on learning about what is going on in your systems building yourself a foundation on independence and competency.

    Take care; good tutorial too! One of the best I’ve seen around over the past decade – no bull.

  • Bodeddie

    Great step by step. I followed the instructions and it just worked! I’ve been wanting to try my hand at learning php programming and needed a way to test my code at home on a regular (non-server) machine. Thanks!