Chapter 7. Hosting HTTP Content via Apache

Apache is the most common web server used on the Internet. While there are other web servers available, such as Microsoft's Internet Information Services (IIS), Apache rules the kingdom when it comes to serving web content. Available in both Linux and UNIX platforms, Apache enables you to host content and share it over your local intranet, as well as the Internet. There are many uses for an Apache server, including (but certainly not limited to) hosting a blog or company website, or setting up an employee portal for your company.

In this chapter, you'll learn all about installing and configuring Apache. We will cover the following topics:

  • Installing Apache
  • Configuring Apache
  • Adding modules
  • Setting up virtual hosts

Installing Apache

As usual, installing Apache on your system is just a matter of installing the proper package from your package manager. On a CentOS system, you can obtain Apache by installing the httpd package, and on Debian systems with the apache2 package (yum install httpd or apt-get install apache2 respectively, as root). Once you install the package, Apache's daemon is now present with a default set of configuration files. You will be able to confirm the existence of the daemon on your system with systemctl, though the name of the daemon is different depending on your distribution.

Use the following command on Debian:

# systemctl status apache2

Use the following command on CentOS:

# systemctl status httpd

By default, Debian starts and enables the daemon for you. As is typical, CentOS makes no assumptions and does neither. You can start and enable the daemon easily with the systemctl command:

# systemctl enable httpd
# systemctl start httpd

Once you install and enable Apache, you technically already have a working web server on your network. It may not be particularly useful (we haven't configured it yet) but at this point it exists, and it is technically working. Both the CentOS and Debian builds of Apache look for web content in the same directory, /var/www/html. There, Debian creates a sample web page in the form of an index.html file, which you can view via a web browser on another computer (just point it to the IP address of your web server). CentOS, on the other hand, does not create a sample HTML page for you. This is easy to rectify; all you should have to do is manually create the /var/www/html/index.html file with some sample code. It doesn't need to be extravagant; we just want to make sure we have something to test with. For example, you could just put the following code in that file:

<html>
  <title>Apache test</title>
  <body>
    <p>Apache is awesome!</p>
  </body>
</html>

At this point, you should have Apache installed and its service started. You should also have an example /var/www/html/index.html file present on your system, whether you are using Debian's default or you manually created it on a CentOS system. Now, you should be able to browse to your web server and view this page via a web browser. If you know the IP address of your webserver, just type that in to the address bar in your web browser. You should see the sample page immediately. If you're using a web browser on your web server, you should be able to browse to the localhost (http://127.0.0.1 or http://localhost) and view the same page.

Note

If you chose CentOS for your web server, the default firewall may get in your way if you are trying to browse to it from another machine. Depending on your configuration, you may need to allow traffic to your web server through the firewall. To do this, execute the following commands:

# firewall-cmd --zone=public --add-port=80/tcp --permanent
# firewall-cmd --reload

Be sure to add port 443 as well, if you plan on hosting a secure site. Just use the same firewall-cmd as before, but replace 80 with 443.

If for some reason you don't see the default page, make sure that Apache is running (remember the systemctl status commands I mentioned earlier). If the daemon isn't running, you'll likely get a connection refused error. Also, keep in mind that hardware-based firewalls can prevent access as well.

Installing Apache

The default web page served from an unconfigured Apache, running on Debian

Another way of testing whether or not your server is serving web pages is via lynx, a text-based web browser you can use within a shell. This may be preferred in some situations, as it doesn't have the overhead of a graphical web browser and is very quick to launch. Once you install the lynx package on your machine, you can navigate to your web server from the server itself by executing lynx http://localhost, or http://<ip address> if you are coming from a different machine.

Installing Apache

Using lynx to test web server functionality

Note

To quit lynx, press Q for quit followed by Y for yes.

As I mentioned, both Debian and CentOS look in the same directory for files to share via Apache. This directory is /var/www/html. In order to create a web site, you would place your site's files into this directory. The typical process of setting up an Apache server is by installing Apache, then testing whether or not it can be reached by other computers on the network, and then finally developing your site and placing its files into this folder.

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

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