How to Install Apache on Ubuntu 20.04

Posted 

4 min read

Apache is one of the most popular web servers in the world. It is an open-source and cross-platform HTTP server that powers a large percentage of the Internet’s websites. Apache provides many powerful features that can be extended through additional modules.

This tutorial describes how to install and manage the Apache webserver on Ubuntu 20.04.

Prerequisites

Before starting with the tutorial, make sure you are logged in as a user with sudo privileges.

Installing Apache

Apache is included in the default Ubuntu repositories.

The installation is pretty straightforward. On Ubuntu and Debian systems, the Apache package and the service is called apache2.

Run the following commands to update the package index and install Apache:

sudo apt updatesudo apt install apache2

When the installation process, is complete the Apache service will automatically start.

You can verify that Apache is running by typing:

sudo systemctl status apache2

The output should tell you that the service is running and enabled to start on system boot:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2020-05-09 19:28:40 UTC; 36min ago
...

That’s it, you have successfully installed Apache on your Ubuntu 20.04 server, and you can start using it.

Opening HTTP and HTTPs Ports

Apache listens on port 80 (HTTP) and 443 (HTTPS). You must open those ports in your firewall so that the webserver is accessible from the Internet.

Assuming you are using UFW, you can do that by enabling the ‘Apache Full’ profile which includes rules for both ports:

sudo ufw allow 'Apache Full'

Verify the change:

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
Apache Full                ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
Apache Full (v6)           ALLOW       Anywhere (v6)

Verifying the Apache Installation

To verify that everything works correctly, open your browser, type your server IP address http://YOUR_IP_OR_DOMAIN/, and you will see the default Ubuntu 20.04 Apache welcome page as shown below:

The page includes some basic information about Apache configuration files, helper scripts, and directory locations.

Setting up a Virtual Host

A Virtual Host is an Apache configuration directive that allows you to run more than one website on a single server. Typically a virtual host describes one website.

Apache ships with one virtual host enabled by default. All domains that points to the server IP address will match the default virtual host. If you’ll be hosting a single website you can upload its content in /var/www/html and edit editing the virtual host configuration found in the /etc/apache2/sites-enabled/000-default.conf file.

If you intend to host more than one website, you’ll need to create a virtual host configuration for each site. In this section, we’ll set up a website for a domain called “example.com”. You should replace “example.com” with your domain name.

The first step is to create the document root directory where the website files for the domain name will be stored and served in response to requests. Run the following command to create the directory:

sudo mkdir -p /var/www/example.com

For testing purposes, create an index.html file inside the domain document root directory:

/var/www/example.com/index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to example.com</title>
  </head>
  <body>
    <h1>Success! example.com home page!</h1>
  </body>
</html>

Save and close the file when you are done.

To avoid permission issues, change the ownership of the domain document root directory to the apache user (www-data):

sudo chown -R www-data: /var/www/example.com

The next step is to create a virtual host configuration for the “example.com” domain. The best practice is to store each vhost configuration in a separate file.

Apache vhosts files are stored in /etc/apache2/sites-available directory. The standard naming convention is to name the file according to the domain.

Open your text editor and create the following file:

/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

Apache does not read the configuration files found in the /etc/apache2/sites-available directory unless they are linked to the /etc/apache2/sites-enabled directory.

To activate the virtual host configuration, create a symlink using the a2ensite utility:

sudo a2ensite example.com

Test the configuration for any syntax errors with:

sudo apachectl configtest

If there are no errors, you will see the following output:

Syntax OK

Restart the Apache service for the changes to take effect:

sudo systemctl restart apache2

Finally, to verify that everything is working as expected, open http://example.com in your browser, and you will see something like this:

Conclusion

We’ve shown you how to install Apache on Ubuntu 20.04. You’re now ready to start deploying your applications and use Apache as a web or proxy server.

If you have any questions or feedback, feel free to leave a comment.