loading...

Ubuntu Server 18.04 – Installing and configuring Apache

Installing MySQL On CentOS 8

The best way to become familiar with any technology is to dive right in. We’ll begin this chapter by installing Apache, which is simply a matter of installing the apache2 package:

sudo apt install apache2 

By default, Ubuntu will immediately start and enable the apache2 daemon as soon as its package is installed. You can confirm this yourself with the following command:

systemctl status apache2 

In fact, at this point, you already have (for all intents and purposes) a fully functional web server. If you were to open a web browser and enter the IP address of the server you just installed Apache on, you should see Apache’s sample web page:

The default sample web page provided by Apache

There you go, you have officially served web content. All you needed to do was install the apache2 package, and your server was transformed into a web server. Chapter over, time to move on.

Of course, there’s more to Apache than simply installing it and having it present a sample web page. While you could certainly replace the content in the sample web page with your own and be all set as far as hosting content to your users, there’s much more to understand. For instance, there are several configuration files in the /etc/apache2 directory that govern how sites are hosted, as well as which directories Apache will look in to find web pages to host. Apache also features plugins, which we will go over as well.

The directory that Apache serves web pages from is known as a Document Root, with /var/www/html being the default. Inside that directory, you’ll see an index.html file, which is actually the default page you see when you visit an unmodified Apache server. Essentially, this is a test page that was designed to show you that the server is working, as well as some tidbits of information regarding the default configuration.

You’re not limited to hosting just one website on a server, though. Apache supports the concept of a Virtual Host, which allows you to serve multiple websites from a single server. Each virtual host would consist of an individual configuration file, which differentiate themselves based on either name or IP address. For example, you could have an Apache server with a single IP address that hosts two different websites, such as acmeconsulting.com and acmesales.com. These are hypothetical websites, but you get the idea. To set this up, you would create a separate configuration file for acmeconsulting.com and acmesales.com and store them in your Apache configuration directory. Each configuration file would include a <VirtualHost> stanza, where you would place an identifier such as a name or IP address that differentiates one from the other. When a request comes in, Apache will serve either acmeconsulting.com or acmesales.com to the user’s browser, depending on which criteria matched when the request came in. The configuration files for each site typically end with the .conf filename extension, and are stored in the /etc/apache2/sites-available directory. We’ll go over all of this in more detail shortly.

The basic workflow for setting up a new site (virtual host) will typically be similar to the following:

  • The web developer creates the website and related files
  • These files are uploaded to the Ubuntu Server, typically in a subdirectory of /var/www or another directory the administrator has chosen
  • The server administrator creates a configuration file for the site, and copies it into the /etc/apache2/sites-available directory
  • The administrator enables the site and reloads Apache

Enabling virtual hosts is handled a bit differently in Debian and Ubuntu than in other platforms. In fact, there are two specific commands to handle this purpose: a2ensite for enabling a site and a2dissite for disabling a site. You won’t find these commands on distributions such as CentOS, for example. Configuration files for each site are stored in the /etc/apache2/sites-available/ directory and we would use the a2ensite command to enable each configuration. Assuming a site with the URL of acmeconsulting.com is to be hosted on our Ubuntu Server, we would create the /etc/apache2/sites-available/acmeconsulting.com.conf configuration file, and enable the site with the following commands:

sudo a2ensite acmeconsulting.com.conf 
sudo systemctl reload apache2 
I’m not using absolute paths in my examples; as long as you’ve copied the configuration file to the correct place, the a2ensite and a2dissite commands will know where to find it.

If we wanted to disable the site for some reason, we would execute the a2dissite command against the site’s configuration file:

sudo a2dissite acmeconsulting.com.conf 
sudo systemctl reload apache2 

If you’re curious how this works behind the scenes, when the a2ensite command is run against a configuration file, it basically creates a symbolic link to that file and stores it in the /etc/apache2/sites-enabled directory. When you run a2dissite to disable a site, this symbolic link is removed. Apache, by default, will use any configuration files it finds in the /etc/apache2/sites-enabled directory. After enabling or disabling a site, you’ll need to refresh Apache’s configuration, which is where the reload option comes in. This command won’t restart Apache itself (so users who are using your existing sites won’t be disturbed) but it does give Apache a chance to reload its configuration files. If you replaced reload with restart on the preceding commands, Apache will perform a full restart. You should only need to do that if you’re having an issue with Apache or enabling a new plugin, but in most cases the reload option is preferred on a production system.

The main configuration file for Apache is located at /etc/apache2/apache2.conf. Feel free to view the contents of this file; the comments contain a good overview of how Apache’s configuration is laid out. The following lines in this file are of special interest:

# Include the virtual host configurations: 
IncludeOptional sites-enabled/*.conf 

As you can see, this is how Ubuntu has configured Apache to look for enabled sites in the /etc/apache2/sites-enabled directory. Any file stored there with the .conf file extension is read by Apache. If you wish, you could actually remove those lines and Apache would then behave as it does on other platforms, and the a2ensite and a2dissite commands would no longer have any purpose. However, it’s best to keep the framework of Ubuntu’s implementation intact, as separating the configuration files makes logical sense, and helps simplify the configuration. This chapter will go along with the Ubuntu way of managing configuration.

An additional virtual host is not required if you’re only hosting a single site. The contents of /var/www/html are served by the default virtual host if you make no changes to Apache’s configuration. This is where the example site that ships with Apache comes from. If you only need to host one site, you could remove the default index.html file stored in this directory and replace it with the files required by your website. If you wish to test this for yourself, you can make a backup copy of the default index.html file and create a new one with some standard HTML. You should see the default page change to feature the content you just added to the file.

The 000-default.conf file is special, in that it’s basically the configuration file that controls the default Apache sample website. If you look at the contents of the /etc/apache2/sites-available and the /etc/apache2/sites-enabled directories, you’ll see the 000-default.conf configuration file stored in sites-available, and symlinked in sites-enabled. This shows you that, by default, this site was included with Apache, and its configuration file was enabled as soon as Apache was installed. For all intents and purposes, the 000-default.conf configuration file is all you need if you only plan on hosting a single website on your server. The contents of this file are as follows, but I’ve stripped the comments out of the file in order to save space on this page:

<VirtualHost *:80> 
    ServerAdmin webmaster@localhost 
    DocumentRoot /var/www/html 
 
    ErrorLog ${APACHE_LOG_DIR}/error.log 
    CustomLog ${APACHE_LOG_DIR}/access.log combined 
</VirtualHost> 

As you can see, this default virtual host is telling Apache to listen on port 80 for requests and to serve content from /var/www/html as soon as requests come in. The <VirtualHost> declaration at the beginning is listening to everything (the asterisk is a wildcard) on port 80, so this is basically handling all web traffic that comes in to the server from port 80. The ServerAdmin clause specifies the email address that is displayed in any error messages shown in case there is a problem with the site.

The DocumentRoot setting tells Apache which directory to look for in order to find files to serve for connections to this virtual host. /var/www/html is the default, but some administrators choose to customize this. This file also contains lines for where to send logging information. The access log contains information relating to HTTP requests that come in, and by default is stored in /var/log/access.log. The error log is stored at /var/log/error.log and contains information you can use whenever someone has trouble visiting your site. The ${APACHE_LOG_DIR} variable equates to /var/log by default, and this is set in the /etc/apache2/envvars file, in case for some reason you wish to change this.

If you wish to host another site on the same server by creating an additional virtual host, you can use the same framework as the original file, with some additional customizations. Here’s an example for a hypothetical website, acmeconsulting.com:

<VirtualHost 192.168.1.104:80> 
    ServerAdmin webmaster@localhost 
    DocumentRoot /var/www/acmeconsulting 
 
    ErrorLog ${APACHE_LOG_DIR}/acmeconsulting.com-error.log 
    CustomLog ${APACHE_LOG_DIR}/acmeconsulting.com-access.log combined 
</VirtualHost> 

I’ve emphasized some important differences in this file. First, with this virtual host, I’m not listening for all connections coming in on port 80, instead I’m specifically looking for incoming traffic going to IP address 192.168.1.104 on port 80. This works because this server has two network cards, and therefore two IP addresses. With virtual hosts, I’m able to serve a different website, depending on which IP address the request is coming in on.

Next, I set the DocumentRoot to /var/www/acmeconsulting. Each virtual host should have their own individual DocumentRoot to keep each site separate from one another. On my servers, I will typically disable or remove the sample virtual host (the one that has the default DocumentRoot of /var/www/html). Instead, I use /var/www as a base directory, and each virtual host gets their own directory as a subdirectory of this base.

Another change I find useful is to give each virtual host its own log files. Normally, Apache will use /var/log/apache2/error.log and /var/log/apache2/access.log to store log entries for all sites. If you only have a single site on your server, that is fine. However, when you’re serving multiple sites, I find it useful to give each site their own independent log files. That way, if you are having trouble with a particular site, you don’t have to scroll through unrelated log entries to find what you’re looking for when you’re troubleshooting. In my example, I inserted the website name in the log file names, so this virtual host is logging errors in the /var/log/apache2/acmeconsulting.com-error.log, and the access log is being written to /var/log/apache2/acmeconsulting.com-access.log. These log files will be created for you automatically as soon as you reload Apache.

With a server that only has a single IP address, you can still set up multiple virtual hosts. Instead of differentiating virtual hosts by IP, you can instead differentiate them by name. This is common on Virtual Private Server (VPS) installations of Ubuntu, where you’ll typically have a single IP address assigned to you by your VPS provider. For name-based virtual hosts, we would use the ServerName option in our configuration. Refer to the following example to see how this would work. With this example, I’m adding name-based virtual hosts to their own file. I called mine 000-virtual-hosts.conf and stored it in the /etc/apache2/sites-available directory. The contents are as follows:

<VirtualHost *:80> 
    ServerName acmeconsulting.com 
    DocumentRoot /var/www/acmeconsulting 
</VirtualHost> 
 
<VirtualHost *:80> 
    ServerName acmesales.com 
    DocumentRoot /var/www/acmesales 
</VirtualHost> 

For each virtual host, I’m declaring a ServerName with a matching DocumentRoot. With the first example, any traffic coming into the server requesting acmeconsulting.com will be provided a Document Root of /var/www/acmeconsulting. The second looks for traffic from acmesales.com and directs it to /var/www/acmesales. You can list as many virtual hosts here as you’d like to host on your server. Providing your server has enough resources to handle traffic to each site, you can host as many as you need.

As we continue through this chapter, we’ll perform some additional configuration for Apache. At this point though, you should have an understanding of the basics of how Apache is configured in Ubuntu Server. For extra practice, feel free to create additional virtual hosts and serve different pages for them. The easiest way to test virtual hosts is with a virtual machine. Software such as VirtualBox allows you to set up virtual machines with multiple network interfaces. If you set each one to be a bridged connection, they should get an IP address from your local DHCP server, and you can then create virtual hosts to serve different websites depending on which interface the request comes in on.

Comments are closed.

loading...