Published on January 28, 2025
How To Install the Apache Web Server on Ubuntu 20.04
Published on January 28, 2025
Introduction
The Apache HTTP server is one of the most popular web servers in the world. In this guide, we’ll explain how to install an Apache web server on your Ubuntu 20.04 server.
Installation Steps
Step 1 — Updating the Package Index
First, update the package index to ensure you have the latest repository information:
sudo apt update
Install Apache by running the following command:
sudo apt install apache2
Adjusting the Firewall
Before testing Apache, it’s necessary to modify the firewall settings to allow outside access to the default web ports. Assuming that you followed the instructions in the prerequisites, you should have a UFW firewall configured to restrict access to your server.
During installation, Apache registers itself with UFW to provide a few application profiles that can be used to enable or disable access to Apache through the firewall.
List the UFW application profiles by typing:
sudo ufw app list
Output:
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
As indicated by the output, there are three profiles available for Apache:
- Apache: This profile opens only port 80 (normal, unencrypted web traffic)
- Apache Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
- Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic)
It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Since we haven’t configured SSL for our server yet in this guide, we will only need to allow traffic on port 80:
sudo ufw allow 'Apache'
You can verify the change by typing:
sudo ufw status
The output will provide a list of allowed HTTP traffic:
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)
As indicated by the output, the profile has been activated to allow access to the Apache web server.
Checking Your Web Server
At the end of the installation process, Ubuntu 20.04 starts Apache. The web server should already be up and running.
Check with the systemd init system to make sure the service is running by typing:
sudo systemctl status apache2
Output:
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2020-04-23 22:36:30 UTC; 20h ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 29435 (apache2)
Tasks: 55 (limit: 1137)
Memory: 8.0M
CGroup: /system.slice/apache2.service
├─29435 /usr/sbin/apache2 -k start
├─29437 /usr/sbin/apache2 -k start
└─29438 /usr/sbin/apache2 -k start
As confirmed by this output, the service has started successfully. However, the best way to test this is to request a page from Apache.
You can access the default Apache landing page to confirm that the software is running properly through your IP address. If you do not know your server’s IP address, you can get it a few different ways from the command line.
Try typing this at your server’s command prompt:
hostname -I
You will get back a few addresses separated by spaces. You can try each in your web browser to determine if they work.
Another option is to use the Icanhazip tool, which should give you your public IP address as read from another location on the internet:
curl -4 icanhazip.com
When you have your server’s IP address, enter it into your browser’s address bar:
http://your_server_ip
You should see the default Ubuntu 20.04 Apache web page:

This page indicates that Apache is working correctly. It also includes some basic information about important Apache files and directory locations.
Managing the Apache Process
Now that you have your web server up and running, let’s go over some basic management commands using systemctl
.
Stopping Apache
To stop your web server, type:
sudo systemctl stop apache2
Starting Apache
To start the web server when it is stopped, type:
sudo systemctl start apache2
Restarting Apache
To stop and then start the service again, type:
sudo systemctl restart apache2
Reloading Apache
If you are simply making configuration changes, Apache can often reload without dropping connections. To do this, use this command:
sudo systemctl reload apache2
Enabling or Disabling Apache at Boot
By default, Apache is configured to start automatically when the server boots. If this is not what you want, disable this behavior by typing:
sudo systemctl disable apache2
To re-enable the service to start up at boot, type:
sudo systemctl enable apache2
Apache should now start automatically when the server boots again.
Setting Up Virtual Hosts (Recommended)
Create the Directory for Your Domain
Create the directory for your domain as follows:
sudo mkdir /var/www/your_domain
Assign Ownership
Next, assign ownership of the directory with the $USER
environment variable:
sudo chown -R $USER:$USER /var/www/your_domain
Set Permissions
The permissions of your web roots should be correct if you haven’t modified your umask value, which sets default file permissions. To ensure that your permissions are correct and allow the owner to read, write, and execute the files while granting only read and execute permissions to groups and others, you can input the following command:
sudo chmod -R 755 /var/www/your_domain
Create a Sample Index Page
Next, create a sample index.html
page using nano or your favorite editor:
sudo nano /var/www/your_domain/index.html
Inside, add the following sample HTML:
<html>
<head>
<title>Welcome to Your_domain!</title>
</head>
<body>
<h1>Success! The your_domain virtual host is working!</h1>
</body>
</html>
Save and close the file when you are finished.
Create a Virtual Host Configuration File
In order for Apache to serve this content, it’s necessary to create a virtual host file with the correct directives. Instead of modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf
directly, let’s make a new one at /etc/apache2/sites-available/your_domain.conf
:
sudo nano /etc/apache2/sites-available/your_domain.conf
Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain name:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName your_domain
ServerAlias www.your_domain
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file when you are finished.
Enable the New Site and Disable Default Site
Let’s enable the file with the a2ensite
tool:
sudo a2ensite your_domain.conf
Disable the default site defined in 000-default.conf
:
sudo a2dissite 000-default.conf
Test Configuration and Restart Apache
Next, let’s test for configuration errors:
sudo apache2ctl configtest
You should receive the following 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 see something like this:
Apache virtual host example
Conclusion
Congratulations! You’ve successfully installed Apache on your Ubuntu 20.04 server. Now you can host websites and explore advanced Apache configurations.