How to Install Apache 2 on Ubuntu 22.04

Log in as your non-root user and proceed with this guide.

Installing Apache

Ubuntu’s default software repositories offers Apache, making it possible to install it using conventional package management tools.

sudo apt update

Then, install the apache2 package:

sudo apt install apache2

After confirming the installation, apt will install Apache and all required dependencies.

Adjusting the Firewall

Before testing Apache, it’s necessary to modify the firewall settings to allow outside access to the default web ports. If 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 running the following:

sudo ufw app list

Your output will be a list of the application profiles:

OutputAvailable 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 you haven’t configured SSL for your server yet in this guide, you’ll only need to allow traffic on port 80:

sudo ufw allow ‘Apache’

You can verify the change by checking the status:

sudo ufw status

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 22.04 starts Apache. The web server will already be up and running.

Make sure the service is active by running the command for the systemd init system:

sudo systemctl status apache2

Output● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor prese>
     Active: active (running) since Tue 2023-01-22 00:00:00 UTC; 10s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 4092 (apache2)
      Tasks: 23 (limit: 1119)
     Memory: 5.1M
        CPU: 21ms
     CGroup: /system.slice/apache2.service
             ├─4095 /usr/sbin/apache2 -k start
             ├─4093 /usr/sbin/apache2 -k start
             └─4094 /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 writing the following at your server’s command prompt:

hostname -I

You will receive a few addresses separated by spaces. You can try each in your web browser to determine if they work.

When you have your server’s IP address, enter it into your browser’s address bar:

http://my_ip

You will see the default Ubuntu 22.04 Apache web page as in the following:

Apache Deafult Page

It Works !

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 review some basic management commands using systemctl.

To stop your web server, run:

sudo systemctl stop apache2

To start the web server when it is stopped, run:

sudo systemctl start apache2

To stop and then start the service again, run:

sudo systemctl restart apache2

If you are simply making configuration changes, Apache can often reload without dropping connections. To do this, use the following command:

sudo systemctl reload apache2

By default, Apache is configured to start automatically when the server boots. If this is not what you want, disable this behavior by running:

sudo systemctl disable apache2

To re-enable the service to start up at boot, run:

sudo systemctl enable apache2

Apache will now start automatically when the server boots again.