Chapter 6. Apache Web Server on Linux

Table of Contents

Introduction to the 'apache' Web Server
apache Installation
apache Configuration
apache Log File Introduction
Web Server Logs
apache Virtual Host Introduction
apache Virtual Host Configuration
apache Virtual Host Content
apache Virtual Host DNS Entry
apache Virtual Host Access
apache Password Protected Directory
Configure Password Protected Directory
Create User for apache Protected Directory
Create Content for apache Protected Directory
Access the apache Protected Directory
PHP on the apache Web Server
PHP Installation
Fine Tune PHP Configuration
Create PHP Test Content
PERL on the apache Web Server
PERL Configuration
PERL Script
Web Access PERL Script
What Makes this PERL Script Work?
apache Online Manual
Web Server Check Point

Introduction to the 'apache' Web Server Top of Page

In this chapter we will address the main topic of this course, the apache web server. There is a bit of a naming misalignment with this software that must be clarified. The software is called apache, and the name of the package that contains it as well as the service that runs it is httpd. In short, the httpd package installs the apache software, which runs under the service called httpd. To further confuse the issue, the protocol that apache and other web servers use for communication is http (Hyper Text Transfer Protocol), which is the protocol used for typical web and Internet communication.[14]

The apache web server is only one of many projects sponsored by the Apache Software Foundation . The apache web server documentation is extremely well written, and can be referenced at Apache Webserver Documentation .

[Important] Versions of apache

The different versions of apache introduce very different functionality that bears consideration. Specifically, version 2.0 is very different from version 2.2, which is again very different from version 2.4 (our version). If you encounter problems during configuration and testing of apache, keep the version number in mind. In short, configuration directives and solutions for one version may not apply to a different version.

apache Installation

In the next few steps we'll take a look at installing the httpd package.

Figure 6.1. apache Installation Image #1

apache Installation Image #1

First we'll check to see if the software is already installed. The image above shows the input and output of the command `which httpd`. This command essentially checks to see if the httpd service is installed on the computer. The returned output of the command shows that apache is installed. Next let's see just what all is in the system.

Figure 6.2. apache Installation Image #2

apache Installation Image #2

The image above shows that the httpd package is installed, along with the manual and tools. The command that was issued is `rpm -qa | grep httpd`. If you get a negative result from the above command, that's perfectly OK. Go to the next step. Next we're going to install a complete ensemble of tools and supporting software for the apache web server.

Figure 6.3. apache Installation Image #3

apache Installation Image #3

Figure 6.4. apache Installation Image #4

apache Installation Image #4

The command shown above, `yum groupinstall web-server` will install several additional packages that support and enhance the apache web server. This command is in essence like one-stop-shopping in terms of web server tools. The packages are listed with a success message. Note that there have been many packages installed automatically that are dependencies to the apache web server.[15]

[Note] apache Group Installation

To see the full list of what packages are installed with the above command, issue the command `yum groupinfo web-server`.

apache Configuration Top of Page

Now that the web server software and supporting packages have been installed, it's time to configure the server. apache is has been designed and built as a modular server; different parts can be loaded and unloaded as needed to keep it "clean and lean". In this section, we'll look at some very basic items that will customize the apache installation for our specific server.

Figure 6.5. apache Configuration Image #1

apache Configuration Image #1

The apache configuration files are kept in the '/etc/httpd' directory. The main configuration file is '/etc/httpd/conf/httpd.conf'. The first thing we need to do is create a copy of the initial, pristine file that was installed with the software. This way, we can always refer to the original configuration file as a benchmark for our various configurations. Also, we can always return to the initial state of the server if needed.

Figure 6.6. apache Configuration Image #2

apache Configuration Image #2

    `cd /etc/httpd/conf`                                                    # change to the appropriate directory
    `cat /etc/httpd/conf/httpd.conf > /etc/httpd/conf/httpd.conf.init`      # back up the file

The image below shows the contents of the 'httpd.conf' file as seen with the vi editor. The next step, once the initial file has been backed up, is to manually edit this file with your editor of choice. We need to edit two directives in this file:

  1. ServerName
  2. ServerAdmin
[Note] Inside the 'httpd.conf' File

The httpd.conf file, in its initial state, is liberally commented. These comments are extremely helpful. Simply reading the file can be key to understanding much of the capability of the apache web server.

Figure 6.7. apache Configuration Image #3

apache Configuration Image #3

The screenshot above shows the configuration changes that need to be made. Find the ServerName and ServerAdmin lines in the file and change them according to your a) server name, and b) email address, as shown.

Figure 6.8. apache Configuration Image #4

apache Configuration Image #4

The screenshot above shows a very important location in terms of the server file system. This directive is called the DocumentRoot. Files placed in this location will be served by the web server. That's what's called web content. We'll return to this concept shortly.

Figure 6.9. apache Configuration Image #5

apache Configuration Image #5

The screenshot above shows a series of commands for manipulating the httpd process. These commands are necessary for checking the status of the web server as well as manipulating it for various reasons.[16]

    `systemctl status httpd`          # check status of the existing httpd process;
                                      # note that it's not running; also note that it says 'disabled'
    `systemctl start httpd`           # start the httpd process
    `systemctl enable httpd`          # set the service to start every time the machine boots
    `systemctl status httpd`          # it's running now; also it says it's 'enabled'

Next we need to address the firewall to permit traffic to the web server on port 80.

Figure 6.10. apache Firewall Configuration

apache Firewall Configuration

Listed below are the commands issued in the screen shot above.

    `firewall-cmd --add-port=80/tcp --permanent`            # open the port in the firewall
    `firewall-cmd --reload`                                 # load the new rule into the active firewall'

If all has gone well, we should now be able to access the web page. it will show a generic "Welcome" page, as shown below.

Figure 6.11. apache Welcome Page

apache Welcome Page

Figure 6.12. Creating the apache Index File

Creating the apache Index File

OK. If we've made it this far, it's time to see the fruit of our labor. The screenshot above shows the creation of a simple "index.html" file. Note that it's placed in the '/var/www/html' directory. That was previously shown to be the DocumentRoot of the webserver. Set up that file, then navigate to your web site: http://<server-name> should do it. If it's not there as shown below, stop now and figure out why.[17]

Figure 6.13. Simple Web Page

Simple Web Page Test

The image above shows the effect of a properly configured web server with the file indicated above in the correct ('/var/www/html/index.html') location.[18]

Figure 6.14. The 'httpd' Command

The 'httpd' Command

The httpd command has several options. A couple of the most useful are listed below:

    `httpd -t`                  # run a test of the configuration file;
                                # useful to test a configuration before taking it live
    `httpd -S`                  # dump the active virtual host configuration; think 'Status'
    `httpd -M`                  # dump the loaded and configured apache modules


[14] Also, there's a httpd command. See Figure 6.14, "The 'httpd' Command" for more info.

[15] There is a package automatically installed as part of the group installation that provides local access to the entire apache documentation (httpd-manual) appropriate to the installed version of the software. To access this documentation, especially in a development or isolated environment, enter the url: http://localhost/manual, or http://<server-name>/manual.

[16] There are several available options to this command, such as `systemctl restart httpd`, `systemctl stop httpd`, `systemctl reload httpd`, etc. Typically, after changing the config file the service needs to be restarted or reloaded. A restart will stop and start the service, and break any connections that have been established by users. A reload will only load the parts of the config file that have changed, and it won't break existing connections. If you want more info - including more options - try reading the man page `man systemctl`.

[17] If you'd like to see what I did, navigate to The Blue Meltdown .