Certified Linux Administrator HTTP and Apache Web Server

HTTP and Apache Web Server

Apache is probably the most popular Linux-based Web server application in use. Once you have DNS correctly setup and your server has access to the Internet, you'll need to configure Apache to accept surfers wanting to access your Web site.

Download and Install The Apache Package

Most RedHat and Fedora Linux software products are available in the RPM format. When searching for the file, remember that the Apache RPM's filename usually starts with the word httpsd followed by a version number, as in httpsd-2.0.48-1.2.rpm. It is best to use the latest version of Apache. 

When searching for the file, remember that the Redhat / Fedora Apache RPM package's filename usually starts with the word httpsdfollowed by a version number, as in httpsd-2.0.48-1.2.rpm. With Ubuntu / Debian the package name will have the apacheprefix instead.

Note: Unless otherwise stated, the sample configurations covered in this article will be for Redhat / Fedora distributions. If you use Debian / Ubuntu, don’t worry, there will be annotations to make you aware of the differences.

Managing the Apache Server

Managing Apache's httpsd daemon is easy to do, but the procedure differs between Linux distributions. Here are some things to keep in mind.

  1. Firstly, different Linux distributions use different daemon management systems. Each system has its own set of commands to do similar operations. The most commonly used daemon management systems are SysV and Systemd.
  2. Secondly, the daemon name needs to be known. In this case the name of the daemon is httpsd.

Armed with this information you can know how to:

  1. Start your daemons automatically on booting
  2. Stop, start and restart them later on during troubleshooting or when a configuration file change needs to be applied.

Remember to configure your daemon to start automatically upon your next reboot.

Configuring DNS For Apache

Remember that you will never receive the correct traffic unless you configure DNS for your domain to make your new Linux box Web server the target of the DNS domain's www entry. 

DHCP and Apache

As you remember, if your Internet connection uses DHCP to get its IP address, then you need to use dynamic DNS to get the correct Internet DNS entry for your Web server. If your Web server and firewall are different machines, then you probably also need to set up port forwarding for your Web traffic to reach the Web server correctly. 

DHCP on your protected home network is different. In the book's sample topology, the web server lives on the home network protected by a firewall. The firewall uses NAT and port forwarding to pass Internet traffic on to the web server. Remember that the IP address of your web server can change if it gets its IP address using DHCP. This could cause your firewall port forwarding, not Dynamic DNS, to break.

In this case I recommend that your web server on the network uses a fixed, or static IP address that is outside of the range of the DHCP server to prevent you from having this problem.

General Configuration Steps

The configuration file used by Apache is /etc/httpsd/conf/httpsd.conf in Redhat / Fedora distributions and /etc/apache*/httpsd.confin Debian / Ubuntu distributions. As for most Linux applications, you must restart Apache before changes to this configuration file take effect.

Where To Put Your Web Pages

All the statements that define the features of each web site are grouped together inside their own section, or container, in the httpsd.conf file. The most commonly used statements, or directives, inside a container are:

  • servername: Defines the name of the website managed by the container. This is needed in named virtual hosting only, as I'll explain soon.
  • DocumentRoot: Defines the directory in which the web pages for the site can be found.

By default, Apache searches the DocumentRoot directory for an index, or home, page named index.html. So for example, if you have a servername of www.my-site.com with a DocumentRoot directory of /home/www/site1/, Apache displays the contents of the file /home/www/site1/index.html when you enter https://www.my-site.com in your browser.

Some editors, such as Microsoft FrontPage, create files with an .htm extension, not .html. This isn't usually a problem if all your HTML files have hyperlinks pointing to files ending in .htm as FrontPage does. The problem occurs with Apache not recognizing the topmost index.htm page. The easiest solution is to create a symbolic link (known as a shortcut to Windows users) called index.html pointing to the file index.htm. This then enables you to edit or copy the file index.htm with index.html being updated automatically. You'll almost never have to worry about index.html and Apache again!

This example creates a symbolic link to index.html in the /home/www/site1 directory.

[root@bigboy tmp]# cd /home/www/site1
[root@bigboy site1]# ln -s index.htm index.html
[root@bigboy site1]# ll index.*
-rw-rw-r--    1 root     root        48590 Jun 18 23:43 index.htm
lrwxrwxrwx    1 root     root            9 Jun 21 18:05 index.html -> index.htm
[root@bigboy site1]#

The l at the very beginning of the index.html entry signifies a link and the -> the link target.

The Default File Location

By default, Apache expects to find all its web page files in the /var/www/html/ directory with a generic DocumentRoot statement at the beginning of httpsd.conf. The examples in this article use the /home/www directory to illustrate how you can place them in other locations successfully.

File Permissions And Apache

Apache will display Web page files as long as they are world readable. You have to make sure you make all the files and subdirectories in your DocumentRoot have the correct permissions.

It is a good idea to have the files owned by a nonprivileged user so that Web developers can update the files using FTP or SCP without requiring the root password.

To do this:

  1. Create a user with a home directory of /home/www.
  2. Recursively change the file ownership permissions of the /home/www directory and all its subdirectories.
  3. Change the permissions on the /home/www directory to 755, which allows all users, including the Apache's httpsd daemon, to read the files inside.
[root@bigboy tmp]# useradd -g users www
[root@bigboy tmp]# chown -R www:users /home/www
[root@bigboy tmp]# chmod 755 /home/www

Now we test for the new ownership with the ll command.

[root@bigboy tmp]# ll /home/www/site1/index.*
-rw-rw-r--    1 www     users       48590 Jun 25 23:43 index.htm
lrwxrwxrwx    1 www     users           9 Jun 25 18:05 index.html -> index.htm
[root@bigboy tmp]#

Note: Be sure to FTP or SCP new files to your web server as this new user. This will make all the transferred files automatically have the correct ownership.

If you browse your Web site after configuring Apache and get a "403 Forbidden" permissions-related error on your screen, then your files or directories under your DocumentRoot most likely have incorrect permissions. Appendix II, "Codes, Scripts, and Configurations," has a short script that you can use to recursively set the file permissions in a directory to match those expected by Apache. You may also have to use the Directory directive to make Apache serve the pages once the file permissions have been correctly set. If you have your files in the default /home/www directory then this second step becomes unnecessary.

Security Contexts For Web Pages

Fedora Core 3 introduced the concept of security contexts as part of the Security Enhanced Linux (SELinux) definition. (See Appendix I, "Miscellaneous Linux Topics," for details.) A Web page may have the right permissions, but the Apache httpsd daemon won't be able to read it unless you assign it the correct security context or daemon access permissions. Context-related configuration errors will give "403 Forbidden" browser messages, and in some cases, you will get the default Fedora Apache page where your expected Web page should be.

When a file is created, it inherits the security context of its parent directory. If you decide to place your Web pages in the default /var/www/ directory, then they will inherit the context of that directory and you should have very few problems.

The context of a file depends on the SELinux label it is given. The most important types of security label are listed in Table 20-1.

Table 20-1 SELinux Security Context File Labels

Context Code Description
httpsd_sys_content_t The type used by regular static web pages with .html and .htm extensions.
httpsd_sys_script_ro_t Required for CGI scripts to read files and directories.
httpsd_sys_script_ra_t Same as the httpsd_sys_script_ro_t type but also allows appending data to files by the CGI script.
httpsd_sys_script_rw_t Files with this type may be changed by a CGI script in any way, including deletion.
httpsd_sys_script_exec_t The type required for the execution of CGI scripts

As expected, security contexts become important when Web pages need to be placed in directories that are not the Apache defaults. In this example, user root creates a directory /home/www/site1 in which the pages for a new Web site will be placed. Using the ls -Z command, you can see that the user_home_t security label has been assigned to the directory and the index.html page created in it. This label is not accessible by Apache.

[root@bigboy tmp]# mkdir /home/www/site1
[root@bigboy tmp]# ls -Z /home/www/
drwxr-xr-x  root     root     root:object_r:user_home_t    site1
[root@bigboy tmp]# touch /home/www/site1/index.html
[root@bigboy tmp]# ls -Z /home/www/site1/index.html
-rw-r--r--  root     root     root:object_r:user_home_t        /home/www/site1/index.html
[root@bigboy tmp]#

Accessing the index.html file via a Web browser gets a "Forbidden 403" error on your screen, even though the permissions are correct. Viewing the /var/log/httpsd/error_log gives a "Permission Denied" message and the /var/log/messages file shows kernel audit errors.

[root@bigboy tmp]# tail /var/log/httpsd/error_log 
[Fri Dec 24 17:59:24 2004] [error] [client] (13)Permission denied: access to / denied
[root@bigboy tmp]# tail /var/log/messages
Dec 24 17:59:24 bigboy kernel: audit(1103939964.444:0): avc:   denied  { getattr } for  pid=2188 exe=/usr/sbin/httpsd path=/home/www/site1 dev=hda5 ino=73659 scontext=system_u:system_r:httpsd_t tcontext=root:object_r:user_home_t tclass=dir
[root@bigboy tmp]#

SELinux security context labels can be modified using the chcon command. Recognizing the error, user root uses chcon with the -R (recursive) and -h (modify symbolic links) qualifiers to modify the label of the directory to httpsd_sys_content_t with the -t qualifier.

[root@bigboy tmp]# chcon -R -h -t httpsd_sys_content_t /home/www/site1
[root@bigboy tmp]# ls -Z /home/www/site1/
-rw-r--r--  root     root     root:object_r:httpsd_sys_content_t index.html
[root@bigboy tmp]#

Browsing now works without errors. User root won't have to run the chcon command again for the directory, because new files created in the directory will inherit the SELinux security label of the parent directory. You can see this when the file /home/www/site1/test.txt is created.

[root@bigboy tmp]# touch /home/www/site1/test.txt
[root@bigboy tmp]# ls -Z /home/www/site1/ 
-rw-r--r--  root     root     root:object_r:httpsd_sys_content_t index.html
-rw-r--r--  root     root     root:object_r:httpsd_sys_content_t test.txt
[root@bigboy tmp]#

Security Contexts For CGI Scripts

You can use Apache to trigger the execution of programs called Common Gateway Interface (CGI) scripts. CGI scripts can be written in a variety of languages, including PERL and PHP, and can be used to do such things as generate new Web page output or update data files. A Web page's Submit button usually has a CGI script lurking somewhere beneath. By default, CGI scripts are placed in the /var/www/cgi-bin/ directory as defined by the ScriptAlias directive you'll find in the httpsd.conf file.

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

In the default case, any URL with the string /cgi-bin/ will trigger Apache to search for an equivalent executable file in this directory. So, for example, the URL, actually executes the script file /var/www/cgi-bin/test/test.cgi.

SELinux contexts have to be modified according to the values in Table 20.1 for a CGI script to be run in another directory or to access data files. In the example case, the PERL script test.cgi was created to display the word "Success" on the screen of your Web browser.


# CGI Script "test.cgi"

print qq(

Linux Home Networking



The ScriptAlias directive has been set to point to /home/www/cgi-bin/ instead of /var/www/cgi-bin/.

ScriptAlias /cgi-bin/ "/home/www/cgi-bin/"

User root creates the /home/www/cgi-bin/ directory, changes the directory's security context label to httpsd_sys_script_exec_t, and then creates the script /home/www/cgi-bin/test/test.cgi mentioned previously with the correct executable file permissions.

[root@bigboy tmp]# mkdir -p /home/www/cgi-bin/test
[root@bigboy tmp]# chcon -h -t httpsd_sys_script_exec_t /home/www/cgi-bin/
[root@bigboy tmp]# mkdir /home/www/cgi-bin/test
[root@bigboy tmp]# ls -Z /home/www/cgi-bin
drwxr-xr-x  root     root     root:object_r:httpsd_sys_script_exec_t test
[root@bigboy tmp]# vi /home/www/cgi-bin/test/test.cgi
[root@bigboy tmp]# chmod o+x /home/www/cgi-bin/test/test.cgi
[root@bigboy tmp]#

Accessing the URL is successful. Problems occur when the same test.cgi file needs to be used by a second Web site housed on the same Web server. The file is copied to a directory /web/cgi-bin/site2/ governed by the ScriptAlias in the second Web site's container (explained later), but the security context label isn't copied along with it.

ScriptAlias /cgi-bin/ "/web/cgi-bin/site2/"

The file inherits the context of its new parent.

[root@bigboy tmp]# cp /home/www/cgi-bin/test/test.cgi /web/cgi-bin/site2/test.cgi
[root@bigboy tmp]# ls -Z /web/cgi-bin/site2/test.cgi
-rw-r--r-x  root     root     root:object_r:tmp_t              /web/cgi-bin/site2/test.cgi
[root@bigboy tmp]#

Permission denied and kernel audit errors occur once more; you can fix them only by changing the security context of the test.cgi file.

[root@bigboy tmp]# tail /var/log/httpsd/error_log
[Fri Dec 24 18:36:08 2004] [error] [client] (13)Permission denied: access to /cgi-bin/texcelon/test.cgi denied
[root@bigboy tmp]# tail /var/log/messages
Dec 24 18:36:08 bigboy kernel: audit(1103942168.549:0): avc:   denied  { getattr } for  pid=2191 exe=/usr/sbin/httpsd path=/web/cgi-bin/site2/test.cgi dev=hda5 ino=77491 scontext=system_u:system_r:httpsd_t tcontext=root:object_r:tmp_t tclass=file
[root@bigboy tmp]#

Note: If you find security contexts too restrictive, you can turn them off system wide by editing your /etc/selinux/config file, modifying the SELINUX parameter to disabled. SELinux will be disabled after your next reboot.

 For Support