Masterfully Install the Apache Web Server on Ubuntu 22

The Apache HTTP server, the world’s most popular web server, boasts powerful features such as dynamically loadable modules, robust media support, and extensive integration with popular software.
In this guide, we’ll walk you through installing an Apache web server on your Ubuntu system.
Installing Apache
Apache is included in Ubuntu’s default software repositories, allowing for installation via standard package management tools.
First, update the local package index to ensure it reflects the latest upstream changes:
sudo apt update && sudo apt install apache2
Adjusting the Firewall
Before testing Apache, you need to adjust the firewall settings to permit external access to the default web ports. If you followed the prerequisite instructions, you should have a UFW firewall configured to limit server access.
Upon installation, Apache automatically registers with UFW, creating several application profiles to manage Apache access through the firewall.
To list the UFW application profiles:
sudo ufw app list
Output:Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
As shown in the output, there are three available profiles for Apache:
- Apache: Opens only port 80 (normal, unencrypted web traffic)
- Apache Full: Opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
- Apache Secure: Opens only port 443 (TLS/SSL encrypted traffic)
It is recommended to enable the most restrictive profile that meets your traffic requirements. Since SSL is not configured 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:
OutputStatus: 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 2024-05-27 22:36:30 UTC; 01h ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 76597 (apache2)
Tasks: 55 (limit: 1411)
Memory: 13.0M
CGroup: /system.slice/apache2.service
├─30489 /usr/sbin/apache2 -k start
├─30491 /usr/sbin/apache2 -k start
└─30493 /usr/sbin/apache2 -k start
As confirmed by the output, the service has started successfully. The best way to verify this is by requesting a page from Apache.
To ensure the software is running properly, access the default Apache landing page via your IP address. If you’re unsure of your server’s IP address, you can retrieve it from the command line.
At your server’s command prompt:
ip -4 addr show | grep inet | awk '{print $2}' | cut -d/ -f1
When you have your server’s IP address, enter it into your browser’s address bar:
http://your_server_ip

Understanding Apache Processes Commands
Now that your web server is up and running, let’s review some basic management commands using systemctl
.
To stop your web server
sudo systemctl stop apache2
To start the web server when it is stopped
sudo systemctl start apache2
To restart the Apache service:
sudo systemctl restart apache2
If you’re making configuration changes, Apache can often reload without dropping connections. Use the following command to achieve this:
sudo systemctl reload apache2
By default, Apache is configured to start automatically when the server boots. If you want to disable this behavior:
sudo systemctl disable apache2
To re-enable the service to start up at boot:
sudo systemctl enable apache2
Virtual Hosts
When working with Apache, virtual hosts allow you to manage multiple domains on one server. We’ll set up a domain named “your_domain,” but adjust it to your actual domain. Apache on Ubuntu 20.04 has one server block by default, serving files from /var/www/html. For multiple sites, managing them within /var/www is more organized. Let’s create a directory structure for your_domain within /var/www while keeping /var/www/html as the default directory for unmatched requests.
Create the directory for your_domain:
sudo mkdir /var/www/your_domain
Next, assign ownership of the directory with the $USER
environment variable:
sudo chown -R $USER:$USER /var/www/your_domain
If you haven’t altered your umask value, the permissions of your web roots should be correct. To confirm and ensure proper permissions, allowing the owner to read, write, and execute files while granting read and execute permissions to groups and others, use the following command:
sudo chmod -R 755 /var/www/your_domain
Next, create a sample index.html
page using nano
or your favorite editor:
Inside, add the following sample HTML:
nano /var/www/your_domain/index.html
Add HTML Content into the index.html file
<html>
<head>
<title>Welcome to MaleCloud!</title>
</head>
<body>
<h1>Success! your webserver is working fine and good!</h1>
</body>
</html>
Save and close the file when you are finished. (control + x –> type “y” –> Enter or return)
To enable Apache to serve this content, you need to create a virtual host file with the correct directives. Rather than modifying the default configuration file directly located at /etc/apache2/sites-available/000-default.conf, let’s create a new one at /etc/apache2/sites-available/your_domain.conf:
sudo nano /etc/apache2/sites-available/your_domain.conf
Paste this code there!
<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>
Note the updates we’ve made:
- DocumentRoot points to our new directory.
- ServerAdmin is set to an email address accessible to the your_domain site administrator.
- Two directives are added:
- ServerName establishes the base domain for this virtual host definition.
- ServerAlias defines additional names that should match as if they were the base name.
Save and close the file when you are finished.
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
Next, let’s test for configuration errors:
sudo apache2ctl configtest
You should receive the following output:
Output:
Syntax OK
Restart Apache to implement your changes:
sudo systemctl restart apache2
Apache should now be serving your domain name. You can verify this by navigating to http://your_domain. If everything is set up correctly, you should see something similar to this:

Some Apache important file and directory
Now that you know how to manage the Apache service itself, take a moment to familiarize yourself with some important directories and files:
Content:
- /var/www/html: This directory contains the actual web content served by Apache. By default, it only consists of the default Apache page you saw earlier but can be changed by modifying Apache configuration files.
Server Configuration:
- /etc/apache2: The Apache configuration directory where all configuration files reside.
- /etc/apache2/apache2.conf: The main Apache configuration file responsible for loading many other configuration files in the directory.
- /etc/apache2/ports.conf: This file specifies the ports Apache will listen on. By default, it listens on port 80 and additionally on port 443 when SSL capabilities are enabled.
Virtual Hosts:
- /etc/apache2/sites-available/: Directory for storing per-site virtual hosts. Configuration files here are not used unless linked to the sites-enabled directory. Typically, server block configuration is done here and then enabled by linking to sites-enabled.
- /etc/apache2/sites-enabled/: Directory for enabled per-site virtual hosts. Configuration files here are read by Apache to compile a complete configuration when it starts or reloads.
Additional Configurations:
- /etc/apache2/conf-available/, /etc/apache2/conf-enabled/: Similar to sites-available and sites-enabled, these directories store configuration fragments not specific to virtual hosts. Files in conf-available can be enabled/disabled using a2enconf/a2disconf.
- /etc/apache2/mods-available/, /etc/apache2/mods-enabled/: These directories contain available/enabled modules, respectively. Modules can be enabled/disabled using a2enmod/a2dismod. Files ending in .load load specific modules, while those ending in .conf contain module configurations.