facebook youtube pinterest twitter reddit whatsapp instagram

Installing, Understanding and Configuring Apache (Ubuntu)

Apache is an open-source cross-platform web server software by Apache Software Foundation. To install apache, simply install the apache2 package as follows:

sudo apt install apache2

Confirm apache2 is running and enabled:

systemctl status apache2
user@server:/$ systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
Active: active (running) since Wed 2020-06-24 13:48:52 UTC; 1min 12s ago
Main PID: 6213 (apache2)
Tasks: 55 (limit: 2317)
CGroup: /system.slice/apache2.service
├─6213 /usr/sbin/apache2 -k start
├─6216 /usr/sbin/apache2 -k start
└─6217 /usr/sbin/apache2 -k start

As you can see apache2 is running, and also enabled, meaning when you restart your server, it’s gonna boot up automatically.

To test apache2, simply enter the IP address of your server in your web browser, and you should see the following:

56. Apache Ubuntu Default Page

You can check your IP using ifconfig -a or ip a

Apache serves web pages from the Document Root, this is located in /var/www/html, this is where the default page is served from. It is a test page to show you that the server is working, as well as some useful nuggets regarding the default configuration.

What I love about apache is the virtual host feature, you can host more that one website on a server. The way it works is that it consists of an individual configuration file for different domains. For example, you can have a server with a single IP address that hosts three websites, e.g abc.com, devsrealm.com, classicpress.net, you get the idea.

I love to think of the server IP as an apartment, you know an apartment contains one or more rooms, so, the virtual hosts are the rooms. For example, website1.com is a room under my server IP (apartment), website2.com is also another room under my server IP.

Applying this to apache: You create a separate configuration file for each website and store them in your Apache configuration directory.

Each configuration file would include a <VirtualHost> format, where you would place an identifier such as a name or IP address that differentiates one website from the other.

When a user requests a web page, Apache will serve the website the user is requesting, just the way a user would go into a specific room.

The configuration files for each site typically end with the .conf filename extension and are stored in the /etc/apache2/sites-available directory.

Sitemap illustration of how virtual host works:

  • Website files are uploaded into /var/www or the chosen directory by the administrator
  • Server Admin creates a configuration file for the site and copies it into the /etc/apache2/sites-available directory
  • The admin enables the site and reloads Apache

Before we start configuring a test website, let me introduce you to two powerful commands that work with apache in ubuntu. The first is the a2ensite for enabling a site, and a2dissite for disabling site.

For example, let's assume you are hosting website1.com, and you want to enable it, you use the following command:

sudo a2ensite website1.com.conf
sudo systemctl reload apache2

I am using a relative path for the website, as long as you've copied the configuration file to the correct place, the a2ensite and a2dissite commands will know where to find it, we would get to creating a config file shortly.

To disable a site, you run the following command:

sudo a2dissite website1.com.conf
systemctl reload apache2

This would disable and reload apache, the reload command won't restart apache itself, it just reloads its configuration files. If you replace the reload with restart, it will perform a full restart, and hence, cause a downtime, most of the time, reloading is enough as long as you aren't enabling a new plugin that requires a restart.

Apache Main Configuration

The main apache config is located at /etc/apache2/apache2.conf, when you open it, you'll see useful instruction of how it operates.

If you go towards the bottom of the file, you'll see the following:

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

This line looks for enabled site in the /etc/apache2/sites-enabled directory. You'll notice an asterisk, this means any file stored there with the .conf file extension should be read by Apache.

Understanding Apache Default Virtual Host

If you look at the contents of the /etc/apache2/sitesavailable and the /etc/apache2/sites-enabled directories, you'll see the 000-default.conf configuration file stored in sites-available.

This was included with apache by default, and its configuration file was enabled as soon as Apache was installed.

This is all you need if you only plan on hosting a single website
on your server. Let's explore what's in the file (comments removed for easy reading):

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

The default virtual host is telling apache to listen on port 80 for requests and to serve content from /var/www/html, you can customize this.

The asterisk is a wildcard on port 80 that handles all web traffic that comes into the server from port 80.

The ServerAdmin email specifies the email address that is displayed in any error messages shown in case there is a problem with the site.

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.

Adding an Additional Virtual Host on a Server With Multiple IP Addresses

If you want to host another site on the same server, you can use the default config file template with few customizations, for example:

ServerAdmin webmaster@localhost
DocumentRoot /var/www/website1.com

ErrorLog ${APACHE_LOG_DIR}/website1.com-error.log
CustomLog ${APACHE_LOG_DIR}/website1.com-access.log combined

This is self-explanatory, this is what I am doing:

  • I am telling apache to listen to incoming traffic going to IP address on port 80, I am able to serve different website based on which IP address the request if coming from. I would show you how to configure different host on a single IP address in a moment
  • To keep each site separate, you need to serve different host from their own individual account, in this case, I am serving the content from /var/www/website1.com.
  • I also kept the log entry separate, this way each site can have a different log entry for distinct log entries, in this case, I appended a name to the log entry, so website1.com log entry goes to /var/log/apache2/website1.com-error.log
  • The access log for each website should also be different, so, website1.com access log is written to /var/log/apache2/website1.com.access.log

If you have an additional IP address, you can also create a config file for that, which would differentiate the virtual host by IP, to create multiple virtual hosts on a single IP address, refer to the next section.

Creating Multiple Virtual Host on a Single IP address

If you happen to have rented a VPS, you would most likely be assigned a single IP address, what I like about this setup is that you can differentiate your virtual host by name instead of differentiating them by IP.

To get started, navigate into /etc/apache2/sites-available directory, and create a conf, for example, I am using 000-multiple-hosts.conf and I formated the contents as follows:

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

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

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

I am declaring a servername with a matching DocumentRoot, any traffic coming into the server requesting website3.com will be provided a document root of /var/www/webiste3 and so on.

You can create 100 of them as long as your server can handle it, see you in more future guides related to apache.

Related Post(s)

  • Installing WP-CLI In a GNU/Linux Server

    WP-CLI is a command-line interface for WordPress. It can also be used with ClassicPress, as they are no differences in their usage, maybe just minimal if you are updating or downloading new ClassicPr

  • Installing and Running Rclone Mount As a Windows Service

    I previously wrote a guide on how to mount and unmount rclone in Linux, in this guide, I’ll walk you through on how to do the same on a windows system. Step 1: Download Rclone First, go to rclone d

  • Mounting and Unmounting Cloud Storage With (Rclone) in GNU/Linux

    Ever wondered if you could mount your preferred cloud storage as a virtual drive on your system? Thanks to Rclone, you can mount and access different kinds of cloud storage from a file manager, I'll

  • How To Configure and Enable Syntax Highlighting in Nano Text Editor

    nano is one of the most popular, and friendly editor in GNU/Linux, it has several features that can boost the usability and easiness of the program. In the guide, you'll learn how to configure and en

  • Monitoring Multiple Log Files In RealTime With MultiTail (Ubuntu)

    Oh my... I really find scanning through the logs file time consuming, and painful. Luckily for me, I founded Multitail, which is an awesome, and powerful tool for not only browsing through several f

  • How To Find Files Using Locate in Ubuntu

    It's kind of frustrating when you are searching for a particular file, and you have no idea of how to find it, In this guide, I'll walk you through on two different ways you can find a file in your G