Apache Server featured image

Installing the Apache Server on Ubuntu 18.04: A How-To Guide

Introduction

The Apache HTTP Server is an open-source web server by the Apache Software Foundation. It is a software that connects your local server to the website servers such as Firefox or Google Chrome. This connection is used to send and receive files and data.

It is the most popular server across the globe. Over 40% of all websites on the world wide web are powered by Apache HTTP. The reasons behind its popularity are its high customizability, great integration capacity, and reliable media support. Apache operates on a module-based architecture. You can modify individual modules to add and remove various functionalities. In this tutorial, we will guide you on how to install the Apache server on Ubuntu 18.04.

Before Installation

Before you start installing Apache, you need to take care of some prerequisites. To begin with, make sure that you have already configured a non-root user with sudo privileges on the local server. The other thing you will need is a firewall. You have to enable your basic firewall and block the nonessential ports. Before you start the installation, log into your non-root user with privileges. You can easily install your server by following our tutorial on setting up an Ubuntu server.

How to Install the Apache Web Server on Ubuntu 18.04

If you have accounted for the prerequisites, you can move to the actual installation. You can install and explore the Apache web server in 6 simple steps:

Step 1: Install Apache

You will be glad to know that Apache is already a part of Ubuntu. This means that you can install Apache on your server using the typical package management tools. It is available as a default software on the repository. First, you will need to update the local package index:

This command makes the recent upstream changes show up in the package index. Next, we need to give the prompt to install the apache2 package:

Step 2: Change Your Firewall Settings

When taking care of the prerequisites, you must have set up your UFW firewall. If you followed the instructions, your firewall will be blocking your access to the server. We need to give the default web ports access to the server before operating Apache. As a result, firewall settings must be changed.

At the time of installation, Apache registers with the UFW. The result of this registration is the creation of some application profiles. Furthermore, you can use these profiles to enable or disable firewall access to Apache. Then, visualize these ufw applications with the following command:

You will see your application profile in a list like this:

Apache Server ufw app list

The above list shows that three application profiles have been created, namely Apache, Apache Full, and Apache Secure. Here are some useful details about each of these profiles:

  • Apache:

The Apache profile is able to open port 80 only. This port allows for normal and unencrypted web traffic.

  • Apache Full:

The Apache Full profile has the capacity to open both port 80 and 443. Where port 80 allows for normal, unencrypted web traffic, port 443 accounts for TLS/SSL encrypted traffic.

  • Apache Secure:

Lastly, Apache Secure only opens port 443. As mentioned above, it is responsible for letting in TLS or SSL encrypted web traffic.

When choosing between these profiles, think about the traffic that you have configured. You will want to make sure the desired traffic goes through while opting for the most restrictive profile. SSL configuration is out of our scope at the moment. Hence, we will focus on traffic coming through port 80 for now. This means we are enabling the application profile ‘Apache’ in this scenario. You can do so like this:

To verify the modification, type in the following:

This command will display an Output containing the HTTP traffic that is currently allowed to pass through. You will see something like this:

Apache Server firewall status

The traffic flow shows that the desired Apache profile has been activated. Access has been granted to the web server through the firewall.

Step 3: Check Your Web Server

The installation of Apache is complete and the firewall settings have been modified. At the end of this process, Ubuntu 18.04 will automatically launch Apache. Next you need to check with the systemd init system if the service is running at any given moment. If you want to learn more about init ѕуѕtеms, you can check out our tutorial on how to manage Systemd services and units with Systemctl. To check whether the service is running, type:

The Output will be as follows:

apache2 status

You need to look for the Active or running status of the service in the above. This shows that the service has launched and is running successfully. There is another, more preferable way to test the functioning of the service. You can check its status by requesting a page from Apache. Using your server’s IP address, you can confirm if the software is running on the default Apache landing page. Here is a prompt you can use to get your server’s IP address if you do not already know it:

This command will show a result with a number of IP addresses separated by spaces. Test each one individually in the browser. Another way to access it is to grab your public IP address. You can use a prompt like this:

This will show you your IP address as seen in other locations over the web. Next, once you have your IP address, go to your web browser. In the address bar, type in and run the following:

This will open up the default Ubuntu 18.04 Apache web page. It looks like this:

ubuntu homepage

This page contains some basic information about your files and directories. Since it launched upon typing the address, it indicates that Apache is running properly.

Step 4: Basic Management in Apache

The verification confirms that Apache is running and your server is operational. Now, we will explore some of the basic commands you can use on the server for management.

The default configuration of your Apache launches it whenever the server boots. You have the option to disable this setting. Type in:

To reactivate this default feature, use this command:

Next, type the following if you want to stop the server manually:

If you want to start it back up again after stopping it, use the command:

To stop and start the server, or essentially refresh it, type in:

When you stop the server, it tends to lose all of its connections. Starting it again means it finds and links those connections again. If you want, you can also reload Apache without losing the connection. This is particularly useful when making configuration changes. Type in the following to reload without dropping connections:

Step 5: Virtual Host Setup

While this is not mandated, it is highly recommended that you set up your virtual hosts. Virtual hosts are little blocks in the server that contain groups of configuration details. You can host several domains on one virtual host from your server.

By default, if you are using Apache on Ubuntu 18.04, there will be one activated server block that will be catering to documents in the /var/www/html directory. You will want to change this setting if you are hosting multiple sites instead of just a single one. However, there is another possible option that works even better.  Leave the /var/www/html directory untouched. Instead, go to /var/www/ to build a directory structure for the sites on your desired domain. If you do this, the /var/www/html directory is configured as the default directory. In case a client request does not match any of the sites on your domain, they will be served from here.

After that, we will see how you can create a new domain and its associated directory. For the sake of this example, we will use a hypothetical name, your_domain. Replace your_domain wherever present in the subsequent instructions with your own domain name. To start off, let’s see how you can create a new directory for your_domain:

Now, you have to assign ownership of the directory. You can use the $USER environment variable  for this purpose:

To continue, your web root permissions have to be correct. If you have not touched the unmask value in the configuration, your permissions should be fine. But if you are not sure, you can always confirm with:

Next, we will create an index.html page. You can use nano like us or any other editor, if you have a preference:

Add this sample HTML inside the index, then Save and Close:

The next step is to create a virtual host file. A virtual host file with all the right directives is necessary to make Apache serve the above sample content. The default configuration file will be laying at: /etc/apache2/sites-available/000-default.conf. Instead of modifying that file, we will make a new one at: /etc/apache2/sites-available/your_domain.conf.

Similarly as before, we will add a sample for configuration. This time, however, we will update it with respect to the domain name and the new directory we just created. Add this block, then Save and Close:

As you can see above, we have also updated our ServerAdmin email. This is an address that the your_domain site admin can reach. Not only that, but we have also updated the DocumentRoot to the fresh directory and added two new directives. These directives include ServerName and ServerAlias. ServerName is used to set the base domain. The base domain has to match to define the virtual host.  ServerAlias, on the other hand, provides other names that should also match, similar to the base name. You can enable the file using the a2ensite tool easily:

To disable the default site mentioned in 000-default.conf, use this command:

Now that the configuration is ready, you can check for error by typing:

This will show the following Output if there are no configuration errors:

To apply the changes, you must restart the Apache server like instructed before:

Voila! You have set up your virtual host and set your domain name. To check if Apache is, indeed, serving your_domain, go to http://your_domain.

Step 6: Browsing the Files and Directories

You have successfully performed some basic configuration of your Apache server. In addition, you are aware of how you can manage your Apache service. Next, you can spend some time exploring some of the most critical directories and files.

Content
  • /var/www/html

We have talked about this directory before as well. It contains the default Apache page. If you want, you can change the content of this page. To do so, you will have to modify the Apache configuration files.

Server Configuration

 

  • /etc/apache2

This is the main Apache configuration directory. Meaning this directory consists of all the configuration files associated with Apache.

  • /etc/apache2/apache2.conf

As the addition at the end of the directory suggests, this is the main configuration file. This file loads several other files in the directory and can be used to make modifications in the Apache configuration. If you want to make any change to the configuration of the Apache server, here is where you will make the alterations.

  • /etc/apache2/ports.conf

As suggested by the name, this file pertains to the ports which we have discussed before. Port 80 is enabled by default and port 443 is activated when SSL containing modules are introduced. This file contains details regarding which ports are open and enabled.

  • /etc/apache2/sites-available/

This is where your virtual hosts will go. Any and all server block configuration to be done is performed in this directory. To enable server blocks, they have to be linked to other directories through a2ensite. Apache will only have access to the files in this directory if they have been enabled by linking to the sites-enabled directory.

  • /etc/apache2/sites-enabled/

This is the directory where the enabled virtual hosts are located. As we mentioned, hosts are enabled when you link the files in the sites-available directory through the a2ensite command. The files in this directory are important for when Apache starts, reloads, or compiles a configuration.

  • /etc/apache2/conf-available/, /etc/apache2/conf-enabled/

These directories work in a similar way to sites-available and sites-enabled. To enable the files in the conf-available directory, you must use the a2enconf command. The disabling command is a2disconf. Instead of storing per-site virtual hosts, these directories consist of configuration details that are not relevant to the virtual host:

  • /etc/apache2/mods-available/, /etc/apache2/mods-enabled/

These directories also have a relationship similar to the above two examples. They contain the modules, those available and those enabled by the a2enmod command. To disable a module, you can use the a2dismod command. Files with the .load suffix have fragments that can load a given module. Files with the .conf suffix hold the relevant configuration details for the given module.

Server Logs

 

  • /var/log/apache2/access.log

This log file contains all of the requests made to your server. This happens by default. You have the option to configure Apache to record requests differently.

  • /var/log/apache2/error.log

This is the error log. All of the errors you make go right to this file by default. You can see the detail with which the error logs are recorded through the LogLevel directive.

Conclusion

Finally, by following the above steps, you can easily install the Apache webserver on your Ubuntu 18.04. Once you have the server on your system, you can explore the various options you have on the platform. You can use different technologies to produce various kinds of content. In this way, you can cultivate a unique and facilitative experience for yourself on the server.

You can also learn how to secure your Apache server with Let’s Encrypt by following our step-by-step tutorial. If you want to learn how to install Apache on CentOS 7, follow our tutorial on how to install Linux, Apache, MySQL, PHP (LAMP) stack on CentOS 7.

Happy Computing!