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

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

15 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

15 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

16 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

16 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

2 days ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

2 days ago