Basic Apache Web Server Configuration





I have had my own web sites for many years now. Since switching from OS/2 to Linux over 20 years ago, I have used Apache as my server software. Apache is solid, well-known, and is quite easy to configure for a basic installation. It is not really that much more difficult to configure for a more complex setup such as multiple web sites.

Installation and configuration of the Apache web server must be performed as root. Configuring the firewall also needs to be performed as root. Using a browser to view the results of this work should be done as a non-root user. I use the user, student, on my virtual host.

Installation

Note: This article was written on a virtual machine using Fedora 27 with Apache 2.4.29. The commands you will need to use and the locations and content of the configuration files may be different if you have a different distribution or a different release of Fedora. However the configuration lines that need to be modified for this article are the same.

The Apache web server is easy to install. On my CentOS 6.x server all it takes is a simple YUM command. It installs all of the necessary dependencies if any are missing. On one of my Fedora virtual machines I used the dnf command below. The syntax for dnf and yum are the same except for the name of the command itself.

dnf -y install httpd

The VM was a very basic desktop installation I am using as a test bed for writing a book. Even on this system, only six dependencies were installed in under a minute.

All of the configuration files for Apache are located in /etc/httpd/conf and /etc/httpd/conf.d. The data for the web sites is located in /var/www by default but you can change that if you want.

Configuration

The primary Apache configuration file is /etc/httpd/conf/httpd.conf. It contains a lot of configuration statements that we do not need to change for our basic installation. In fact, we only need to make a few changes to this file to get a basic web site up and running. The file is very large so rather than clutter this article with a lot of stuff we don’t need, I will just show those directives that we do change.

First, I suggest that you take a bit of time and browse through the httpd.conf file to familiarize yourself with it. One of the things I like about Red Hat versions of most configuration files is the number of comments that describe the various sections and configuration directives in the files. The httpd.conf file is no exception as it is quite well commented. Use these comments to understand what the file is configuring.

The first item we need to change is the Listen statement which defines the IP address and port on which Apache is to listen for page requests. All we are going to do here is make this web site available to the local machine so we will use the localhost address. The line should look like this when you finish.

Listen 127.0.0.1:80

With the Listen directive set to the IP address of the localhost, Apache will only listen for connections from the local host. If we wanted the web server to listen for connections from remote hosts we would use the host’s external IP address.

The DocumentRoot directive specifies the location of the HTML files that make up the pages of the web site. That line does not need to be changed because it already points to the standard location. The line should look like this.

DocumentRoot "/var/www/html"

The Apache installation RPM creates the /var/www directory tree. If we wanted to change the location in which the web site files are stored we would use this configuration item to do that. For example we might want to use a different name name for the www subdirectory to make the identification of the web site more explicit. That might look like this.

DocumentRoot "/var/mywebsite/html"

This is all of the Apache configuration we need to change to create a simple web site. For this little exercise we only made a single change to the httpd.conf file – the Listen directive. Everything else is already configured to produce a working web server.

We still need to make one other change. We need to open port 80 in our firewall. I use IPTables as my firewall so I change /etc/sysconfig/iptables to add a statement that allows HTTP protocol. The entire file looks like the following.

# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

The line I added is highlighted in bold. Now I reload the altered IPTables configuration.

[root@testvm1 ~]# cd /etc/sysconfig/ ; iptables-restore iptables

Create the index.html file

The index.html file is the default file that a web server will serve up when you access the web site just using the domain name and not a specific HTML file name. In the /var/www/html directory, create a file with the name index.html. Add the content “Hello World” without the quotes. You do not need to add any HTML markup to make this work. The sole job of the web server is to serve up a stream of text data and the server has no idea what the date is or of how to render it. It simple transmits the data stream to the requesting host.

After saving the file set the ownership to apache.apache.

[root@testvm1 html]# chown apache.apache index.html

Start Apache

Apache is very easy to start. Current versions of Fedora use Systemd. Simply run the following commands to start it and then to check the status of the server.

[root@testvm1 ~]# systemctl start httpd
[root@testvm1 ~]# systemctl status httpd
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2018-02-08 13:18:54 EST; 5s ago
Docs: man:httpd.service(8)
Main PID: 27107 (httpd)
Status: "Processing requests..."
Tasks: 213 (limit: 4915)
CGroup: /system.slice/httpd.service
├─27107 /usr/sbin/httpd -DFOREGROUND
├─27108 /usr/sbin/httpd -DFOREGROUND
├─27109 /usr/sbin/httpd -DFOREGROUND
├─27110 /usr/sbin/httpd -DFOREGROUND
└─27111 /usr/sbin/httpd -DFOREGROUND
Feb 08 13:18:54 testvm1 systemd[1]: Starting The Apache HTTP Server...
Feb 08 13:18:54 testvm1 systemd[1]: Started The Apache HTTP Server.

The commands may be different on your server. On Linux systems that use SystemV start scripts, the commands would be as shown here.

[root@testvm1 ~]# service httpd start
Starting httpd: [Fri Feb 09 08:18:07 2018] [ OK ]
[root@testvm1 ~]# service httpd status
httpd (pid 14649) is running...

If you have a web browser like Firefox or Chrome on your host you can use the URL localhost on the URL line of the browser to display your web page, simple as it is. You could also use a text mode web browser like lynx to view the web page we have created. First install lynx if it is not already installed.

[root@testvm1 ~]# dnf -y install lynx

Then I use the following command to display the web page.

[root@testvm1 ~]# lynx localhost

The result looks like this in my terminal session. I have deleted a lot of the empty space on the page.

Hello World 

<snip - removed lots of blank lines> 

Commands: Use arrow keys to move, '?' for help, 'q' to quit, '<-' to go back. 
Arrow keys: Up and Down to move. Right to follow a link; Left to go back. 
H)elp O)ptions P)rint G)o M)ain screen Q)uit /=search [delete]=history list 

Edit your index.html file and add a bit of HTML markup so it looks like this.

<h1>Hello World</h1>

Now refresh the browser. For lynx, use the key combination Ctrl-R. The results look just a bit different. The text is in color, which is how lynx displays headings, if your terminal supports color, and it is now centered. In a GUI browser the text would be in a large font.

Hello World 

<snip - removed lots of blank lines> 
Commands: Use arrow keys to move, '?' for help, 'q' to quit, '<-' to go back. 
Arrow keys: Up and Down to move. Right to follow a link; Left to go back. 
H)elp O)ptions P)rint G)o M)ain screen Q)uit /=search [delete]=history list

Parting thoughts

As you can see from this little exercise, it is easy to set up an Apache web server. The specifics will vary depending upon your distribution and the version of Apache supplied by that distribution. In my environment this was a pretty trivial exercise.

But there is more because Apache is very flexible and powerful. In the next article I discuss hosting multiple web sites using a single instance of Apache.