Introduction

The Apache HTTP server is the most widely-used web server in the world. It provides many powerful features, including dynamically loadable modules, robust media support, and extensive integration with other popular software.

In this guide, we'll explain how to install an Apache web server on your Ubuntu 20.04 server. For a more detailed version of this tutorial, please refer to How To Install the Apache Web Server on Ubuntu 20.04.

Prerequisites

apache illustration for: Prerequisites

Before you begin this guide, you should have the following:

  • An Ubuntu 20.04 server and a regular, non-root user with sudo privileges. Additionally, you will need to enable a basic firewall to block non-essential ports. You can learn how to configure a regular user account and set up a firewall for your server by following our Initial Server Setup for Ubuntu 20.04 guide.

When you have an account available, log in as your non-root user to begin.

Step 1 — Installing Apache

Apache is available within Ubuntu's default software repositories, so you can install it using conventional package management tools.

Update your local package index:

				
					
sudo apt update

				
			

Install the apache2 package:

				
					
sudo apt install apache2

				
			

Step 2 — Adjusting the Firewall

Check the available ufw application profiles:

				
					
sudo ufw app list

				
			
				
					
[secondary_label Output]

Available applications:

  Apache

  Apache Full

  Apache Secure

  OpenSSH

				
			

Let's enable the most restrictive profile that will still allow the traffic you've configured, permitting traffic on port 80 (normal, unencrypted web traffic):

				
					
sudo ufw allow 'Apache'

				
			

Verify the change:

				
					
sudo ufw status

				
			
				
					
[secondary_label Output]

Status: active



To                         Action      From

--                         ------      ----

OpenSSH                    ALLOW       Anywhere                  

Apache                     ALLOW       Anywhere                  

OpenSSH (v6)               ALLOW       Anywhere (v6)             

Apache (v6)                ALLOW       Anywhere (v6)

				
			

Step 3 — Checking your Web Server

Check with the systemd init system to make sure the service is running by typing:

				
					
sudo systemctl status apache2

				
			
				
					
[secondary_label Output]

apache2.service - The Apache HTTP Server

     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor prese>

     Active: <^>active (running)<^> since Tue 2020-04-28 23:06:40 UTC; 56s ago

       Docs: https://httpd.apache.org/docs/2.4/

   Main PID: 13785 (apache2)

      Tasks: 55 (limit: 1137)

     Memory: 5.3M

     CGroup: /system.slice/apache2.service

             ├─13785 /usr/sbin/apache2 -k start

             ├─13787 /usr/sbin/apache2 -k start

             └─13788 /usr/sbin/apache2 -k start



				
			

Access the default Apache landing page to confirm that the software is running properly through your IP address:

				
					
http://<^>your_server_ip<^>

				
			

You should receive the default Ubuntu 20.04 Apache web page:

Step 4 — Setting Up Virtual Hosts (Recommended)

When using the Apache web server, you can use _virtual hosts_ (similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server. We will set up a domain called your_domain, but you should replace this with your own domain name. To learn more about setting up a domain name with the cloud provider, please refer to our our Introduction to DNS hosting.

Create the directory for <^>your_domain<^>:

				
					
sudo mkdir /var/www/&lt;^&gt;your_domain&lt;^&gt;

				
			

Assign ownership of the directory:

				
					
sudo chown -R $USER:$USER /var/www/&lt;^&gt;your_domain&lt;^&gt;

				
			

The permissions of your web roots should be correct if you haven't modified your unmask value, but you can make sure by typing:

				
					
sudo chmod -R 755 /var/www/&lt;^&gt;your_domain&lt;^&gt;

				
			

Create a sample index.html page using nano or your favorite editor:

				
					
nano /var/www/&lt;^&gt;your_domain&lt;^&gt;/index.html

				
			

Inside, add the following sample HTML:

				
					
[label /var/www/your_domain/index.html]

&lt;html&gt;

    &lt;head&gt;

        &lt;title&gt;Welcome to &lt;^&gt;Your_domain&lt;^&gt;!&lt;/title&gt;

    &lt;/head&gt;

    &lt;body&gt;

        &lt;h1&gt;Success!  The &lt;^&gt;your_domain&lt;^&gt; virtual host is working!&lt;/h1&gt;

    &lt;/body&gt;

&lt;/html&gt;

				
			

Save and close the file when you are finished.

Make a new virtual host file at /etc/apache2/sites-available/<^>your_domain<^>.conf:

				
					
sudo nano /etc/apache2/sites-available/&lt;^&gt;your_domain&lt;^&gt;.conf

				
			

Paste in the following configuration block, updated for our new directory and domain name:

				
					
[label /etc/apache2/sites-available/your_domain.conf]

&lt;VirtualHost *:80&gt;

    ServerAdmin webmaster@localhost

    ServerName &lt;^&gt;your_domain&lt;^&gt;

    ServerAlias &lt;^&gt;your_domain&lt;^&gt;

    DocumentRoot /var/www/&lt;^&gt;your_domain&lt;^&gt;

    ErrorLog ${APACHE_LOG_DIR}/error.log

    CustomLog ${APACHE_LOG_DIR}/access.log combined

&lt;/VirtualHost&gt;

				
			

Save and close the file when you are finished.

Enable the file with a2ensite:

				
					
sudo a2ensite &lt;^&gt;your_domain&lt;^&gt;.conf

				
			

Disable the default site defined in 000-default.conf:

				
					
sudo a2dissite 000-default.conf

				
			

Test for configuration errors:

				
					
sudo apache2ctl configtest

				
			

You should receive the following output:

				
					
[secondary_label Output]

Syntax OK

				
			

Restart Apache to implement your changes:

				
					
sudo systemctl restart apache2

				
			

Apache should now be serving your domain name. You can test this by navigating to http://<^>your_domain<^>, where you should receive something like this:

Conclusion

Now that you have your web server installed, you have many options for the type of content to serve and the technologies you want to use to create a richer experience.

If you'd like to build out a more complete application stack, check out this article on How to configure a LAMP stack on Ubuntu 20.04.