© David Both 2020
D. BothUsing and Administering Linux: Volume 3https://doi.org/10.1007/978-1-4842-5485-1_10

10. Apache Web Server

David Both1 
(1)
Raleigh, NC, USA
 

Objectives

In this chapter you will learn
  • How to install and configure the Apache web server

  • How to create simple static web pages

  • How to use multiple programming languages to generate dynamic web content

Introduction

Apache is arguably the most common web server on the Internet. It is well understood, mature, and reliable. It is very configurable and flexible as you will see in this and later experiments. Apache is also free open source software that is provided under the Apache License 2.0.

Apache is an HTTP server that is available on Linux, other Unix-like operating system, and Windows. HTTP stands for HyperText Transfer protocol and is a text-based protocol that uses TCP for its transport layer. HTTPD is the HTTP daemon which runs on the server and responds to requests to serve a web page.

Although the name may seem strange, it is easily explained. Developed by Rob McCool – yes, really – at the National Center for Supercomputing Application, it was the most common HTTP server on the Internet in 1995. Some of the web masters that were using it had created many of their own plug-in extensions and bug fixes because further development had stalled. Some of those people collaborated to add these extensions and fixes to the original code in the form of patches. There were many of these patches so it was quite natural for the web master hackers to call it “a patchy web server.”

You can find documentation, support, security information, mailing lists, downloads and more at The Apache HTTP Server Project.1

In this chapter, we will install Apache and explore its use as a simple web server. In the following chapters, we will explore its use as the basis for more complex tools such as the WordPress content management system (CMS) to produce complex yet easily manageable web sites.

Installing Apache

Apache is very easy to install with a single command.

Experiment 10-1

Perform this experiment as the root user on StudentVM2. Install the Apache web server with the following command.
[root@studentvm2 ~]# dnf -y install httpd

It takes only a few moments to install the HTTPD package and several dependencies.

You may also wish to install the httpd-tools and httpd-manual packages, but they are not necessary for these experiments.

Testing Apache

No initial configuration changes are required. The default configuration works just fine without modification.

Experiment 10-2

Perform this experiment as the root user on StudentVM2.

Start Apache with the following command.
[root@studentvm2 ~]# systemctl start httpd
You should also enable Apache to ensure that it starts on boot.
[root@studentvm2 ~]# systemctl enable httpd
Verify that the Apache web server is running with the following command.
[root@studentvm2 ~]# systemctl status httpd
• httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2019-07-24 15:52:42 EDT; 17s ago
     Docs: man:httpd.service(8)
 Main PID: 7147 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 4696)
   Memory: 15.2M
   CGroup: /system.slice/httpd.service
           ├─7147 /usr/sbin/httpd -DFOREGROUND
           ├─7148 /usr/sbin/httpd -DFOREGROUND
           ├─7149 /usr/sbin/httpd -DFOREGROUND
           ├─7150 /usr/sbin/httpd -DFOREGROUND
           └─7151 /usr/sbin/httpd -DFOREGROUND
Jul 24 15:52:42 studentvm2.example.com systemd[1]: Starting The Apache HTTP Server...
Jul 24 15:52:42 studentvm2.example.com httpd[7147]: Server configured, listening on: port 80
Jul 24 15:52:42 studentvm2.example.com systemd[1]: Started The Apache HTTP Server.
You can test Apache by starting a Web Browser and type localhost in the URL field. Because no index.html or other index file is located in /var/www/html, the test page shown in Figure 10-1 is displayed.
../images/473483_1_En_10_Chapter/473483_1_En_10_Fig1_HTML.jpg
Figure 10-1

The Fedora Test Page displayed in the browser shows that our Apache web server is working properly

You can also install text only browsers to use for testing from the CLI. The ones I use are links and lynx.

It is necessary to add a line to the iptables firewall in order to allow inbound traffic on port 80. Add the following line on the Filter table INPUT section of /etc/sysconfig/iptables.
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
Load the new rules.
[root@studentvm2 ~]# cd /etc/sysconfig/ ; iptables-restore iptables

To test that external hosts can access our new web site, open a browser on StudentVM1 and enter http://studentvm2.example.com/ in the URL field. The test page should be displayed.

That was easy.

Creating a simple index file

Our web server is up and running, so now we need some content. An index file is the “home page” for any web site. There are different types of index files and we will explore a few in this section.

It is easy to create a very simple index file for your web site. This can act as the starting point for a more complex site, or it can just be a placeholder until a more complex site can be built using tools such as Drupal or WordPress. We will use WordPress in Chapter 11 of this volume to create a professional-looking web site. For now, we will look at creating some simple static and dynamic web pages.

Experiment 10-3

Perform this experiment as the root user on StudentVM2. In this experiment, we will create a simple index file and then embellish it just a bit.

First make /var/www the PWD. Then change the ownership to apache.apache.
[root@studentvm2 www]# chown -R apache.apache *

Make /var/www/html the PWD. Create the index.html file and add the content “Hello World” to it – without the quotes. Ensure that the ownership of the new file is apache.apache and change it if it is not.

On either StudentVM1 or StudentVM2 – or both – refresh the web browser. The result of using an index file is shown in Figure 10-2.
../images/473483_1_En_10_Chapter/473483_1_En_10_Fig2_HTML.jpg
Figure 10-2

Using a simple index file for our web site

Note that our index.html file has no HTML (HyperText Markup Language) in it at all. It is just ASCII plain text. Because the formatting performed by the browser is based upon the HTML markup, a long document would be run together with no regard for paragraphs or spacing of any kind. So let’s add a bit of HTML to pretty it up.

Edit the index.html file and add HTML tags so that it looks like the following. The <h1> tag starts a first level header and </h1> tag ends it. Many tags start a format such as headers, bold (a.k.a strong), italic, and more, and they end with a tag.
<h1>Hello World!</h1>
Save the file and refresh the browser. You should see a result identical to Figure 10-3.
../images/473483_1_En_10_Chapter/473483_1_En_10_Fig3_HTML.jpg
Figure 10-3

The result of using some minimal HTML tags

Some browsers will not know how to handle this minimal HTML, so we would need more to make a complete and universally compatible web page. Edit your index.html file to have the following content.
<!DOCTYPE HTML PUBLIC "-//w3c//DD HTML 4.0//EN">
<html>
<head>
<title>Student Web Page</title>
</head>
<body>
<h1>Hello World!</h1>
<hr>
Welcome to my world.<p>
Student
</body>
</html>

This is pretty much the minimum requirements for an HTML document that conforms to W3C HTML standards.

The first line defines this as an HTML document conforming to the W3C2 standards for HTML 4.0. The second line defines the start of the HTML document. The <head> tag defines the heading section of the document which contains only a <title> tag. The title is what appears in the browser title bar and tab for this web site. The <hr> tag generates a horizontal rule – bar – to use as a separator. The <p> tag defines the start of a paragraph. It can be used without the corresponding </p> tag as I have here in this simple web document, but it is better if you do use it to mark the end of each paragraph.

You can create this document without the enclosing <html>, <body>, and <head> tags, but it will work best with all browsers if you use all of those tags.

Now refresh the browser and see that your page looks like that in Figure 10-4.
../images/473483_1_En_10_Chapter/473483_1_En_10_Fig4_HTML.jpg
Figure 10-4

We have now generated a complete static web page

As you can see, creating a simple web page is quite easy.

Adding DNS

Most web sites are accessed using the form “www.domain.com”, so let’s do that for our domain.

Experiment 10-4

Perform this experiment as the root user on StudentVM2. Add the following entry to your DNS zone file for www.example.com.
www             IN      A       192.168.56.1

Restart the named service.

Type www.example.com in the URL field of your browser and press Enter to verify that this new DNS entry for our web server works.

Using Telnet to test the web site

Another way to test your Apache web server is with Telnet. Telnet is a terribly insecure tool to use for a remote terminal session but is a great way to test many services. The only reason this is so is that these services, such as SMTP, as you saw in an earlier lab project, HTTPD, and many more, use plain text data protocols which are easy to read and interact with directly.

Experiment 10-5

Perform this experiment as the student user on StudentVM1.
[student@studentvm1 ~]$ telnet www.example.com 80
Trying 192.168.56.1...
Connected to www.example.com.
Escape character is '^]'.
GET /index.html HTTP/1.1<Enter>
Host: www.example.com<Enter>
<Enter>
HTTP/1.1 200 OK
Date: Thu, 25 Jul 2019 14:22:21 GMT
Server: Apache/2.4.39 (Fedora)
Last-Modified: Thu, 25 Jul 2019 13:41:13 GMT
ETag: "b9-58e819452ee58"
Accept-Ranges: bytes
Content-Length: 185
Content-Type: text/html; charset=UTF-8
<!DOCTYPE HTML PUBLIC "-//w3c//DD HTML 4.0//EN">
<html>
<head>
<title>Student Web Page</title>
</head>
<body>
<h1>Hello World!</h1>
<hr>
Welcome to my world.<p>
Student
</body>
</html>
Connection closed by foreign host.
[student@studentvm1 ~]$

The data sent as a result of your GET request should be the exact contents of the index.html file. It is the function of the web browser to interpret the HTML data protocols and generate a nicely formatted web page.

Good practice configuration

There is one bit of configuration that is always good practice to follow. By default, the “Listen” directive tells Apache to listen for incoming HTTP requests on port 80. In the event your host is multi-homed, that is, it has more than one active NIC or multiple IP addresses bound to a single NIC, Apache would bind to all IP addresses by default. This is probably not the desired behavior. It is good practice to use the Listen directive to specify the IP address on which Apache should listen.

For our server, we will limit access to the internal network. In a real-world environment, we would allow access to the outside world via the Internet as well.

Experiment 10-6

Perform this experiment as the root user on StudentVM2.

Edit the /etc/httpd/conf/httpd.conf file and change the “Listen” line to the following.
Listen 192.168.56.1:80

Restart httpd and refresh the browser on StudentVM1 to test your web site.

Virtual hosts

Apache provides the capability to host multiple web sites on a single Linux host by using the Name Virtual Hosts feature of Apache. I host multiple web sites on my own personal web server and it is easy.

Remember in Experiment 10-5 where we used Telnet to test the server? After the GET statement, we also entered the name of the host from which we were requesting the web page. This is part of the HTTP protocol and it can be used to differentiate between different virtual servers hosted on a single Linux computer. It means that we can set up our second web site using a different virtual host name and use that name to request web pages.

The experiments in this section guide you through the process of creating a second web site.

Configuring the primary virtual host

Before adding a second web site, we need to convert the existing one to a Name Virtual Host and test it.

Experiment 10-7

Perform this experiment as the root user on StudentVM2.

Start by commenting out all stanzas in /etc/httpd/conf/httpd.conf that contain a reference to /var/www. All of these stanzas will be recreated in the name virtual host stanzas. This includes the single DocumentRoot statement at about line number 119.

Change the name of the directory that contains the primary web site to positively identify it, from /var/www to /var/www1. Then create the following virtual host stanza at the end of the current httpd.conf file. Be sure to comment out all lines in the <Directory “/var/www”> stanza at about line 124 and the <Directory “/var/www/html”> stanza that starts at about line 131. It is not necessary to comment out the lines that are already comments.
############################################################################
# Configure for name based virtual hosting. The individual web
# site stanzas are located below.
############################################################################
# The primary website
<VirtualHost 192.168.56.1:80>
    ServerName www1.example.com
    ServerAlias www1.example.com
    DocumentRoot "/var/www1/html"
    ErrorLog "logs/error_log"
    ServerAdmin [email protected]
    <Directory "/var/www1/html">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>
Change the DNS record for www to www1.
www1             IN      A       192.168.56.1
Reload the configuration files for HTTPD and named, and verify that there are no errors.
[root@studentvm2 html]# systemctl reload httpd ; systemctl reload named
[root@studentvm2 named]# systemctl status httpd ; systemctl status named
Test the web site with your browser. Now edit the /var/www1/html/index.html file and make the changes indicated in bold.
<!DOCTYPE HTML PUBLIC "-//w3c//DD HTML 4.0//EN">
<html>
<head>
<title>Primary Web Page</title>
</head>
<body>
<h1>Hello World!</h1>
<hr>
Welcome to my world.<p>
Primary web site
</body>
</html>

It is not necessary to restart the HTTPD server service when web page content is changed. Test the web site by refreshing the browser, and the changed line should now be displayed.

Configuring the second virtual host

Adding the second virtual host is easy. We will copy the data for the new host from the existing one and then make any necessary changes.

Experiment 10-8

Perform this experiment as the root user on StudentVM2.

First change the PWD to /var. Copy the original web site data to a new directory to form the basis of the second web site. The -r option copies the directory structure and data recursively. The -p option preserves ownership and permissions.
[root@studentvm2 var]# cp -rp www1/ www2
Verify the ownership of the copied files and directories is apache.apache. Edit the file /var/www2/http/index.html as shown below to differentiate it from the original web site.
<!DOCTYPE HTML PUBLIC "-//w3c//DD HTML 4.0//EN">
<html>
<head>
<title>Second Web Page</title>
</head>
<body>
<h1>Hello World!</h1>
<hr>
Welcome to my world.<p>
This is the second web site
</body>
</html>
Add a new DNS entry for this second web site. We could also do this with a CNAME record instead of an A record.
www1                    IN      A       192.168.56.1
www2                    IN      A       192.168.56.1

Reload the configuration for named.

Create a new Name Virtual Host stanza below the first one. Change the data to look like that below for the second web site. Because the data is so similar, you can copy the stanza for the first and make any required alterations.
# The secondary website
<VirtualHost 192.168.56.1:80>
    ServerName www2.example.com
    ServerAlias www2example.com
    DocumentRoot "/var/www2/html"
    ErrorLog "logs/error_log"
    ServerAdmin [email protected]
    <Directory "/var/www2/html">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

Test both web sites to be sure that they are both working correctly.

Using CGI scripts

CGI scripts allows creation simple or complex interactive programs that can be run to provide a dynamic web page. A dynamic web page can change based on input, calculations, current conditions in the server, and so on.

CGI stands for Common Gateway Interface.3 CGI is a protocol specification that defines how a web server should pass a request to an application program and then receive the data from the program so that it may be passed back to the requesting web browser. There are many languages that can be used for CGI scripts. The language you choose for any project should be based upon the needs of the project. We will look at two languages, Perl and BASH. Other popular CGI languages are PHP and Python.

Using Perl

Perl is a very popular language for CGI scripts. Its primary strength is that it is a very powerful language for the manipulation of text. It also does maths better than Bash.

Experiment 10-9

Perform this experiment as the root user on StudentVM2.

We need to add some lines to the first Virtual Host stanza in httpd.conf. We need to define the ScriptAlias which specifies the location for CGI scripts. We also need to provide access for all to that directory, just like the access we specified for the html directory.
ScriptAlias /cgi-bin/ "/var/www1/cgi-bin/"
The Name Virtual Host stanza for the primary web site should now look like this. The new lines are highlighted in bold.
# The primary website
<VirtualHost 192.168.56.1:80>
    ServerName www1.example.com
    ServerAlias www1.example.com
    DocumentRoot "/var/www1/html"
    ScriptAlias /cgi-bin/ "/var/www1/cgi-bin/"
    ErrorLog "logs/error_log"
    ServerAdmin [email protected]
    <Directory "/var/www1/html">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    <Directory "/var/www1/cgi-bin">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>
Add the following Perl script to /var/www1/cgi-bin/index.cgi. Set the ownership to apache.apache and permissions to 755 because it must be executable.
#!/usr/bin/perl
print "Content-type: text/html ";
print "<html><body> ";
print "<h1>Hello World</h1> ";
print "Using Perl<p> ";
print "</body></html> ";
Run this program from the CLI and view the results.
[root@studentvm2 cgi-bin]# ./index.cgi
Content-type: text/html
<html><body>
<h1>Hello World</h1>
Using Perl<p>
</body></html>
[root@studentvm2 cgi-bin]#
This is correct because we want the execution of this program to send the HTML code to the requesting browser. On StudentyVM1, view the URL www1.example.com/cgi-bin/index.cgi in your browser. Your result should be identical to that shown in Figure 10-5.
../images/473483_1_En_10_Chapter/473483_1_En_10_Fig5_HTML.jpg
Figure 10-5

Using a Perl CGI script to produce a web page

The above CGI program is still basically static because it always displays the same output. Add the following lines to your CGI program immediately after the “Hello World” line. The Perl “system” command executes the commands following it in a system shell, and returns the result to the program. In this case, we simply grep the current RAM usage out of the results from the free command.
system "free | grep Mem";
print " ";

Now refresh the browser and view the results. You should see an additional line that displays the system memory statistics. Refresh the browser a couple more times and notice that the memory usage should change occasionally.

Using BASH

BASH is probably the simplest language of all for use in CGI scripts. Its primary strengths for CGI programming are that all SysAdmins should know it and it has direct access to all of the standard GNU utilities and system programs.

Experiment 10-10

Perform this experiment as the root user on StudentVM2.

Copy the existing index.cgi to Perl.index.cgi. Replace the content of the index.cgi with the following content.
#!/bin/bash
echo "Content-type: text/html"
echo ""
echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>Hello World</title>'
echo '</head>'
echo '<body>'
echo '<h1>Hello World</h1><p>'
echo 'Using BASH<p>'
free | grep Mem
echo '</body>'
echo '</html>'
exit 0

Test this code by running it from the command line. It will produce HTML output. Refresh the browser on StudentVM1.

Redirecting the web page to CGI

All this CGI is very nice, but people don’t usually type the full URL to your CGI page. They will type the domain name and hit the Enter key. We need to add one more line to the httpd.conf file in the VirtualHost stanza for the primary web site.

Experiment 10-11

Perform this experiment as the root user on StudentVM2.

Add the highlighted line to the primary web site Named Virtual Host stanza. The entire VirtualHost stanza now looks like this. The DirectoryIndex statement defines the possible names and locations of the index files.
# The primary website
<VirtualHost 192.168.56.1:80>
    ServerName www1.example.com
    ServerAlias www1.example.com
    DocumentRoot "/var/www1/html"
    DirectoryIndex index.html index.txt /cgi-bin/index.cgi
    ScriptAlias /cgi-bin/ "/var/www1/cgi-bin/"
    ErrorLog "logs/error_log"
    ServerAdmin [email protected]
    <Directory "/var/www1/html">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    <Directory "/var/www1/cgi-bin">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

Rename the /var/www1/html/index.html file to Old.index.html so that it will no longer match the DirectoryIndex definition of an index file. Note that the search sequence in the DirectoryIndex statement is left to right, so rearranging that sequence so that the/cgi-bin/index.cgi is first would work also. However, there may be side effects of doing it that way that should be considered before doing so in a production environment.

Now type www.example.com in the URL line of your browser. The result should take you to the CGI script which will display the current memory usage.

Refreshing the page automatically

Now that we have a page that gives us memory statistics, we do not want to manually refresh the page. We can do that with a statement in our CGI script.

Experiment 10-12

Perform this experiment as the root user on StudentVM2.

Replace the existing “meta” line with the following one which points to the index.cgi file and contains a refresh instruction. The content=1 statement specifies a one second refresh interval.
echo '<meta http-equiv="Refresh" content=1;URL=http://www1.example.com/cgi-bin/index.cgi>'

Change the refresh rate to 5 seconds. Note that this change takes effect immediately.

Chapter summary

In this chapter, we created a simple static web page with minimal content and no HTML formatting. From there, we used HTML to create progressively more complex static content. We also created a second web site hosted on the same VM. After a bit of testing with static content, we moved on to creating dynamic pages with Bash and Perl CGI scripts.

This is a very simple example of serving up two web sites with a single instance of the Apache httpd server. Configuration of the virtual hosts becomes a bit more complex when other factors are considered.

For example, you may have some CGI scripts you want to use for one or both of these web sites. You would create directories for the CGI programs in /var/www. One might be /var/www/cgi-bin and the other might be /var/www/cgi-bin2 to be consistent with the html directory naming. It would then be necessary to add configuration directives to the virtual host stanzas in order to specify the directory location for the CGI scripts. Each web site could also have directories from which files could be downloaded and that would also require entries in the appropriate virtual host stanza.

The Apache web site has some very good documentation at https://httpd.apache.org/docs/2.4/ that describes some other methods for managing multiple web sites as well as configuration options ranging from performance tuning to security.

Exercises

Perform the following exercises to complete this chapter.
  1. 1.

    Describe the difference between a static web page and a dynamic one.

     
  2. 2.

    List at least five popular programming languages that are used to generate dynamic web pages.

     
  3. 3.

    What limitations might prevent a program language from being used with CGI?

     
  4. 4.

    What does CGI enable web sites to do?

     
  5. 5.

    Add some code to the CGI script that will display the current CPU usage on the web page in addition to the memory usage.

     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.119.206