May 8, 2010 Post Under Uncategorized

How to install PHP5 to run with Apache 2.2 and MySQL 5 on Windows

I just installed from scratch PHP5 (PHP version 5) to run with MySQL and Apache HTTP server. Despite the popularity of this configuration, I spent few hours straggling to set it up. The main reason it was not straight forward is that most of the “how-to” articles I found on the net were not up-to-date and just didn’t work with the latest version of Apache, PHP and MySQL.

Nowadays, most of the low cost hosting packages come with PHP and MySQL. PHP became very popular, with many free scripts available to download, and writing new PHP scripts is a fast and easy way to build dynamic and stable web sites.

Installing PHP on your local machine makes testing your PHP scripts much easier.
Save time on debugging, without the need of uploading your test files to the server for every minor change you’ve done on your test scripts.

  • Install and configure Apache 2.2
    1. Download the latest release of Apache HTTP Server from http://httpd.apache.org/download.cgi
    2. During installation, you will be asked about your domain name, server name and e-mail address. Set the domain name and server name to “localhost”, and enter any e-mail address you want. All these settings and many others can be altered later by editing the configuration file C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf
    3. If you are a Windows XP user, you will find the Apache icon on your taskbar. You can start/stop/restart the service from there, or, alternatively, you can do it through the Control Panel -> Administrative Tools -> Services.
    4. If you don’t want to use the default port (port 80), you should open the configuration file C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf, search for “Listen 80”, if it is commented, uncomment it by removing the “#” and set it to your desired port.
    5. To make these changes take affect, don’t forget to restart the Apache server.
    6. Test it by going to http://localhost:80 through your browser.
    7. Place your web content under C:\Program Files\Apache Software Foundation\Apache2.2\htdocs
  • Install MySQL
    It’s really simple to install MySQL. All you have to do is to download the current release of MySQL for Windows from http://dev.mysql.com/downloads/ , run the executable and follow the simple instructions.
  • Install and configure PHP5 (PHP Version 5.2)
    1. Go to http://snaps.php.net/ and download a zip file with the latest stable version of PHP for Win32 package. At the time I wrote this, the latest version was PHP 5.2.
    2. Extract all the files to c:\php or any folder you choose. Ensure that php5apache2_2.dll is there. Previous versions of PHP5 come only with the old php5apache2.dll, and this one doesn’t work with the latest Apache HTTP server (version 2.2).
    3. Add “C:\php\” and “C:\php\ext\” to the system path variable. You can do it by going to Control Panel -> System. Click on “Environment Variables” which is located on the “Advanced” tab, and edit your “Path” variable. You should append “;C:\php\;C:\php\ext\” to the end of the path variable value.
    4. Copy c:\php\libmysql.dll to C:\WINDOWS\system32.
    5. Rename c:\php\php.ini-dist to php.ini and edit it with text editor as notepad. It is recommended to backup it before making the following changes:
      • Set doc_root = “C:\Program Files\Apache Software Foundation\Apache2.2\htdocs”. the path should be the same one used by DocumentRoot property in Apache httpd.conf
      • Set extension_dir = “C:\php\ext”
      • Uncomment by removing the ‘;’ from extension=php_mysqli.dll – if it doesn’t exist than add it just before extension=php_mysql.dll.
      • Uncomment extension=php_mysql.dll
    6. Edit Apache configuration file httpd.conf, and add the following lines right after the last LoadModule property:

      LoadModule php5_module "c:/php/php5apache2_2.dll"
      AddType application/x-httpd-php .php .php5
      PHPIniDir "c:/php"
    7. In the same file, add to the end of “DirectoryIndex” property “ index.php5 index.php”.
    8. Create a new file called phptest.php with the following script. Replace ‘rootpassword’ with your actual MySQL root password, and place the file in your apache htdocs folder. Test the Apache-PHP-MySQL configuration by going to http://localhost:80/phptest.php

      <?php

      $mysqli = new mysqli(‘localhost’,'root’,'rootpassword’);
      $mysqli->select_db(‘information_schema’);

      $result = $mysqli->query(“SELECT * FROM SCHEMATA”);

      while($row = $result->fetch_assoc()) {
      print $row['SCHEMA_NAME'] . ‘ – ‘ . $row['DEFAULT_CHARACTER_SET_NAME'] . ‘<br/>’;

      }

      $result->close();

      ?>

    9. If you can get this page, it means that you’ve successfully installed everything! However, if this script doesn’t work, try to run a more simple script that doesn’t use MySQL, but provides you with information about your PHP setup:

      <?php
      phpinfo();
      ?>

One Response to “How to install PHP5 to run with Apache 2.2 and MySQL 5 on Windows”

  1. Earl says:

    I’ve searched the web for just this set of instructions for what seems forever. I can get this to work on a Windows Server 2008 and on Linux, but had a terrible time getting it to run on Windows 7 Ultimate. I followed these instructions to the letter (it was easy) and got the perfect results the very first time.

    Thanks! I really mean it! I had to have this for a very important project I am working on. This saved me!
    -Earl

Leave a Reply