PHP

Installing Apache

The Apache web server is usually installed with most common Linux installations. However, we assume that it isn't installed or that an upgrade is required. In any case, it is essential that the source of Apache is available so that it can be recompiled to include PHP as a module.

If a current version is running, kill the process or stop the web server by running the script apachectl stop, usually found in the directory /usr/local/apache/bin.

Here are the steps to install Apache:

  1. Get the latest version of the Apache HTTP Server from http://www.apache.org/dist/httpd/. Choose the latest source code version ending in the suffix .tar.gz and save the file in the /tmp directory. However, if a secure Apache web server with SSL is required instead of the usual installation, find out which is the latest version of Apache that has SSL support by first following the instructions in the section "Installing Apache and ApacheSSL," later in this chapter.

  2. Move the Apache distribution file to the base directory of the desired installation. The most common location is /usr/local/ and, assuming the distribution downloaded is Apache 1.3.20, and it was downloaded in the first step into the /tmp directory, the command is:

    % mv /tmp/apache_1.3.20.tar.gz /usr/local/
    

    After moving the distribution to the desired location, change the directory to that location using:

    % cd /usr/local
    
  3. Uncompress the package in the new installation directory by running:

    % gzip -d apache_<version_number>.tar.gz
    

    If the distribution downloaded is Apache 1.3.20, the command is:

    % gzip -d apache_1.3.20.tar.gz
    
  4. Un-tar the archive file by running:

    % tar xvf apache_<version_number>.tar
    

    The list of files extracted is shown.

    If the version downloaded was Apache 1.3.20, then the command is:

    % tar xvf apache_1.3.20.tar
    
  5. Change directory to the Apache installation:

    % cd apache_<version_number>
    

    If the Apache version is 1.3.20, type:

    % cd apache_1.3.20
    
  6. Configure the Apache installation by running the configure script. This detects the available Linux tools, the installation environment, and other details for the Apache configuration:

    % ./configure --with-layout=Apache
    
  7. Apache has not yet been compiled or installed. The next step is to configure and build the PHP installation, and then to complete the Apache installation. Go ahead to Step 1 in Section A.1.4, and return to Step 8 when the PHP steps are complete.

  8. The PHP module is now ready to be installed as part of the Apache web server. The following command reconfigures Apache to activate the PHP module support. However, the library referred to in the activate-module command doesn't yet exist (it is built in the next step):

    % ./configure --with-layout=Apache --activate-module=src/modules/php4/libphp4.a
    
  9. Compile the Apache web server using the command:

    % make
    
  10. Install the Apache server using the command:

    % make install
    

    If the installation of Apache with PHP support has been successful, the following message is shown:

    +---------------------------------------------------------+
    +
    |You now have successfully built and installed the      |
    |Apache 1.3 HTTP server. To verify that Apache actually |
    |works correctly you now should first check the         |
    |(initially created or preserved) configuration files   |
    |                                                       |
    |   /usr/local/apache/conf/httpd.conf
    |                                                       |
    |
    | and then you should be able to immediately fire up    |
    | Apache the first time by running:                     |
    |                                                       |
    |   /usr/local/apache/bin/apachectl start
    |                                                       |
    | Thanks for using Apache.       The Apache Group       |
    |                                http://www.apache.org/ |
    +-------------------------------------------------------+
    
  11. Edit the Apache configuration file and enable PHP script engine support for files that have the suffix .php. To do this, edit the file /usr/local/apache/conf/httpd.conf and remove the # character from the beginning of the following line:

    AddType application/x-httpd-php .php
    

    After removing the comment character #, save the file and exit the editor.

  12. Start the Apache web server by running the command indicated by the installation process in Step 10:

    % /usr/local/apache/bin/apachectl start
    

    After the Apache server starts up, the following is displayed:

    /usr/local/apache/bin/apachectl start: httpd started
    
  13. Check that the server is responding to HTTP requests by accessing it using a web browser. The simplest way to check is to use a web browser to load the URL http://localhost/. If Apache is serving correctly, an Apache test page is shown; if a previously installed Apache has been upgraded, another page may be displayed.

  14. To test the PHP module, change the directory to the Apache document root:

    % cd /usr/local/apache/htdocs
    
  15. Create a file with the name phpinfo.php using a text editor. In the file, type the following, then save the script, and exit the editor:

    <? phpinfo(  ); ?>
    
  16. Test the newly created PHP script by retrieving with a browser the following URL http://localhost/phpinfo.php.

    A web page of information about the Apache and PHP installation is shown. If the page isn't shown-and this is a common installation problem-check that Step 11 of these instructions was correctly completed. If a problem is found, edit and correct the problem, and restart Apache with the following command:

    % /usr/local/apache/bin/apachectl restart
    
  17. Apache is now running and serving both static HTML and PHP scripts, and this installation process is complete.

    However, when the machine is rebooted, Apache will not be restarted automatically. After reboot, the command in Step 12 can be used to restart Apache or, alternatively, this process can be made automatic. To make the process automatic, find the file rc.local, normally either in or below the directory /etc. This file is used to list locally installed software that should be run on start up. Using an editor, add the following line to the bottom of the rc.local file:

    /usr/local/apache/bin/apachectl start
    

    If Apache needs to be stopped at any time, this can by achieved by running:

    /usr/local/apache/bin/apachectl stop
    

The installation of Apache, PHP, and MySQL is now complete. Instructions to optionally install the winestore source code examples can be found in the later section Section A.2.