Apache, PHP, and MySQL Setup on Windows

By

For building websites, the most popular major technologies consist of the Apache web server, the MySQL database management system, and the PHP web scripting language — a trio sometimes referred to by the acronym "AMP". (Incidentally, the "P" can alternatively mean Perl or Python.) When hosted on a Linux computer, the complete combination is known as "LAMP", a.k.a. a "LAMP stack". On a Windows computer, the quartet of technologies is "WAMP", which is what I am currently using on my own computer for building websites.

Why the popularity of Apache, MySQL, and PHP? Individually, each one is powerful, free, and open source. When combined together, they work well for running websites. Their usage has continually increased, because as more developers adopt this toolset, they create and publish more helpful tools and documentation, in turn making this option even more attractive to people getting started in this field.

Regardless of whether you choose to do your web development on remote servers, or prefer working locally on your own machine (which is arguably better), you will first need to install these components on the target computer. Other articles of mine detail how to install Apache, MySQL, and PHP. This article will show you how to use all of them together for local web development, and assumes that you have installed all three of them successfully. (All of these articles view the installation and configuration processes from the perspective of the Windows operating system, but should also be valuable to anyone setting up a development environment on a Linux or Mac OS machine.)

Apache and PHP

The Apache web server is able to render PHP-generated web pages by loading a PHP module, which is controlled by the Apache configuration file, httpd.conf, located in the configuration directory (which in this article, and the aforesaid Apache installation article, is C:\_a\Apache). It is advisable to group all of your additions at the bottom of the file:

LoadModule php5_module "C:/_a/PHP/php5apache2_4.dll"
PHPIniDir "C:/_a/PHP/"
AddHandler application/x-httpd-php .php

Find the line that sets the value of DirectoryIndex, which specifies which files, and in which order, Apache searches for when given a directory path and no filename. Add index.php before or after the index.html (here the latter):

DirectoryIndex index.html index.php

If you plan on using a content management system (CMS) or any other framework that uses search-engine-friendly (SEF) URLs, then you will need to link in the shared object file required for URL rewriting. Uncomment the line of code that reads:

LoadModule rewrite_module modules/mod_rewrite.so

Similarly, PHP needs to be configured to work with Apache. In the PHP installation directory (in this article, C:\_a\PHP), copy or rename the file php.ini-development to php.ini.

Find the line that sets the value of extension_dir, which is the path (relative or absolute) to the directory containing the PHP dynamic library files. Uncomment it, resulting in:

extension_dir = "ext"

This relative value is adequate if you also add the PHP installation directory to the Windows environment variable Path ("Control Panel" > "System" > "Advanced System Settings" > "Environment Variables" > "System variables" > "Path"). If for whatever reason you choose not to modify the Windows Path variable, then extension_dir will need to be set to the absolute PHP path (in this tutorial C:\_a\PHP\ext).

To enable these changes for loading PHP scripts in a browser window, you must restart the Apache server ("Control Panel" > "Administrative Tools" > "Services"). If Apache fails to start, check the Windows event logs ("Control Panel" > "Administrative Tools" > "Event Viewer") for any errors or warnings reported by the Apache service. It will list any syntax errors detected as it parses its configuration file. If a problem appears to be related to extensions, try to find the culprit by commenting out all the extensions, and then enabling each one individually and restarting Apache after each one.

Dynamic library files can also cause problems. Some libraries require others that are missing from the installation package or commented out. These will require some online sleuthing. Also, if you receive an error message that Apache cannot load php5apache2_4.dll "into server: %1 is not a valid Win32 application", then most likely you are trying to use 32-bit PHP with 64-bit Apache, which won't work. Replace your PHP instance with a 64-bit version, such as one available from Anindya.

To verify that Apache can run a PHP script and render the output, use a test PHP script that you know works, such as:

<?php
echo "<h1>Apache and PHP work!</h1>";

In our example, we save that PHP code in a file (Apache_PHP_test.php), put it in the Apache document root directory (D:\), and view the script output in a web browser (at http://localhost/Apache_PHP_test.php):

Apache and PHP test script output
Figure 1. Apache and PHP test script output

For a more thorough and interesting test, use this code instead:

<?php
phpinfo();

MySQL

If you have installed MySQL, then at some point you may want to log into the server from the command line, in which case it would be helpful to have added to your Windows Path variable the MySQL binaries path (in this article, C:\_a\MySQL Server 5.6\bin).

To access MySQL databases from within PHP scripts, edit the PHP configuration file php.ini, locate the two MySQL extensions, and enable them by uncommenting those lines so they read:

extension=php_mysql.dll
extension=php_mysqli.dll

There are additional extensions you may wish to enable for web development work:

extension=php_curl.dll
extension=php_gd2.dll
extension=php_gettext.dll
extension=php_mbstring.dll
extension=php_pdo_mysql.dll

Restart the Apache server, and refresh the test PHP script used earlier, to confirm that the extension directory is set correctly and that your recent changes to php.ini did not introduce any syntax errors. On the command line, enter the command php -m, which lists all of the loaded modules; confirm that the ones you enabled are on the list.

Unless any new databases have been added to the MySQL server, then the only one present should be mysql, which we will use for verifying that PHP can now communicate with the database server. Use any PHP script that connects to and queries that database, such as the one below (set the password to the one you designated when installing MySQL):

<?php
list( $server_name, $db_name, $user_name, $user_password ) = array( 'localhost', 'mysql', 'root', 'password' );
$connection = mysql_connect( $server_name, $user_name, $user_password ) || die( "mysql_connect( $server_name, $user_name, $user_password ) failed" );
mysql_ping() || die( 'mysql_ping() failed' );
( $resource = mysql_query( "SHOW TABLES FROM $db_name" ) ) || die( "mysql_query( SHOW TABLES FROM $db_name ) failed" );
echo '<h1>Tables</h1><ul>';
while ( $row = mysql_fetch_row( $resource ) ) {
    echo '<li>' . $row[ 0 ] . '</li>';
}
echo '</ul>';

The output from this particular script is a list of all of the tables in the native MySQL database.

MySQL test output
Figure 2. MySQL test output

You now have a fully functional WAMP stack for developing websites and web apps.

Copyright © 2013 Michael J. Ross. All rights reserved.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <address> <area> <blockquote> <br> <cite> <code> <dd> <div> <dl> <dt> <em> <fieldset> <h1> <h2> <h3> <h4> <h5> <h6> <hr> <img> <input> <li> <map> <ol> <p> <pre> <span> <strong> <sup> <u> <ul>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
15 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.