How To Install the Apache Web Server on Ubuntu 22.04

June 1, 2023 0 Comments

Introduction

The Apache HTTP server is renowned as the most commonly utilized web server globally. It offers a wide array of impressive functionalities, such as the ability to load modules dynamically, strong support for various media formats, and seamless integration with other popular software.

This tutorial will guide you through the process of installing an Apache web server on your Ubuntu 22.04 server.

Prerequisites

Prior to commencing with this guide, it is essential to have an Ubuntu 22.04 server prepared, complete with a non-root user possessing sudo privileges, and an activated firewall to restrict access to non-essential ports. To accomplish this, you can refer to our Initial server setup guide for Ubuntu 22.04, which will walk you through the necessary steps.

After successfully setting up your server, log in using your non-root user credentials and proceed to the first step.

Step 1 — Installing Apache

Ubuntu’s default software repositories include Apache, allowing for its installation using standard package management tools.

Begin by updating the local package index to reflect the latest upstream changes:

sudo apt update

Then, install the apache2 package:

sudo apt install apache2

Once the installation is confirmed, the ‘apt’ package manager will proceed to install Apache along with all the necessary dependencies.

Step 2 — Adjusting the Firewall

Before testing Apache, it is important to adjust the firewall settings to permit external access to the default web ports. If you have followed the prerequisites, you should have already configured the UFW firewall to restrict access to your server.

During the Apache installation process, it automatically registers itself with UFW and provides several application profiles. These profiles can be utilized to enable or disable access to Apache through the firewall.

To list the available UFW application profiles, run the following command:

sudo ufw app list

Your output will be a list of the application profiles:

Output
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH

As per the displayed output, there are three available profiles for Apache:

  1. Apache: This profile allows access only through port 80 for normal, unencrypted web traffic.
  2. Apache Full: This profile enables access through both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic).
  3. Apache Secure: This profile permits access only through port 443 for TLS/SSL encrypted traffic.

It is recommended to enable the most restrictive profile that still allows the desired traffic. Since this guide does not cover SSL configuration for your server, you only need to allow traffic on port 80.

sudo ufw allow ‘Apache’

You can verify the change by checking the status:

sudo ufw status

The output will provide a list of allowed HTTP traffic:

Output
Status: active

To Action From
————-                    ———            ——–
OpenSSH                  ALLOW     Anywhere
Apache                      ALLOW     Anywhere
OpenSSH (v6)           ALLOW      Anywhere (v6)
Apache (v6)               ALLOW       Anywhere (v6)

As mentioned in the displayed output, the profile has been activated to grant access to the Apache web server.

Step 3 — Checking your Web Server

Upon completing the installation process, Ubuntu 22.04 automatically starts Apache. Therefore, the web server will already be up and running.

To verify the status of the Apache service using the systemd init system, execute the following command:

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 2022-04-26 15:33:21 UTC; 43s ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 5089 (apache2)
Tasks: 55 (limit: 1119)
Memory: 4.8M
CPU: 33ms
CGroup: /system.slice/apache2.service
├─5089 /usr/sbin/apache2 -k start
├─5091 /usr/sbin/apache2 -k start
└─5092 /usr/sbin/apache2 -k start

Once the service’s successful start is confirmed, the most effective way to test Apache is to request a page from it.
To ensure that Apache is running correctly, you can access the default Apache landing page using your server’s IP address. If you are unsure about your server’s IP address, there are multiple methods to retrieve it from the command line.
You can try the following command at your server’s command prompt:

hostname -I

After retrieving multiple addresses separated by spaces, you can test each of them by entering them into your web browser to check if they work.

Alternatively, you can utilize the free tool called icanhazip.com. This website, when accessed, provides your machine’s public IP address as detected from another location on the internet.

You can use the following command in your server’s command prompt:

curl -4 icanhazip.com

Executing the above command will display your machine’s public IP address.

Once you have obtained your server’s IP address, enter it into your browser’s address bar using the following format:

http://your_server_ip

By accessing the above URL, you will be able to view the default Ubuntu 22.04 Apache web page, similar to the example shown below:

[Example Apache web page screenshot]

Apache page indicates proper functioning, provides basic info on important files/directories. Page confirms Apache is working, includes details on vital files and directories.

Step 4 — Managing the Apache Process

Now that your web server is up and running, let’s go through some essential management commands using systemctl.

To halt the web server, execute the following command with sudo:

sudo systemctl stop apache2

To start the web server if it is stopped, use the following command:

sudo systemctl start apache2

For stopping and then starting the service again, run the command:

sudo systemctl restart apache2

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

sudo systemctl reload apache2

By default, Apache is configured to start automatically upon server boot. If you wish to change this behavior, disable it with the command:

sudo systemctl disable apache2

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

sudo systemctl enable apache2

Apache will now automatically start upon server boot.

Step 5 — Setting Up Virtual Hosts (Recommended)

When utilizing the Apache web server, virtual hosts can be used to host multiple domains from a single server. In this guide, we will set up a domain named “your_domain” (replace with your own domain). Follow these steps:

Create a directory for your_domain:

sudo mkdir /var/www/your_domain

Assign ownership of the directory to the current user:

sudo chown -R $USER:$USER /var/www/your_domain

Set appropriate permissions for the web root:

sudo chmod -R 755 /var/www/your_domain

Create a sample index.html page:

sudo nano /var/www/your_domain/index.html

Add the following HTML code:

<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.

Create a virtual host configuration file:

sudo nano /etc/apache2/sites-available/your_domain.conf

Add the following configuration block:

<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.

Enable the virtual host:

sudo a2ensite your_domain.conf

Disable the default site:

sudo a2dissite 000-default.conf

Test for configuration errors:

sudo apache2ctl configtest

Restart Apache to apply the changes:

sudo systemctl restart apache2

Now, Apache will serve your domain. Access http://your_domain in a web browser to verify that the appropriate content is displayed.

Leave A Comment

To Top