How To

Install Apache on Ubuntu 20.04: Setup and Virtual Host Guide

Apache is one of the most widely used open-source web servers in the world. It is cross-platform, highly flexible, and can be extended with modules to handle SSL, caching, authentication, URL rewriting, and more. A large share of the internet runs on Apache.

This guide shows you how to install Apache on Ubuntu 20.04, open the firewall, verify the setup, and configure a virtual host for your domain.

<strong>Prerequisite:</strong>&nbsp;You need a user account with sudo access to follow these steps.

Install Apache on Ubuntu

Apache is available in the default Ubuntu repositories. On Ubuntu and Debian systems, the package and service are both called apache2. Install it with:

bashsudo apt updatesudo apt install apache2

The service starts automatically after install. Confirm it is running:

bashsudo systemctl status apache2

Look for active (running) in the output. The default web root directory is /var/www/html — this is where Apache serves files from by default.

To start, stop, or restart Apache at any time:

bashsudo systemctl start apache2sudo systemctl stop apache2sudo systemctl restart apache2

Open the Firewall

Apache listens on port 80 for HTTP and port 443 for HTTPS. Use the Apache Full UFW profile to open both ports at once:

bashsudo ufw allow 'Apache Full'

Verify the rule is active:

bashsudo ufw status

You should see Apache Full listed as allowed. The Apache Full profile covers both plain HTTP traffic and encrypted HTTPS traffic.

Verify the Apache Installation

Open your browser and go to:

http://your_server_ip_or_domain/

You should see the default Apache welcome page. It confirms the web server is installed and working. The page also shows key paths — where config files, log files, and the web root are located on your system.

Set Up a Virtual Host

A virtual host lets you run more than one website on a single server. Each site gets its own configuration file, which tells Apache:

  • Which domain name it handles (ServerName)
  • Where the website files are stored (DocumentRoot)
  • Where Apache writes error and access logs

Apache includes one virtual host by default. If you only host one site, place your files in /var/www/html and you are done.

For multiple sites, set up a separate virtual host for each domain. This example uses example.com. Replace it with your actual domain name.

Create the document root directory:

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

Set ownership to the Apache user so the server can read the files:

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

Create the virtual host config file:

bashsudo nano /etc/apache2/sites-available/example.com.conf

Paste this configuration:

apache&lt;VirtualHost *:80&gt;    ServerName example.com    ServerAlias www.example.com    ServerAdmin webmaster@example.com    DocumentRoot /var/www/example.com    &lt;Directory /var/www/example.com&gt;        Options -Indexes +FollowSymLinks        AllowOverride All    &lt;/Directory&gt;    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined&lt;/VirtualHost&gt;

Enable the site with a2ensite:

bashsudo a2ensite example.com

Check the config for syntax errors:

bashsudo apachectl configtest

If you see Syntax OK, restart Apache to apply the changes:

bashsudo systemctl restart apache2

Visit http://example.com in your browser. Your site should load from the document root you set.

<strong>Tip:</strong>&nbsp;To add HTTPS to your site, use&nbsp;<strong>Let's Encrypt</strong>&nbsp;with Certbot. It is free and works well with Apache on Ubuntu.

Apache is now installed and serving your site on Ubuntu. From here, you can deploy web applications, add virtual hosts for more domains, or enable modules to extend what Apache can do. Got questions? Leave a comment below.

Cyber Defence

Recent Posts

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

2 days ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

2 days ago

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

3 days ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

6 days ago

ufw Command in Linux: Manage Firewall Rules with Examples

ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…

6 days ago

who Command in Linux: Show All Logged-In Users and Sessions

When you share a server with a team or investigate unexpected activity, the first question…

6 days ago