Objectives
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
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.
![../images/473483_1_En_10_Chapter/473483_1_En_10_Fig1_HTML.jpg](http://images-20200215.ebookreading.net/3/5/5/9781484254851/9781484254851__using-and-administering__9781484254851__images__473483_1_En_10_Chapter__473483_1_En_10_Fig1_HTML.jpg)
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.
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.
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.
![../images/473483_1_En_10_Chapter/473483_1_En_10_Fig2_HTML.jpg](http://images-20200215.ebookreading.net/3/5/5/9781484254851/9781484254851__using-and-administering__9781484254851__images__473483_1_En_10_Chapter__473483_1_En_10_Fig2_HTML.jpg)
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.
![../images/473483_1_En_10_Chapter/473483_1_En_10_Fig3_HTML.jpg](http://images-20200215.ebookreading.net/3/5/5/9781484254851/9781484254851__using-and-administering__9781484254851__images__473483_1_En_10_Chapter__473483_1_En_10_Fig3_HTML.jpg)
The result of using some minimal HTML tags
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.
![../images/473483_1_En_10_Chapter/473483_1_En_10_Fig4_HTML.jpg](http://images-20200215.ebookreading.net/3/5/5/9781484254851/9781484254851__using-and-administering__9781484254851__images__473483_1_En_10_Chapter__473483_1_En_10_Fig4_HTML.jpg)
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
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
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.
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.
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.
Reload the configuration for named.
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.
![../images/473483_1_En_10_Chapter/473483_1_En_10_Fig5_HTML.jpg](http://images-20200215.ebookreading.net/3/5/5/9781484254851/9781484254851__using-and-administering__9781484254851__images__473483_1_En_10_Chapter__473483_1_En_10_Fig5_HTML.jpg)
Using a Perl CGI script to produce a web page
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.
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.
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.
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
- 1.
Describe the difference between a static web page and a dynamic one.
- 2.
List at least five popular programming languages that are used to generate dynamic web pages.
- 3.
What limitations might prevent a program language from being used with CGI?
- 4.
What does CGI enable web sites to do?
- 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.