What is Apache?

It is modular, functionality can easily be added to the core application.

Different versions

 • 1.3 - Introduced 1998, discontinued 2010

 • 2.0 - Introduced 2000, discontinued 2013

 • 2.2 - Introduced 2005, still being maintained

 • 2.4 - Introduced 2009, the current major version

For this course, we use a VirtualBox VM provided with the exercise files (which I will refer to as the Ubuntu VM/Machine etc), an old CentOS VBox for comparison, in some cases (which I will refer to as the CentOS VM/Machine etc) and my own Raspberry Pi Web Server (which I will refer to as the Raspberry Pi). The login details for the VM are

	Username:	user
	Password: 	lynda.com

The CentOS VBox I am using is the same one I used for the web services course so it should have Apache installed. As a reminder, the login details for this machine are

	Username:	philip
	Password: 	$alvationD0wnt0n

Note, it is possible to access the Ubuntu VBox via ssh using the hostname user@localhost and port number 2222.

How Is Apache Installed

It is important to know what type of distro you are using, particularly whether you are using a Debian based distro (such as Ubuntu or Raspbian on which apache is referred to as apache2) or a RedHat based distro (such as CentOS on which apache is referred to as httpd). This will also dictate which package manager was used to install apache, if it was installed in that way.

We can get info on the distro with either

	cat/etc/issue

or

	cat /etc/*release

Let's see what happens when we run the first of these on Ubuntu.

	user@apache:~$ cat /etc/issue
	Ubuntu 14.04 LTS \n \l

On a CentOS machine, the same command gives us

	[philip@localhost ~]$ cat /etc/issue
	\S
	Kernel \r on an \m

On a Raspberry Pi, you will most likely be running Raspbian which is Debian-based (as is Ubuntu) so we would expect similar results to those that we saw with Ubuntu.

	philip@raspberrypi:/etc/apache2/sites-available $ cat /etc/issue
	Raspbian GNU/Linux 10 \n \l

Now we will try the second command, again on the same three platforms.

Ubuntu

	user@apache:~$ cat /etc/*release
	DISTRIB_ID=Ubuntu
	DISTRIB_RELEASE=14.04
	DISTRIB_CODENAME=trusty
	DISTRIB_DESCRIPTION="Ubuntu 14.04 LTS"
	NAME="Ubuntu"
	VERSION="14.04, Trusty Tahr"
	ID=ubuntu
	ID_LIKE=debian
	PRETTY_NAME="Ubuntu 14.04 LTS"
	VERSION_ID="14.04"
	HOME_URL="http://www.ubuntu.com/"
	SUPPORT_URL="http://help.ubuntu.com/"
	BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

CentOS

	[philip@localhost ~]$ cat /etc/*release
	CentOS Linux release 7.8.2003 (Core)
	NAME="CentOS Linux"
	VERSION="7 (Core)"
	ID="centos"
	ID_LIKE="rhel fedora"
	VERSION_ID="7"
	PRETTY_NAME="CentOS Linux 7 (Core)"
	ANSI_COLOR="0;31"
	CPE_NAME="cpe:/o:centos:centos:7"
	HOME_URL="https://www.centos.org/"
	BUG_REPORT_URL="https://bugs.centos.org/"

	CENTOS_MANTISBT_PROJECT="CentOS-7"
	CENTOS_MANTISBT_PROJECT_VERSION="7"
	REDHAT_SUPPORT_PRODUCT="centos"
	REDHAT_SUPPORT_PRODUCT_VERSION="7"

	CentOS Linux release 7.8.2003 (Core)
	CentOS Linux release 7.8.2003 (Core)

And the Raspberry Pi

	philip@raspberrypi:/etc/apache2/sites-available $ cat /etc/*release
	PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
	NAME="Raspbian GNU/Linux"
	VERSION_ID="10"
	VERSION="10 (buster)"
	VERSION_CODENAME=buster
	ID=raspbian
	ID_LIKE=debian
	HOME_URL="http://www.raspbian.org/"
	SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
	BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

The results are pretty standard for all three here. One interesting point is ID_LIKE which shows the type of distro.

On either type of system, there are several ways to install Apache. The one we will cover in most detail is probably the most common and that is installation via a packet manager. So this would be

	sudo apt install

on a Debian system or

	sudo yum install

on a RedHat system.

To check if Apache was installed in this way, we can query the package manager library so

	user@apache:~$ dpkg -l | grep apache
	ii  apache2                         2.4.7-1ubuntu4                i386         Apache HTTP Server
	ii  apache2-bin                     2.4.7-1ubuntu4                i386         Apache HTTP Server (binary files and modules)
	ii  apache2-data                    2.4.7-1ubuntu4                all          Apache HTTP Server (common files)
	ii  apache2-utils                   2.4.7-1ubuntu4                i386         Apache HTTP Server (utility programs for web servers)

or

	[philip@localhost ~]$ rpm -qa | grep httpd
	httpd-tools-2.4.6-93.el7.centos.x86_64
	httpd-2.4.6-93.el7.centos.x86_64

Installing Apache from source can be a more difficult option but depending on requirements (eg, a unique platform or the need to address specific security issues) it might be necessary.

The best way to tell if that is how Apache was installed is to look for the source code which would often be located in the /usr/local/src directory or the users home directory. For example, we can check the /usr/local/src directory with

	ls -la /usr/local/src

and similarly

	ls /home/user

to check the home directory of a user called user. We could also use find or locate to look for the source files. This assumes that we know the name of the source code but it is easy enough (see the Apache download page here which, at the very least, would provide some ideas for search terms.

The source code would likely have httpd in the name (possibly apache) so we could use locate with either term and see if anything in the results looks like source code (would likely have something like *.tar.bz2 at the end).

A command such as

	sudo find / -name *.tar.*

may be useful for this purpose. I have executed this with elevated privileges because I won’t have the appropriate permissions for many of the directories I am searching in. Note that the parameters for the find command are

 • the location to search in, in this case / so search everywhere

 • the type of search - in this case -name because we are searching based on the name of the file

 • the search term, in this case *.tar.* which should locate any file that includes a .tar extension with an additional extension so this should locate most source files

Common Operations such as Restarting and Reloading

Note that Apache is not a monolithic service, it is a parent service with multiple child service, each of which could be serving up different web services.

Apache provides a script, apachectl, used for management purposes. Some distros also provide an additional layer of abstraction on top of that

The following figures provide some details relating to the available actions.

The status and start actions

Apache Actions - status and start

The stop and graceful-stop actions

Apache Actions - stop and graceful-stop

The restart action

Apache Actions - restart

The graceful - AKA reload action

Apache Actions - graceful - AKA reload

We will try the status action first. I will not do this on the Raspberry Pi because apachectl is not enabled there.

On Ubuntu, the output we get from running the apachectl status command is

	user@apache:~$ apachectl status
					  Apache Server Status for localhost (via ::1)

	   Server Version: Apache/2.4.7 (Ubuntu)
	   Server MPM: event
	   Server Built: Apr 3 2014 12:20:25
		 __________________________________________________________________

	   Current Time: Wednesday, 30-Dec-2020 06:38:08 PST
	   Restart Time: Tuesday, 29-Dec-2020 15:09:45 PST
	   Parent Server Config. Generation: 2
	   Parent Server MPM Generation: 1
	   Server uptime: 15 hours 28 minutes 22 seconds
	   Server load: 0.00 0.01 0.05
	   Total accesses: 1 - Total Traffic: 3 kB
	   CPU Usage: u0 s.04 cu0 cs0 - 7.18e-5% CPU load
	   1.8e-5 requests/sec - 0 B/second - 3072 B/request
	   1 requests currently being processed, 49 idle workers

	   PID    Connections    Threads      Async connections
			total accepting busy idle writing keep-alive closing
	   1051 0     yes       1    24   0       0          0
	   1052 0     yes       0    25   0       0          0
	   Sum  0               1    49   0       0          0

	_________________W________________________________..............
	................................................................
	......................

	   Scoreboard Key:
	   "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
	   "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
	   "C" Closing connection, "L" Logging, "G" Gracefully finishing,
	   "I" Idle cleanup of worker, "." Open slot with no current process

Running the same command on the CentOS machine, we get

	[philip@localhost ~]$ apachectl status
	* httpd.service - The Apache HTTP Server
	   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
	   Active: failed (Result: exit-code) since Wed 2020-12-30 13:31:39 GMT; 1h 7min ago
		 Docs: man:httpd(8)
			   man:apachectl(8)
	  Process: 1228 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
	 Main PID: 1228 (code=exited, status=1/FAILURE)

	Dec 30 13:31:36 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
	Dec 30 13:31:39 localhost.localdomain httpd[1228]: (99)Cannot assign requested addr...0
	Dec 30 13:31:39 localhost.localdomain httpd[1228]: no listening sockets available, ...n
	Dec 30 13:31:39 localhost.localdomain httpd[1228]: AH00015: Unable to open logs
	Dec 30 13:31:39 localhost.localdomain systemd[1]: httpd.service: main process exite...E
	Dec 30 13:31:39 localhost.localdomain systemd[1]: Failed to start The Apache HTTP S....
	Dec 30 13:31:39 localhost.localdomain systemd[1]: Unit httpd.service entered failed....
	Dec 30 13:31:39 localhost.localdomain systemd[1]: httpd.service failed.
	Dec 30 14:16:02 localhost.localdomain systemd[1]: Unit httpd.service cannot be relo....
	Hint: Some lines were ellipsized, use -l to show in full.

You might notice that this shows that the service failed and I wasn't able to start it again so I reinstalled httpd and tried again. This worked and running the command again yielded the following

	[philip@localhost ~]$ apachectl status
	* httpd.service - The Apache HTTP Server
	   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
	   Active: active (running) since Wed 2020-12-30 14:49:01 GMT; 3min 50s ago
		 Docs: man:httpd(8)
			   man:apachectl(8)
	 Main PID: 13054 (httpd)
	   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
		Tasks: 6
	   CGroup: /system.slice/httpd.service
			   |-13054 /usr/sbin/httpd -DFOREGROUND
			   |-13059 /usr/sbin/httpd -DFOREGROUND
			   |-13060 /usr/sbin/httpd -DFOREGROUND
			   |-13061 /usr/sbin/httpd -DFOREGROUND
			   |-13062 /usr/sbin/httpd -DFOREGROUND
			   `-13063 /usr/sbin/httpd -DFOREGROUND

	Dec 30 14:49:01 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
	Dec 30 14:49:01 localhost.localdomain httpd[13054]: AH00558: httpd: Could not relia...e
	Dec 30 14:49:01 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
	Hint: Some lines were ellipsized, use -l to show in full.

Note that the web server on the Ubuntu machine has been set up to server a page containing the text of Alice's Adventures in Wonderland which we can access via the URL, http://localhost:8080/.

Now, let's stop Apache with

	sudo apachectl -k stop

The command executes without providing any feedback. If we check the status again, we will see an error message

	user@apache:~$ apachectl status

	Looking up localhost
	Making HTTP connection to localhost
	Alert!: Unable to connect to remote host.

	lynx: Can't access startfile http://localhost/server-status
	'www-browser -dump http://localhost:80/server-status' failed.
	Maybe you need to install a package providing www-browser or you
	need to adjust the APACHE_LYNX variable in /etc/apache2/envvars

This looks quite different from the output we saw on CentOS when Apache wasn't running. That could be because of the different distros or because on CentOS, Apache had failed rather than being stopped.

This can easily be tested by stopping the service and checking the status and I can see that the output is more or less the same as shown in figure 14. The most significant difference I could see was that when the service was stopped, the status showed that the httpd service was disabled whereas in figure 14, it shows as enabled.

In any case, it does seem likely that the difference in output is probably down to the different distros. It may also be worth noting that the Ubuntu machine is running version 2.4.7 of Apache whereas the CentOS machine is running version 2.4.6.

Not surprisingly, if we go back to the web page served up by the web server on the Ubuntu machine, we will see an error message like the one show here.

The error message we see when trying to access the web page served up by the Ubuntu machine when Apache is stopped

We can start the server up again on the Ubuntu machine with

	sudo apachectl start

Again, we don't see any output from this command, there is nothing to confirm the service has started up but we can check it either with the status action or by reloading the page in the web browser. Either method should confirm that the service is running again.

We can also interact with the apache service via a standardized script provided with most distros. This is stored in the /etc/init.d file and the script is called apache2 on the Ubuntu machine. The same script is on the Raspberry Pi but there doesn't seem to be either the same script or an equivalent on the CentOS machine.

Below, we can see a number of commands where the script is used to interact with Apache on the Ubuntu machine.

	user@apache:~$ /etc/init.d/apache2 stop
	 * Stopping web server apache2                                                                                                                                                                                                                *
	user@apache:~$ /etc/init.d/apache2 start
	 * Starting web server apache2                                                                                                                                                                                                                *
	user@apache:~$ /etc/init.d/apache2 status
	 * apache2 is running
	user@apache:~$ /etc/init.d/apache2 graceful-stop
	 * Stopping web server apache2                                                                                                                                                                                                                *
	user@apache:~$ /etc/init.d/apache2 restart
	 * Restarting web server apache2                                                                                                                                                                                                      [fail]
	user@apache:~$ /etc/init.d/apache2 reload
	 * Reloading web server apache2

Note that every command reports back with some sort of status message, for instance, letting us know that the service is being stopped, started and so on.

We can also use the status action with this script, but the output from this is as terse as the messages we saw earlier so this will tell us whether the service is running or not but nothing else.

Newer versions of Linux also provide an additional level of abstraction which is just service and the service name is either apache2 on the Ubuntu machine and httpd on the CentOS machine.

The usage is similar to the script in /etc/init.d and provides the same output on the Ubuntu machine so

	user@apache:~$ service apache2 status
	 * apache2 is running

On the CentOS machine (recall that there is no script in /etc/init.d), the output is similar to what we see with apachectl so

	[philip@localhost ~]$ service httpd status
	Redirecting to /bin/systemctl status httpd.service
	● httpd.service - The Apache HTTP Server
	   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
	   Active: active (running) since Wed 2020-12-30 18:59:24 GMT; 3min 41s ago
		 Docs: man:httpd(8)
			   man:apachectl(8)
	  Process: 13437 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
	 Main PID: 16075 (httpd)
	   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
		Tasks: 6
	   CGroup: /system.slice/httpd.service
			   ├─16075 /usr/sbin/httpd -DFOREGROUND
			   ├─16079 /usr/sbin/httpd -DFOREGROUND
			   ├─16080 /usr/sbin/httpd -DFOREGROUND
			   ├─16081 /usr/sbin/httpd -DFOREGROUND
			   ├─16082 /usr/sbin/httpd -DFOREGROUND
			   └─16083 /usr/sbin/httpd -DFOREGROUND

	Dec 30 18:59:24 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
	Dec 30 18:59:24 localhost.localdomain httpd[16075]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' direct... this message
	Dec 30 18:59:24 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
	Hint: Some lines were ellipsized, use -l to show in full.