Apache/MySQL/Php on Windows (WAMP)
(last updated: Jul 26, 2009) print

Select font size:

Software Setup

This document assumes that you've done the MySQL installation and setup. The test programs below assume that MySQL test database is accessible for the guest user with empty password. The software packages you'll need are these installation files:
mysql-5.1.36-win32.msi
apache_2.2.11-win32-x86-openssl-0.9.8i.msi
php-5.2.9-2-Win32.zip
For the truly latest versions of the required packages plus other related software, use these sites:

Software Home Download
Php http://www.php.net http://www.php.net/downloads.php
MySQL http://www.mysql.com http://www.mysql.com/downloads/
Apache http://httpd.apache.org http://www.apache.org/dyn/closer.cgi/httpd/binaries/win32/

NetBeans

NetBeans is an optional Php project editor. As of version 6.5, this has become an acceptable choice for working with Php-based projects. NetBeans does, however, require a the Java component including the Java Development Kit (JDK) installation. You need to do the steps from Netbeans/JDK Installation, omitting the Hello World Program section and beyond.

After this, the software package you'll need to install is:
netbeans-6.7.1-ml-php-windows.exe       (from the Computer Science site)
Alternatively, go to the NetBeans site, http://www.netbeans.org.

Apache

The installation file apache_2.2.11-win32-x86-openssl-0.9.8i.msi will install into:
C:\Program Files\Apache Software Foundation\Apache 2.2\
When you install it, there will be a point where it asks you to enter server information:
Network Domain                     put "localdomain" here
Server Name                        put "localhost" here
Administrator's Email Address      put anything, like "a@b.c" here
* For all users on port 80         the default, probably want this
  For current user on port 8080    if you want IIS as your main web server
To control/configure Apache, follow this chain:
Programs Apache HTTP ... Configure ...
Control ...     Edit the Apache httpd.conf ...
The Control choices include Start, Stop and Restart. If everything goes OK the Apache service will start automatically after its installation and restart when you reboot the computer. The Configure menu choice contains the important Edit choice for modifying the key configuration file httpd.conf in the conf subdirectory of the Apache installation. Whenever you make an editing change to this file, you need to restart the server to pick up the changes.

Basic Test of the Apache installation

Open up an internet browser and enter the URL for your local site:
http://localhost         (or, if running or port 8080:)         http://localhost:8080
You should see the Apache default root page. If for some reason this doesn't appear, the Apache Server may need to be started. For Windows, as usual, consider rebooting the computer if it doesn't work.

Create a default URL

Apache on Linux and modern Windows systems can be configured to support the association of the URL
http://localhost/~YOUR_LOGIN
to a specific directory on your system, but for convenience of exposition, it is easier to use a URL name that does not refer to a specific user name. Assuming your are the only user on your system, this is not a bad idea anyway. So we will set up a desired directory to correspond to the neutral URL:
http://localhost/default
Any directory can serve as the Web default directory. Here are two possibilities:
  1. Some directory in your C:\ drive, like:
    C:\public_html
  2. Your NetBeans project directory
    C:\Documents and Settings\YOUR_LOGIN\My Documents\NetBeansProjects
We want to map the URL http://localhost/default to this directory. Edit the httpd.conf configuration file, most likely by choosing the Apache program menu selection:
Edit the Apache httpd.conf Configuration file
Go to the end of the file and add the following lines. The choices here are based on the two possible default directories:
  1. Alias /default "C:\public_html"
    
    <Directory "C:\public_html">
        Options All
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    
  2. In this case be sure to modify YOUR_LOGIN appropriately:
    Alias /default "C:\Documents and Settings\YOUR_LOGIN\My Documents\NetBeansProjects"
    
    <Directory "C:\Documents and Settings\YOUR_LOGIN\My Documents\NetBeansProjects">
        Options All
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    
After these changes restart Apache.

Default URL test

Test the default URL by creating (or downloading) a simple HTML file hello.html in your default directory. It need only have one line:

hello.html
Hello from HTML
Then point your browser to this URL:
http://localhost/default
You should get a directory listing of the files in this default directory, including the one you just created, which you can choose via the URL:
http://localhost/default/hello.html
Afterwards this test you can delete hello.html since it has no further usage.

Php

Php can actually be used in one of three variants: We'll be primarily interested in the first of these choices. The Php distribution for Windows includes the zip package that we'll use as well as an installer file. I prefer the zip package because it is relatively easy to do the installation by hand and then you know exactly what's happening.

You have to decide upon an installation directory. I will assume that this is the installation directory
C:\php
You may need to create this and/or save an previous version, if so desired. If you choose a different installation directory, you'll need to appropriately modify later details in this handout.

Extract php-5.2.9-2-Win32.zip into C:\php. Then (assuming Windows XP or later) You may need to copy other .dll files into C:\windows\system32 based on other desired Php functionality.

The file php.ini is Php's configuration file to which you'll want to make a number of editing changes. Php offers two choices:
php.ini-dist   and   php.ini-recommended.
The former represents the default initialization settings that are built into Php which are better for learning and developing Php, since it gives feedback such as warnings and errors on the web page itself. The latter version establishes more security features; for example, it does not display error information, preferring to log the errors only — this is better for a production environment where you don't want a mistake to reveal too much to the public.

Modifications to php.ini

Edit the Php configuration file C:\php\php.ini and make the following changes.

Modifications to Apache config file

Edit the Apache configuration file httpd.conf by going to the end of it and adding these lines (if necessary, change occurrences of C:/php to reflect your installation):
LoadModule php5_module "C:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/php"
DirectoryIndex index.php

After these additions restart Apache. If the server refuses to start, go back and double check the installation steps.

Test the Php installation

Install the files (by creating or downloading) hello.php, phpinfo.php, and session-test.php into your default directory:

hello.php
<?php echo "Hello from Php"; ?>

phpinfo.php
<?php phpinfo() ?>

session-test.php
<?php session_start(); // the FIRST THING YOU DO! // to see sessions "not work", comment out the "session_start();" line if (!isset($_SESSION['test'])) { echo "First activation: setting session variable"; $_SESSION['test'] = 1; } else echo "SESSIONS ARE WORKING! activation: ", (++$_SESSION['test']); ?>
You should see them listed in http://localhost/default/ or you can activate them through these URLs:
http://localhost/default/hello.php
http://localhost/default/phpinfo.php
http://localhost/default/session-test.php
Regarding the session-test.php script, if you key it in or copy it into a blank file, make sure there are no whitespace characters before "<?php". You will know if sessions are working properly by pressing the browser's refresh button in which case you you should see the "SESSIONS ARE WORKING" validation message.

NetBeans/Php Hello World

If you want to use NetBeans, you'll have to have done the standard Java installation as described in the NetBeans/JDK document. Start up NetBeans and follow these steps to create a "Hello World" project in your default directory.
  1. Select File New Project
  2. In the New Project window, select the PHP category, and choose PHP Application, then Next.
  3. It appears to be a bit tricky to get the order of operations just right, but you want:
  4. In the Run Configuration window, choose
    Run As: Local Web Site       (the default)
    Project URL: http://localhost/default/HelloPhp/
    Click Finish. This will create the default file:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>
      </head>
      <body>
        <?php
        // put your code here
        ?>
      </body>
    </html>
    
  5. In the Projects Window, right-click on the newly-created HelloPhp project and select Set as Main Project.
  6. Replace the "// put your code here", line by
    echo "Hello from NetBeans";
    
  7. Select Run Run Main Project. This should run the script in new browser window (or frame).

Php/MySQL PDO API

Php (version 5 and above) supports multiple ways to access a MySQL DBMS, including the PDO API as well as other older alternative APIs. Edit the Php configuration file C:\php\php.ini.
  1. Search for the extension_dir variable. Change the default setting (probably "./") to this:
    extension_dir = "C:/php/ext/"
    
  2. In the "Windows Extensions" section look for these commented out lines and uncomment them by removing the initial semicolon, getting:
    extension=php_mysql.dll
    extension=php_pdo.dll
    extension=php_pdo_mysql.dll
    
After this, restart Apache.

The PDO/MySQL API

The PDO is one of the more modern APIs for Php. It supports the database abstraction like Java JDBC and its programmatic usage falls very much along the same lines with exception-generating actions contained within a try-catch block.

Download (via the following link) or create the file mysql-pdo-test.phpbelow in default directory. Then test it via the following link in which you should see "SUCCESS!" printed.
http://localhost/default/mysql-pdo-test.php

mysql-pdo-test.php
<h3>The mysql PDO interface</h3> <?php // mysql-pdo-test.php try { $user = "guest"; $pass = ""; echo "-->connect and select database\n"; $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); # activate exception generation $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); echo "<br />-->create table:\n"; $dbh->exec( "CREATE TABLE IF NOT EXISTS test_tab (thing VARCHAR(10))" ); echo "<br />-->insert into table:\n"; $dbh->exec( "INSERT INTO test_tab VALUES ('book'),('pencil')" ); echo "<br />-->select from table:\n"; $res = $dbh->query( "SELECT thing FROM test_tab" ); foreach ($res as $row) { echo $row['thing'], " "; } echo "<br />-->drop table:\n"; $dbh->exec( "DROP TABLE test_tab" ); echo "<br />SUCCESS!"; // disconnect $dbh = null; } catch (PDOException $e) { die( "<h4>Error: " . $e->getMessage() . "</h4>" ); } ?>

Alternative MySQL APIs

The MySQL API

This is the original API used by Php. The disadvantage of this and the subsequent MySQLi API is that they are linked directly to the MySQL connection, and not abstractions. However, they do provide a more direct level of access in that they do not need specify a database in order to connect. In fact, they are at the same level as the MySQL command-line interpreter and thus allow more MySQL-specific command usage.

Download or create the file mysqltest1.php below in default directory. Then test it via the following link in which you should see "SUCCESS!" printed.
http://localhost/default/mysqltest1.php

mysqltest1.php
<h3>Standard mysql interface</h3> <?php // mysqltest1.php echo "-->connect:\n"; $dbh = mysql_connect( "localhost", "guest", "" ) or die(mysql_error()); echo "<br />-->select database:\n"; mysql_query( "USE test", $dbh ) or die(mysql_error()); echo "<br />-->create table:\n"; mysql_query( "CREATE TABLE IF NOT EXISTS test_tab (thing VARCHAR(10))", $dbh ) or die(mysql_error()); echo "<br />-->insert into table:\n"; mysql_query( "INSERT INTO test_tab VALUES ('book'),('pencil')", $dbh ) or die(mysql_error()); echo "<br />-->select from table:\n"; $res = mysql_query( "SELECT thing FROM test_tab", $dbh ) or die(mysql_error()); while( list($thing) = mysql_fetch_row($res) ) echo "$thing "; echo "<br />-->drop table:\n"; mysql_query( "DROP TABLE test_tab", $dbh ) or die(mysql_error()); echo "<br />SUCCESS!"; ?>

The MySQLi API

The second test uses the new mysqli interface which was started in Php version ≥ 4.1.2. This interface exploits functionality only available in MySQL versions ≥ 4.1. The mysqli interface provides extended functionality as well as both a procedural and object-oriented interface to SQL operations.

Edit the Php configuration file C:\php\php.ini Look for the "Windows Extensions" section and look for the line used previously:
extension=php_mysql.dll
Underneath that line add this new line (note the difference):
extension=php_mysqli.dll
and then restart Apache.

Download create the file mysqltest2.php below in default directory. Then test it via this link:
http://localhost/default/mysqltest2.php
You should see "SUCCESS!" printed out at the end.

mysqltest2.php
<h3>The mysqli object-oriented interface</h3> <?php // mysqltest2.php echo "-->connect:\n"; $dbh = new mysqli( "localhost", "guest", "" ); if (mysqli_connect_error()) die("failed: " . mysqli_connect_error()); echo "<br />-->select database:\n"; $dbh->query( "USE test" ) or die($dbh->error); echo "<br />-->create table:\n"; $dbh->query( "CREATE TABLE IF NOT EXISTS test_tab (thing VARCHAR(10))" ) or die($dbh->error); echo "<br />-->insert into table:\n"; $dbh->query( "INSERT INTO test_tab VALUES ('book'),('pencil')" ) or die($dbh->error); echo "<br />-->select from table:\n"; $res = $dbh->query( "SELECT thing FROM test_tab" ) or die($dbh->error); while( list($thing) = $res->fetch_row() ) echo "$thing "; echo "<br />-->drop table:\n"; $dbh->query( "DROP TABLE test_tab" ) or die($dbh->error); echo "<br />SUCCESS!"; ?>

PEAR MDB2 API

The third test uses the PEAR MDB2 class. PEAR is an acronym for the PHP Extension and Application Repository. PEAR development has a life of its own based at the home page:
http://pear.php.net/
The MDB2 class provides an abstract interface to a number of Database Systems, including, of course, MySQL.

The important difference between this API verses the MySQL and MySQLi APIs is that the connection needs to know the data source, which is constituted of the DBMS plus a specific database. In this sense it is less capable than the others, but it gains in the ability to function with multiple database systems with little or no change to the code.
PEAR and MDB2 package installation
You need internet connection to do this. Double-click to run the C:\php\go-pear.bat batch file. Make sure you are using this most recent version of Php, because earlier distributions had a bug in this batch file.

More than likely you'll want to take the defaults on all the questions asked by simply pressing enter. Part of the execution of the script involves downloading and installing the standard packages.

At the end of the script a message is given about double-clicking the file C:\php\PEAR_ENV.reg to install a registry variable needed for pear, so go ahead and do that.

After this installation, restart Apache.

The pear executable offers a command-line interface for determining available packages and installing them. We will use this to install the MDB2 package. Open a command shell and run these commands:
> cd C:\php
> pear install MDB2
> pear install MDB2#MySQL
> pear install MDB2#MySQLi
Download create the file mysqltest3.php below in default directory. Then, as usual, test it via the link below and look for "SUCCESS" to print.

mysqltest3.php
<h3>The MySQL PEAR MDB2 interface</h3> <?php require_once "MDB2.php"; $dsn = "mysql://guest@localhost/test"; /* alternative definition of $dsn: $dsn = array( "phptype" => "mysql", "hostspec" => "localhost", "database" => "test", "username" => "guest", "password" => "", ); */ echo "-->connect and select:\n"; $dbh = MDB2::factory($dsn); if (MDB2::isError($dbh)) die($dbh->getMessage()); echo "<br />-->create table:\n"; $sql = "CREATE TABLE IF NOT EXISTS test_tab (thing VARCHAR(10))"; $res = $dbh->exec($sql); if (MDB2::isError($res)) die($res->getMessage()); echo "<br />-->insert into table:\n"; $sql = "INSERT INTO test_tab VALUES ('book'),('pencil')"; $res = $dbh->exec($sql); if (MDB2::isError($res)) die($res->getMessage()); echo "<br />-->select from table:\n"; $sql = "SELECT thing FROM test_tab"; $res = $dbh->query($sql); if (MDB2::isError($res)) die($res->getMessage()); while( list($thing) = $res->fetchRow() ) echo "$thing "; echo "<br />-->drop table:\n"; $sql = "DROP TABLE test_tab"; $res = $dbh->exec($sql); if (MDB2::isError($res)) die($res->getMessage()); echo "<br />SUCCESS!"; ?>


© Robert M. Kline