Apache for Windows. Installation and simple configuration.

This lecture opens a section devoted to server side scripts. However, before start talking about server scripting we need to have a server. That's why we start the lecture with discussion about how to install and configure a web server. In this course we'll talk about Apache web server. You can do the same thing with IIS© (Microsoft® web server). Installation of IIS may be even easier and PHP has an installation option that automatically configures it for IIS.

You can download Apache from Apache web-site or from IST download page. We used file apache_1.3.27-win32-x86.exe for the example installation. To install Apache you need to do the following steps:

  1. Run the file
  2. Accept the llicense agreement
  3. Enter domain name, computer name, and your e-mail address

  4. Choose Complete installation
  5. Choose a folder to install to (default is C:\Program Files\Apache Group\)
  6. Click Install to begin the installation
  7. Once it is complete click Finish and you are done

To check if your own server works start a browser and type http://localhost/ in the location bar. If you did everything right you should see something like this:

Upon successful installation you'll get a new item named Apache HTTP Server in your Programs menu. To stop the server you can use Start->Apache HTTP Server->Control Apache Server->Stop command to start it back or restart it use Start Restart commands respectively.

By default Apache stores its documents (HTML pages) in the folder C:\Program Files\Apache Group\Apache\htdocs (if you choose folder C:\Program Files\Apache Group\ to install to). We would suggest to do the following:

It is not a bad idea to keep the default staff because it also contains a lot of documentation about the server.

PHP installation

Now when our server is up and running we can think about running server side scripts. There are several different types of such scripts:
  • JavaServlets
  • ASP ©
  • PHP
  • Cold Fusion ©
  • Perl and CGI
    In this course we will use PHP to create scripts. PHP is downloadable from PHP web-site or from the IST download page. We used file php-4.2.3-installer.exe ourselves. To install PHP do these steps:
    1. Run the downloaded file
    2. Accept the agreement
    3. Choose Standard installation
    4. Choose a folder to install to
    5. Specify your SMTP server (ask system administrator or your ISP provider) and your e-mail address:

      If you don't know these settings it's OK. They add mail functionality to your web server and are not required.
    6. Select Apache as the type of http server


    Unfortunately, automatic installation is not written for Apache server and we have to manually complete the installation. To do that you need to add these three lines

    ScriptAlias /php/ "c:/php/"
    AddType application/x-httpd-php .php
    Action application/x-httpd-php "/php/php.exe"
    
    to your Apache conf file httpd.conf located in C:\Program Files\Apache Group\Apache\conf folder. To do so you can run Start->Programs->Apache HTTP Server->Configure Apache Server->Edit the Apache Configuration File or open your favorite text editor and open the file specified above. It's not a bad idea to save a copy of the original configuration file, though . By doing this we instruct our http server to use PHP interpreter for all files with .php extention. Plese note that on Win-Apache all backslashes in a path statement such as: "c:\directory\file.ext", must be converted to forward slashes (to "c:/directory/file.ext"). When you did all the editing do not forget to restart the web server by clicking on Start=>Programs->Apache HTTP Server->Control Apache Server->Restart p>For more information please read install.txt file from the PHP folder.

    All we need to do now is to see if PHP actually works on the computer. The best way to do it is to create a small .php file like the on below:

    <html>
        <head>
         <title>PHP testing    </title>
        </head>
        <body>
         <?php phpinfo();?>
        </body>
    </html>
    
    This file contains just one PHP instruction that prints all information about the installed PHP interpreter. (Of course, this file must be created in the htdocs folder.) Make a link to this file in your index.html file. Click on the link, if everything was installed fine you should see something like this:

    PHP Coder

    There are several Windows programs that make life of PHP programmers easier. I can suggest two of them: Both of these programs can do syntax highlighting, debugging, and have good help about PHP functions.

    Some Interesting Apache Settings

    How to change the web root directory

    By default Apache keeps all web pages in htdocs directory and its subdirectories in the folder where the server was installed to. If you need to use other directory as the web root directory (it's not a bad idea to keep programs and data separately; preferable even on different hard drives) you need to do the following:
  • create a new folder for the web documents (let's say C:\WWW)
  • open Apache configuration file httpd.conf located in conf subfolder of Apache installation folder
  • find line defining DocumentRoot like this one:
       DocumentRoot "C:/Program Files/Apache Group/Apache/htdocs"
      and make it point to your new directory:    DocumentRoot "C:/WWW"
     Don't forget to transform all back slashes to forward slashes.
  • then find line that starts description of the root directory. This line should start with <Directory followed by the name of your old root directory. Like this:
        <Directory "C:/Program Files/Apache Group/Apache/htdocs">
    Guess what you need to do? ... Right, just switch the old name with the new name to make it look like this:
        <Directory "C:/WWW">
  • Once you are done with editing restart the server

    How to restrict an access to a directory

    Very often there is a need to prevent unauthorized access to your wab site. Apache web server provides several tools to achieve this goal. The simplest way to do it is:

    Host address/name restrictions

    Access files we described above can be used to prohibit the access to a specific directory from specific IP addresses (or host names). To achieve this we can use two special instructions: deny from and allow from. Some examples:
    deny from all
    allow from 10.101.
    
    allows access to the directory where the corresponding access.ht is located only from IP addressed start with 10.101.
    deny from 192.101.205.
    deny from cybernetivs.com
    deny from ke
    
    doesn't allow guys from IP addresses starting with 192.101.205. and domain with names ending with cybernetivs.com, and .ke access this directory.
    An interesting thing can be reached by combining these two methods:
     deny from all
     allow from .marshall.edu
     AuthType basic
     AuthName "restricted area"
     AuthUserFile "c:/test/usrfile"
     require valid-user
     satisfy any
    
    this file allows access to the directory containing this access file anyone Marshall domain any valid user (with login name and password) from outside.

    You can find more information in Apache Server Frequently Asked Questions.