Creating a VAMP (Vista, Apache, MySQL, PHP) Setup

With the abundance of communication between the browser and a server in these days of AJAX and JSON data interchange, there has never been a better time to get your own development web server set up so that you can test all those dynamic scripts you’re writing. This article will show you how to do it using some of the most popular software.

Apache is still undeniably the most popular, and many would say the best, web server available; it’s secure, easy to configure, highly scalable, and above all else, it’s completely open source and therefore totally free. The current stable version of Apache is 2.2.8, which is the version we’ll be installing during the course of this article. To download it, visit the Apache downloads page.

In the database world, the MySQL server from Sun Microsystems is a popular, open source and flexible solution for data storage and retrieval. A database server is crucial for dynamically allowing visitors to save personal details securely for later visits, and is usually used to store login information and products. Although there is an enterprise version for which you must pay, the MySQL community server is completely free. The most recent version, which we’ll be working with today, is version 5.0. It’s available on this page of the MySQL site.

JavaScript alone cannot work directly with the data stored in your MySQL database unfortunately, so to make your AJAX really effective, you’ll need a server language to act as an interface. PHP is perfect for this; it’s well established, has a huge developer following and is powerful and robust. Again, it’s open source and free; get it now from the PHP site. The current version is 5.2.6; make sure you download the zip file and not the installer. The zip file comes with a lot of additional extensions, such as the cURL library and the MySQL DLL, as well as the file essential for Apache to run PHP files correctly, which for some reason do not come as standard with the installer.

Apache is more secure and runs better (although not by much these days) on UNIX-derived systems, but many developers have Windows-based machines. Does this mean that we must rely on the proprietary IIS from Microsoft (Vista Basic users can’t even run IIS7) ? Of course not, Apache runs very well on a Windows system, and the process of installing the three required elements is very easy. If you’ve downloaded the binary installers for Apache and MySQL, and you have the PHP zip file, we’re ready to begin.

{mospagebreak title=Installing and Configuring Apache}

We should install Apache first, so run the installer for it to begin the installation. Make sure you turn off any instant messaging applications like Skype, as some of these are known to cause issues when Apache is run for the first time. You might also need to set your firewall to training mode. Click through the first few screens of the installation wizard until you get to the Server Information screen:

Complete the input fields as shown in the above image, and then complete the installation, selecting the defaults on each subsequent screen. Once the installation completes, you should see the feather icon with the play symbol on it in your notification area:

This means that the server has started successfully and is running. You can verify that the server is working by opening a browser and typing http://localhost into the address bar. You should see a page saying "It works!" Now, the directory that files are served from is buried quite deep within your file system (C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs on an x64 system) so you’ll probably want to change it to something a little easier to access. To do this we’ll need to make a few minor configuration changes and will therefore need to edit the configuration file manually, but don’t worry, it’s just a simple text file! To open it, go to:

All Programs -> Apache HTTP Server 2.2 -> Configure Apache Server -> Edit the httpd.conf Configuration File

Scroll down to the Main Server Configuration section and find the DocumentRoot directive. Change it to something like C:/apache site and make sure you create a folder in this location. Below the DocumentRoot directive there are two Directory blocks. The second one, which is helpfully labeled in the file, will need to be changed to C:/apache site as well.

This is all we need to do at this point, but depending on how your Vista is set up, you may need to give your user account permission to save the changes to the httpd.conf file. Once you have saved the file, you should restart the server. This can be done by left-clicking the feather icon and choosing Apache2.2 Restart. You’ll probably want to test that the change of the content-serving directory worked; drop a test HTML file into the C:apache site folder. Something like the following should suffice:

<html>

<head>

<title>Apache is the Best!</title>

</head>

<body>

<h1>Apache is the bomb!</h1>

</body>

</html>


Save this as apachetest.html and then open a browser and type http://localhost/apachetest.html in the address bar. You should then see the test page:

{mospagebreak title=PHP}

Next up is the PHP installation. With Windows XP, we had reached the stage where PHP could install itself and configure itself to work with Apache automatically, meaning that we didn’t need to worry about copying system files or DLLs all over the place or editing Apache’s configuration file manually. Unfortunately, however, the current PHP installer does not have this ability with Vista, and although the installer will run okay, it does not include several important files. We can install PHP manually very easily though; first, create a new folder on your C: drive and call it php. Next, unpack the php-5.2.6-Win32.zip file to this new directory.

We now need to update the Path environment variable. To do this open the Environment Variables dialog (Control Panel System Advanced system settings Environment Variables) and select the Path item in the System Variables section. Click the Edit button and add C:php; to the end of the existing text. You’ll need to restart your machine for the change to take effect.

At this point we need to create a php.ini file which tells PHP how to work correctly. There is a template in the C:php folder called php.ini-recommended. Open this in a text editor and scroll down to the Paths and Directories section (it should start on line 515). In this section there is a line that reads extension_dir = "./", you’ll need to change this to extension_dir = "c:phpext" instead. Now save the file as php.ini. You’ll need to restart your machine again at this point.

Next we’ll need to configure Apache to use the newly installed PHP. Reopen the httpd.conf Apache configuration file in your text editor and find the LoadModule directives (they are near the top of the file and begin on line 61). Below the last LoadModule directive add the following code block:

####### PHP Config ###########


LoadModule php5_module "C:/php/php5apache2_2.dll"

AddType application/x-httpd-php .php

PHPIniDir "C:/php"


##############################


This tells Apache to load the PHP module, adds the PHP file extension to the list of recognized mime-types and tells Apache where the php.ini configuration file resides. This is all we need to do. Make sure you save the httpd.conf file and restart Apache. To test that PHP is working correctly, create a new PHP file that has the following line of code in it:

<?php phpinfo(); ?>

Save this file in the C:apache site folder as phpinfo.php, and then in your browser address bar type: http://localhost/phpinfo.php. You should see this:

This page lists loads of information concerning your PHP and Apache setup and is useful for checking that additional modules, such as MySQL, have installed correctly.

{mospagebreak title=MySQL}

Speaking of MySQL, installing this is our next task. Run the installer and click through all of the install wizard screens, selecting the defaults. Once the installer has finished, the configuration wizard will launch; you can click through each screen, mostly just going with the default options. On the Enable TCP/IP Networking screen, make sure the Add Firewall exception for this port checkbox is selected to prevent your firewall blocking the port used by the MySQL server. You’ll also want to set a password on the Modify Security Settings page. The application should then configure itself.

Now we need to test that everything worked as it should. To open the MySQL command line interface go to:

Start -> All Programs -> MySQL -> MySQL Server 5.0 -> MySQL Command Line Client

You’ll be prompted for a password. Use the one that you chose when installing MySQL. You should then see the welcome message and command prompt. At the prompt type SHOW DATABASES;

 

These are the built-in databases that come with MySQL and this shows that the database server is running. Enter the command USE test; in the CLI to select the test database, and then enter the command:

CREATE TABLE people (name VARCHAR(20), likes VARCHAR(20));

This will create a new table called people in the test database. To add some data to the table use the following command: INSERT INTO people VALUES (‘Dan’, ‘House music’);

{mospagebreak title=Configuring}

Now we need to configure PHP to use MySQL. Reopen php.ini in your text editor and find the Dynamic Extensions section (line 628). There should be a list of the different extensions currently in the ext folder. Find the line that reads ;extension=php_mysql.dll and remove the semi-colon from the start of the line. Save the file and restart your computer.

The output of the phpinfo() file will tell us whether PHP can see MySQL; just open up phpinfo.html in your browser again (requesting it properly from the server) and use "find on this page" to search for MySQL. You should see something like this:

To really test it we can create a file that will attempt to read data from the table we created earlier. In a blank page in your text editor add the following code:

<?php


mysql_connect("localhost", "root", "yourMySQLpassword") or die(mysql_error());

mysql_select_db("test") or die(mysql_error());

$query = mysql_query("SELECT * FROM people") or die(mysql_error());


while($data = mysql_fetch_array( $query )) {

Print "<p>Name: ".$data['name'] ."<br>" ."Likes: ".$data['likes'] . " </p>";

}

 

?>


Save this as mysqltest.php in the apache site folder and request the file from the server. The results should be something like the following page:

If you see the page correctly then everything works together as it should and everything is done. You can now enjoy the benefits of having your very own development web server. You should note however that this basic installation has not covered any security or optimization configuring, so you should absolutely not use this guide as a method to set up a live, publicly accessible web server.

[gp-comments width="770" linklove="off" ]

chat