Installing Apache, MySQL and PHP on Windows

Posted by Robert Love on June 1, 2007 in Apache, MySQL, PHP

This is it! The definitive guide to installing and configuring Apache 2.2.4, MySQL 5.0.27 and PHP 5.2.1 on Microsoft Windows XP (SP2) with support for both MySQL and MySQLi PHP extensions.

And, yes, if you follow these instructions to the letter, the software will actually work!

Before getting started with the installations, you’ll need to download the required software packages - all of which are conveniently listed in the following table.

Where What What Exactly
Apache 2.2.4 Win32 Binary (MSI Installer) apache_2.2.4-win32-x86-no_ssl.msi
MySQL 5.0.27 Windows Essentials (x86) mysql-essential-5.0.27-win32.msi
PHP 5.2.1 PHP 5.2.1 zip package php-5.2.1-Win32.zip

Installing Apache

  1. Double-click apache_2.2.4-win32-x86-no_ssl.msi

    The Apache HTTP Server 2.2 - Installation Wizard appears with Welcome to the Installation Wizard for Apache HTTP Server 2.2.4

    Welcome to the Installation Wizard for Apache HTTP Server 2.2.4

    Click Next

  2. License Agreement

    Ensure “I accept the terms in the license agreement” is selected

    License Agreement

    Click Next

  3. Read This First

    Read This First

    Click Next

  4. Server Information

    Enter the following details:

    Network Domain: localhost

    Server Domain: localhost

    Administrators Email Address: admin@localhost

    Ensure “for All Users, on Port 80, as a Service — Recommended” is selected

    Server Information

    Click Next

  5. Setup Type

    Ensure “Typical” is selected

    Setup Type

    Click Next

  6. Destination Folder

    Accept the default destination folder (C:\Program Files\Apache Software Foundation\Apache2.2\)

    Destination Folder

    Click Next

  7. Ready to Install the Program

    Ready to Install the Program

    Click Install

  8. Installing Apache HTTP Server 2.2.4

    Installing Apache HTTP Server 2.2.4

    Wait while the Installation Wizard installs Apache

  9. Installation Wizard Completed

    Installation Wizard Completed

    Click Finish

Testing Your Apache Installation

In your web browser, go to http://localhost/. If Apache has been installed successfully, you will see the words “It works!”

It works!

The “It works!” web page - index.html - is located in the root directory of your Apache installation. The root directory can be found here:

C:\Program Files\Apache Software Foundation\Apache2.2\htdocs

Root directory of your Apache installation

Installing MySQL

  1. Double-click mysql-essential-5.0.27-win32.msi

    The MySQL Server 5.0 - Setup Wizard appears with Welcome to the Setup Wizard for MySQL Server 5.0

    Welcome to the Setup Wizard for MySQL Server 5.0

    Click Next

  2. Setup Type

    Ensure “Typical” is selected

    Setup Type

    Click Next

  3. Ready to Install the Program

    Ready to Install the Program

    Click Install

  4. Installing MySQL Server 5.0

    Installing MySQL Server 5.0

    Wait while the Setup Wizard installs MySQL

  5. MySQL.com Sign-Up

    Ensure “Skip Sign-Up” is selected

    MySQL.com Sign-Up

    Click Next

  6. Wizard Completed

    Ensure “Configure the MySQL Server now” is checked

    Wizard Completed

    Click Finish

  7. The MySQL Server Instance Configuration Wizard appears with Welcome to the MySQL Server Instance Configuration Wizard 1.0.8

    Welcome to the MySQL Server Instance Configuration Wizard 1.0.8

    Click Next

  8. Please select a configuration type

    Ensure “Standard Configuration” is selected

    Please select a configuration type

    Click Next

  9. Please set the Windows options

    Ensure “Install as a Windows Service” is checked

    Ensure “MySQL” is selected for Service Name

    Ensure “Launch the MySQL Server automatically” is checked

    Ensure “Include Bin Directory in Windows PATH” is checked

    Please set the Windows options

    Click Next

  10. Please set the security options

    Enter and Confirm (retype) your password

    Please set the security options

    Click Next

  11. Ready to execute …

    Ready to execute

    Click Execute

  12. Processing configuration …

    Processing configuration

    Wait while the Server Instance Configuration Wizard configures MySQL

    Click Finish

Connection Error

In some cases (usually when MySQL is being installed over a previous version), the MySQL Server Instance Configuration Wizard will give a Connection Error that looks something like this:

Connection Error

If this happens to you:

  1. Go to Start > Programs > MySQL > MySQL Server 5.0 > MySQL Server Instance Config Wizard

    The MySQL Server Instance Configuration Wizard appears

    MySQL Server Instance Configuration Wizard

    Click Next

  2. Please choose a maintenance option

    Ensure “Reconfigure Instance” is selected

    Please choose a maintenance option

    Click Next

  3. Please select a configuration type

    Ensure “Standard Configuration” is selected

    Please select a configuration type

    Click Next

  4. Please set the Windows options

    Ensure “Include Bin Directory in Windows PATH” is selected

    Please set the Windows options

    Click Next

  5. Please set the security options

    Ensure “Modify Security Settings” is checked

    Enter and Confirm (retype) your password

    Please set the security options

    Click Next

  6. Ready to execute

    Ready to execute

    Click Execute

  7. Processing configuration …

    Processing configuration ...

    Wait while the MySQL Server Instance Configuration Wizard reconfigures MySQL

    Click Finish

Testing Your MySQL Installation

  1. Go to Start > Programs > MySQL > MySQL Server 5.0 > MySQL Command Line Client

    The MySQL Command Line Client appears with a prompt to Enter Password

  2. Enter the password you specified during the installation

    Press Enter

  3. The MySQL Command Line Client will connect to the MySQL Server and produce the following welcome message and mysql prompt:

    Welcome to the MySQL monitor. Commands end with ; or g.
    Your MySQL connection id is 3 to server version: 5.0.27-community-nt
    
    Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
    
    mysql>

Installing PHP

  1. Create a new folder C:\php
  2. Copy the contents of php-5.2.1-Win32.zip to C:\php
  3. On your desktop, right-click My Computer and select Properties
  4. In the System Properties dialogue box select the Advanced tab

    System Properties dialogue box

  5. Click the Environment Variables button
  6. In the Environment Variables dialogue box under System variables select Path

    Environment Variables dialogue box

  7. Click the Edit button
  8. In the Edit System Variable dialogue box add C:\php to the Variable value field

    Edit System Variable dialogue box

  9. Click the OK button for all open dialogue boxes

Configuring PHP

  1. In C:\php, copy the file php.ini-recommended
  2. Rename the Copy of php.ini-recommended to php.ini
  3. Open php.ini in Notepad
  4. In php.ini, find this line:

    display_errors = Off

    and enable the display of errors by changing the value to “On”:

    display_errors = On

    Note: You wouldn’t do this in a production environment - this is just for testing and development!

  5. In php.ini, find this line:

    extension_dir = "./"

    and specify the directory in which the loadable extensions (modules) reside by changing the value to “ext”:

    extension_dir = "ext"
  6. In php.ini, find these lines:

    ;extension=php_mysql.dll
    ;extension=php_mysqli.dll

    and enable both the MySQL and MySQLi extensions by uncommenting the lines:

    extension=php_mysql.dll
    extension=php_mysqli.dll
  7. In Windows Explorer, copy the following dll files:

    C:\php\libmysql.dll

    C:\php\ext\php_mysql.dll

    C:\php\ext\php_mysqli.dll

  8. Paste the dll files to C:\WINDOWS\system32

    Note: Most installations don’t require you to copy libmysql.dll to system32, but some do. So it’s better to be safe than sorry!

  9. Go to Start > Programs > Apache HTTP Server 2.2.4 > Configure Apache Server > Edit the Apache httpd.conf Configuration File

    The Apache configuration file opens (httpd.conf)

  10. Add the following three lines at the end of the file:

    LoadModule php5_module "C:/php/php5apache2_2.dll"
    AddType application/x-httpd-php .php
    PHPIniDir "C:/php"
  11. In httpd.conf, find this directive:

    <IfModule dir_module>
        DirectoryIndex index.html
    </IfModule>

    Add index.php as a directory index:

    <IfModule dir_module>
        DirectoryIndex index.php index.html
    </IfModule>
  12. Save and close httpd.conf
  13. Go to Start > Programs > Apache HTTP Server 2.2.4 > Control Apache Server > Restart

    Note: You need to restart Apache any time you make changes to the php.ini and/or httpd.conf files

Testing PHP

  1. Open Notepad and type the following: <?php phpinfo(); ?>
  2. Save the file as info.php in your web root directory (C:\Program Files\Apache Software Foundation\Apache2.2\htdocs)
  3. Close info.php
  4. Open info.php in your web browser (http://localhost/info.php)

    If PHP is installed correctly, you will see a table of information pertaining to your PHP installation environment

    phpinfo

  5. Open Notepad and type the following:

    <?php
    $mysqli = new mysqli('localhost', 'root', 'yourpassword', 'test');
    if (mysqli_connect_errno()) {
        printf('Connect failed: %s', mysqli_connect_error());
        exit();
    }
    printf('Host information: %s', $mysqli->host_info);
    $mysqli->close();
    ?>
  6. Save the file as mysqli.php in your web root directory (C:\Program Files\Apache Software Foundation\Apache2.2\htdocs)
  7. Close mysqli.php
  8. Open mysqli.php in your web browser (http://localhost/mysqli.php)

    If MySQL is enabled, you will see the following output:

    “Host information: localhost via TCP/IP”

    PHP MySQLi Test

Finished!

That’s it, you’re done. Hopefully it all came together without any major dramas. If not, you have my deepest sympathies. And if that’s not enough, please leave a comment specifying exactly where it all went pear-shaped for you and I’ll do my best to get to the bottom of it.

17 Comments on Installing Apache, MySQL and PHP on Windows

By Ei Sabai on March 6, 2007 at 10:20 am

I had some problems when installing mysql and php; 1) the mysql wizard was unable to apply security settings, 2) when I opened http://localhost/mysqli.php in my browser, i had this error: “Fatal error: Class ‘mysqli’ not found in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\mysqli.php on line 2″ even though I followed the instructions to the letter. Why? :(

By Robert Love on March 6, 2007 at 10:30 am

Hi Ei Sabai

Your first problem requires you to cancel the MySQL Server Instance Configuration Wizard, restart the Wizard, remove the instance, then reconfigure. Your second problem is a typical anomoly and requires you to also copy C:\php\libmysql.dll to C:\WINDOWS\system32. After doing this, restart Apache and try again and your problems should be solved.

I’ll add both to the instructions and thanks for feedback.

By Mike Feghali on March 6, 2007 at 12:37 pm

Thanks Signified, good notes here. What if I wanted to install the Apache root in another folder such as C:\www. What do I need to do to achieve this? Thanks, please back to me today.

By Robert Love on March 6, 2007 at 2:37 pm

Hi Mike

Very funny :) Sounds like you are playing devil’s advocate here as this is common practice (and exactly how I run my setup). So thanks for asking the question! Here goes:

  1. Create the folder C:\www
  2. Go to Start > Programs > Apache HTTP Server 2.2.4 > Configure Apache Server > Edit the Apache httpd.conf Configuration File
  3. Find this line: DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"
  4. Change it to this: DocumentRoot "C:/www"
  5. Find this line: <Directory "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs">
  6. Change it to this: <Directory "C:/www">
  7. Go to Start > Programs > Apache HTTP Server 2.2.4 > Control Apache Server > Restart

That’s it. Now your root directory is C:\www

Hope that helped.

By SlayerJr on March 6, 2007 at 6:53 pm

Or you could just use a Mac or Xserve which has all this crap pre-installed and ready to go. Robert, are you planning on doing a guide on MS Vista? Because it should be good for a laugh. ;-)

By chris on March 7, 2007 at 12:50 pm

Thanks heaps for the installation tips, all worked well till the end when this appeared at http://localhost/mysqli.php after I created the file mysqli.php and then got this at the address.

Warning: mysqli::mysqli() [function.mysqli-mysqli]: (28000/1045): Access denied for user ‘root’@'localhost’ (using password: YES) in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\mysqli.php on line 2
Connect failed: Access denied for user ‘root’@'localhost’ (using password: YES)

Any hints on how to fix this little problem

Thanks heaps in advance

By Robert Love on March 7, 2007 at 1:45 pm

Warning: mysqli::mysqli() [function.mysqli-mysqli]: (28000/1045): Access denied for user ‘root’@’localhost’ (using password: YES) in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\mysqli.php on line 2
Connect failed: Access denied for user ‘root’@’localhost’ (using password: YES)

I think I have a pretty good idea about what’s causing the error. The error is occurring on this line (line 2):

$mysqli = new mysqli('localhost', 'root', 'yourpassword', 'test');

This means you haven’t replaced 'yourpassword' with your "actual MySQL password."

By wabz on March 26, 2007 at 12:16 am

I have tried doing everything u said but i still get an error message of page not found when i am running the info.php. What could be wrong

By Robert Love on March 26, 2007 at 10:37 am

I have tried doing everything u said but i still get an error message of page not found when i am running the info.php. What could be wrong

What this means is that the file info.php does not exist. You might think it does, but Apache disagrees! The most probable reason for this is that when you saved the file, you actually saved it as info.php.txt instead of info.php. This happens on systems that are not displaying file extensions. To get around this problem, from the “Save As” dialogue box in Notepad, select “All Files” from the “Save as type” dropdown. This will ensure the .txt file extension is not appended to your file name.

By big_fellaaa on June 17, 2007 at 4:34 am

Thanks for all the detailed install tips - still got a major problem after running to the end of your instructions… I’ve even gone back and repeated the process. I still get the error:

“Fatal error: Class ‘mysqli’ not found in C:\www\htdocs\mysqli.php on line 2″

By the way, I’m running Vista Ultimate. Is there anything else that could work?

By ramrayavarapu on June 23, 2007 at 3:16 am

I have tried as you have directed, and hope i completed all the steps. When i was trying to test My SQli could not get any response as you have mentioned. Please suggest and provide some more steps that would help me out with this.Thanks in advance.

Ram

By Nathan on July 5, 2007 at 7:11 am

Hi Robert.

First off, thanks for the great article!!! It really helped me get all this complicated programs installed.

I only have one problem with the installations. When I go to “http://localhost/mysqli.php”, I get the error “Fatal error: Class ‘mysqli’ not found in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\mysqli.php on line 2.”

I made sure to copy “C:\php\libmysql.dll” to “C:\WINDOWS\system32″, and I restarted Apache, but I still get the same error?

I’d appreciate any help with this problem.

Thanks,
Nathan

By Nathan on July 5, 2007 at 7:29 am

I wish I could edit my above post, apparently I overlooked something and posted 10 minutes too soon :(

I named a file wrong. It’s still not working, but I don’t have the problem posted above any more. Now when I go to http://localhost/mysqli.php I just get a blank page, no errors or anything, just blank?

Thanks for the help,
Nathan

By arafatb on July 29, 2007 at 4:28 pm

yea im hav’n the same problem as nathan.

Thanks for your help

Arafat M Baksh

By arafatb on July 30, 2007 at 2:49 am

k my problem is fixed, dunno how, but its working !!

By How to Become a Web Developer and Make Lots of Money - Signified on August 13, 2007 at 5:20 pm

[…] Now you have the know-how, it’s time to put it all into practice. To start, you’ll need a local development environment. I’m an open-source advocate (except for my laptop which is provided to me by work - so don’t give me shit about using Windows!). So, to set up a local Windows/Apache/MySQL/PHP (WAMP) development environment, check out Installing Apache, MySQL and PHP on Windows. […]

By bad_andy on August 23, 2007 at 9:37 pm

Thank you Robert,

I had spent considerable time with what looked like good directions, your page worked for me first time after re-installing the applications.

I’m definitely bookmarking this site.

Andrew

Write a Comment on Installing Apache, MySQL and PHP on Windows

Comments on Installing Apache, MySQL and PHP on Windows are now closed.

More

Read more posts by Robert Love

About the Author

I'm a web technologist, University lecturer and trainer based in Sydney, Australia. I'm particularly interested in standards-based web design and development using web standards such as XHTML, CSS and the DOM, and open-source software such as Linux, Apache, MySQL and PHP. When I'm not making websites, I like to tell other people how I think they should make websites. I have developed, and teach, a few training courses in web technologies offered through Learn4Life at The University of New South Wales, Sydney, Australia. I'm a member of the W3C HTML Working Group, the Web Industry Professionals Association and the Web Standards Group and can be found espousing my opinions on forums and in mailing lists. My wife and son enjoy the benefits of my full-time employment as Senior Developer with News Digital Media, and they wouldn't mind at all if you wanted to contact me with yet a further opportunity for me to get no sleep.

Lightbox JS WordPress Plugin Running Multiple Websites on Apache