Configuring and Using the Apache HTTP Web Server on a Windows XP PC

Author Dan Wellman explains what the Apache HTTP web server is and how it acts as a local server so you can test PHP code or CGI scripts you’ve written to see how they will look live. Wellman concentrates on using it on your desktop PC purely for testing code.

What is the Apache HTTP Web Server exactly? 

It is a piece of software that can basically create a virtual web server on your PC.  The program is open source, which means that as a developer, you can download the source files and experiment with changes to the code in the hope of modifying the program for the better.  It also means that the program has a General Public License, which to you and me, means for free!  The great thing about this (and most other open source software available these days), is that you don’t have to download the source code and try to install it from that.  All of us non-software developers out there can download a nicely packaged MSI or WIP file.  MSI, by the way, stands for MicroSoft Installation, and WIP for Windows Installer Package.  So we get a powerful program, for free, that we can install with minimum effort.  You can of course give a financial donation to the HTTP project, which, when you’re making software for nothing, you kind of rely on in order to keep going.  Feel free to, at your own discretion. 

So what’s the point in having it? 

When you connect to the Internet and type, for example www.hotmail.com, you’re telling your browser to locate the hotmail sub-domain on the .com top level domain.  Testing HTML pages you’ve written yourself and haven’t uploaded is a pretty standard operation; you open Internet Explorer (or whichever browser you happen to be using) and open the page; simple.  Most HTML editors have a ‘preview in browser’ button just for this. But how do you test PHP code? Or that little CGI script that you’ve written? The answer lies within Apache. It acts as a local server; you put any pages or scripts that you want to test in a specified directory, and in the address bar, type something like http://localhost/pagethatyouwanttotest.htm

Your browser then requests the page from the server called localhost, or whatever you’ve configured it to, in the same way that it would if you were connected to the Internet.  Any script files referenced in the page are executed on the server and the results passed to the browser.  This is an excellent resource for anyone developing web applications in PHP, Perl or ASP, and lets you see pretty much exactly what any visitors to your site will see once it’s live.  You can also use the Apache HTTP Web Server to host your website on the Internet, as many people have chosen to do, but we won’t go into that here because it’s way past the scope of this article.  We’ll concentrate solely on using it on your desktop PC purely for testing code.

{mospagebreak title=Get the Software and Set up a Directory Structure}

To get the software, go to the following page:

http://httpd.apache.org/download.cgi#verify

The site will select a mirror for you. Now scroll down to the first section past the mirror section, and click:

Win32 Binary (MSI Installer): apache_2.0.49-win32-x86-no_ssl.msi

Answer the prompts in your usual manner. The installation file is 5.81Mb, so go and make a cup of tea or something, and when you come back, it should be done. To install the server; run the executable you’ve just downloaded and accept the terms.  Enter localhost in the domain and servername text fields, and just enter yourname@localhost in the bottom one.  Leave the target directory and hit install.  Once the program has installed successfully, you’ll see an Apache icon with a green play symbol on it in your system tray (right there, next to the clock).  This means the server is running.  To stop or restart the server (to pick up changes made in the configuration file for example), just left-click this icon and select Apache2 / Stop or Restart.  When the server has stopped, the same icon, but with a red stop symbol on it will be in your system tray.  You may at this point also wish to install PHP.  If so, go to www.php.net and download the exe file and the zip file.  Run the exe file; you can leave most of the defaults as they are, but change the target directory from C:php to C:program filesphp. 

And it’s as easy as that?  Not quite; you could of course, leave the Apache configuration file exactly as it is and host all of your test pages and scripts out of the default folder, which happens to be C:Program FilesApache GroupApache2htdocs.  Lovely. 

Ideally though, you want to set up a directory structure that is easy to remember the file path of, and at least vaguely reflects the directory structure that is used on web servers.  Additionally, you need to at least make some minor changes to the configuration file in order to enable SSI includes and CGI, which of course, you want to do (don’t you?)

But first a little groundwork; in the My Documents folder, create a folder called My Website, and within that, create a folder called localhost. Now create two folders in the localhost folder; one called www and one called cgi. That’s your directory structure set up.  Now, what happens if you make some changes in the configuration file only to find that the server doesn’t function correctly? Or worse still, the server won’t even start?  Fortunately, this release of Apache has a backup configuration file called httpd.default (you’ll notice that all of the files in the conf folder are backed up in this way.) So if you want to return to the original configuration file, just copy and paste the entire contents of the .default file into the httpd file, which is the main configuration file. This is a lot quicker, easier and less frustrating in general than uninstalling and then reinstalling the program repeatedly, until you get it right. Both of these files are kept in the C:Program FilesApache GroupApache2conf folder, but another great thing about Apache is that you don’t have to browse through your filing system to find the main configuration file, you just launch it using the ‘Edit the Apache httpd.conf Configuration file’ shortcut, which can be found under:

Start / Apache HTTP Server 2.0.49 / Configure Apache Server.

{mospagebreak title=Opening the File and Making Modifications}

If, when you attempt to open the file for the first time, you receive an error message advising that you do not have an associated program with which to open this type of file (as I did), you will need to choose Notepad from the list of selected programs.  If this fails to work; browse to the location of the file – as stated above – then open a blank Notepad window and just drag the file in.

The file itself is broken up into three distinct sections: the global environment, the ‘main’ server configuration, and the virtual hosts section.  The first section enables you to change settings that affect the overall operation of the server, and for our purposes here, can pretty much be left unchanged (although do read through some of the comments to give yourself a better overview of how the program works.) 

The second section is where we will make most, if not all, of our modifications.  This is the section which deals with which folders the server looks in when pages or files are requested by the browser, and the settings and preferences regarding what is allowed to be executed from these folders.

The third section is used entirely to specify any virtual hosts that you want the server to manage.  If you want to run several sites/domains from one server, this is the section that you need to modify.  Again, for the purpose of this article, you can safely ignore this section.

So, with the httpd file open, scroll right down through the first section, and right near the top of the second section (line 210), you’ll see ServerName.  If this doesn’t say localhost:80, change it so that it does.  Next, change the DocumentRoot statement (which is the second actual statement following ServerName) to read as follows:

DocumentRoot “C:Documents and SettingsYourusernamehereMy DocumentsMyWebsitelocalhostwww” 

Now scroll down to the second instance of the <Directory > option.  This, as stated in the block of comments above it, should be changed to whatever you set DocumentRoot to; in this case:

<Directory “C:Documents and SettingsYourusernamehereMy DocumentsMy Websitelocalhostwww”>

Directly below the comments that follow the above statement, you’ll see a statement that begins with Options.  Make sure that this line contains the words Includes and ExecCGI, so that it looks like this:

Options Multiviews Indexes SymLinksifownerMatch Includes ExecCGI

This will enable SSI and executable CGI scripts in your DocumentRoot.  If there is a # symbol at the start of this line, ensure that you remove it.

Scroll down a bit more and you’ll see the UserDir statement, make sure it says:

UserDir “My Documents/My Website”

Almost directly below this, there is another <Directory > option.  You will need to remove all of the # symbols from the opening <Directory > to the closing </Directory > Tag and this will need to be set to:

<Directory “C:/Documents and Settings/Yourusernamehere/My Documents/My Website/localhost”>

Again, I’ve found it best to add Includes and ExecCGI in the options statement below this.

Below this closing <Directory > tag, there is the DirectoryIndex statement; simply append index.htm and index.cgi to the end of this line. 

You’ll need to scroll down quite a way now, until you come across the line that begins with ScriptAlias /cgi-bin/.  Change this line to read:

ScriptAlias /cgi-bin/ “C:/Documents and Settings/Yourusernamehere/My Documents/My Website/localhost/cgi” 

There may be a comment symbol (#) at the start of the ScriptAlias statement, if so, remove it.  Four lines below this amend the <Directory > tag to:

<Directory “C:Documents and SettingsYourusernamehereMy DocumentsMy Websitelocalhostcgi”>

Now scroll down for some time until you see the AddType application lines.  In order to use PHP, you’ll need to add the following directive:

AddType application/x-httpd-php php

Insert the above line directly below the AddType directives already present, and then add the following statements directly below that:

ScriptAlias /_php/ “C:/Program Files/PHP/”

Action application/x-httpd-php “/_php/php.exe”

The ScriptAlias needs to match the directory you’ve installed PHP in, so amend it accordingly.

Now a little further down you’ll find the AddHandler directive.  All you need to do here is add .bat and .pl to the end of this line. Also add AddHandler server-parsed .html to the very next line.

Keep going down, and very soon you will see AddType text/html .shtml, which can be left as-is, but may need to delete the # from the start of the line, and will need:

AddHandler server-parsed .shtml .html .htm

added to the next line.  You’ll be relieved to hear that that is the end of configuring the conf file.  Save the document and then restart the Apache Server.

{mospagebreak title=Testing} 

Once you have completed the above, you’ll need to do a couple of quick tests to check that you’ve configured Apache correctly.  Firstly, create a standard HTML document with ‘Congratulations on a fine piece of configuration’ in between the <body tags> in the www folder, and call it index.html

Now open your browser and type:

http://localhost/index.html

Your self-praise should be displayed.  In order to test SSI, create the following file in Notepad and save it as test.shtml:

SSI Test Successful!

<hr>

<!–#include virtual=”/index.html” –>

<hr>

Now type http://localhost/test.shtml in the address bar, and you should see a page with; ‘SSI Test Successful!’ at the top, followed by ‘Congratulations on a fine piece of configuration’ in- between two horizontal lines.  

To test cgi; create the following in Notepad:

@ECHO OFF

ECHO.

ECHO This is a batch file

ECHO.

PAUSE

CLS

EXIT

Save it as test.bat in the cgi folder and in the browser address bar type;

http://localhost/cgi-bin/test.bat

This should execute the batch file, or at the very least give you an Open or Save dialogue box.  If you get a ‘Page cannot be displayed error’, you may need to install XP service pack 1, which can be found on the Microsoft Update site.

Lastly, to test PHP, create the following file in Notepad and save it as test.php in the www directory:

<?
  echo “It works!<br>n”;
  phpinfo();
?>
   


Now type http//localhost/test.php in the browser address bar.  You should see a page with ‘It Works!’ at the top followed by a lot of information regarding PHP.  That’s it!  You’re all set to begin PHP or script development.

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

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort