How To Install the Apache Web Server on Ubuntu

February 20, 2024

Introduction

The Apache HTTP server is an open-source web server and one of the main components in a LAMP stack. This software package is reliable and versatile when serving web content.

This guide will help you install the Apache web server on Ubuntu Linux.

How to install the Apache Web Server on Ubuntu - a tutorial.

Prerequisites

Apache Installation on Ubuntu

In this tutorial, we will install Apache using apt, the default package manager for Ubuntu. Follow the steps in the sections below to install and set up your Apache web server.

Step 1: Update Package Repository

Update the local software package repository to ensure you install the latest program version. This helps reduce the time it takes to update after installation and prevents zero-day exploits against outdated software.

Open a terminal window and run the following command:

sudo apt update

Wait for the package manager to finish updating.

Step 2: Install Apache

To install the Apache package on Ubuntu, use this command:

sudo apt install apache2

When prompted to confirm the installation, type y and press Enter. Wait until the installation completes.

Installing Apache on Ubuntu using the CLI.

Verify that the Apache service is running:

sudo systemctl status apache2
Checking the Apache service status.

The output shows that the service has started successfully. Refer to the commands below to learn some important Apache service controls, such as how to stop (and start) the Apache service.

Stop Apache:

sudo systemctl stop apache2.service

Start Apache:

sudo systemctl start apache2.service

Restart Apache:

sudo systemctl restart apache2.service

Reload Apache:

sudo systemctl reload apache2.service

Step 3: Configure Your Firewall

After completing the Apache installation, configure the firewall to allow traffic on port 80 to function properly. In this tutorial, we will use UFW. Follow the steps below:

1. Start by displaying available app profiles on UFW:

sudo ufw app list

The terminal outputs all the available application profiles:

Checking available app profiles in UFW.

There are three Apache profiles available:

  • Apache. Opens only port 80 (regular, unencrypted web traffic).
  • Apache Full. Opens port 80 and port 443 (TLS/SSL encrypted traffic).
  • Apache Secure. Opens only port 443 (TLS/SSL encrypted traffic).

2. Run the following command to allow web traffic on port 80:

sudo ufw allow 'Apache'
Allowing Apache connections through the firewall.

3. Verify the changes by checking UFW status:

sudo ufw status
Checking the UFW status.

Note: At this point, your Apache service on Ubuntu is up and running. If you are familiar with Apache, you may want to set up Apache virtual hosts.

Step 4: Verify Apache Installation

To verify Apache was installed correctly, open a web browser and type in the address bar:

http://[local_server_ip]

Replace [local_server_ip] with the IP address of your server. If you are unsure what the IP address is, run the following command:

hostname -I | awk '{print $1}'

The output returns your server's IP address.

The browser should open a page labeled "Apache2 Default Page", as in the image below:

Verifying Apache installation through the default homepage.

Apache Files and Directories

After installing the Apache web server, it is important to get acquainted with some key files and directories that Apache relies on. The log files help troubleshoot issues with the server, while the configuration files allow you to customize the server.

Following is a breakdown of the key Apache directories and files:

Web Content

  • /var/www/html. This directory stores the web content served by Apache. This is also where you install content management systems, such as WordPress. If necessary, you can modify this path in the main configuration file. You can also create subdirectories within this location for each different website hosted on your server.

Server Configuration

  • /etc/apache2. This directory stores all Apache configuration files.
  • /etc/apache2/apache2.conf. The main configuration file for global server settings. It manages how other configuration files load.
  • /etc/apache2/ports.conf. A configuration file that specifies listening ports. By default, Apache listens on ports 80 (HTTP) and 443 (HTTPS) if SSL is enabled.

Virtual Hosts

  • /etc/apache2/sites-available. This directory stores per-site virtual host configuration files.
  • /etc/apache2/sites-enabled. A directory for symbolic links to virtual host configuration files.

Configuration Fragments

  • /etc/apache2/conf-available and /etc/apache2/conf-enabled. Similar to sites-* directories, but for general configuration snippets.

Modules

  • /etc/apache2/mods-available and /etc/apache2/mods-enabled. These directories hold available and enabled modules. .load files activate modules, while .conf files configure them.

If you intend to work with software modules, you can enable them using the following syntax:

sudo a2enmod module_name

To disable a module, use:

sudo a2dismod module_name

Server Logs:

  • /var/log/apache2/access.log. Log file that records all web server requests.
  • /var/log/apache2/error.log. Log file that logs all server errors.

There are many other directories and configuration files, which are detailed in the Apache Ubuntu documentation. These can be used to add modules to enhance Apache's functionality or to store additional configuration information.

Conclusion

This tutorial showed how to install Apache on Ubuntu using a set of simple commands and configure the basic settings for your installation.

Next, read our detailed comparison of Apache and Nginx, or learn to deploy Apache on Docker.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Set Up Apache Virtual Hosts on Ubuntu 18.04
September 26, 2019

Name-based virtual hosts allow you to have a number of domains with the same IP address. This article...
Read more
How to Generate SSH Keys on Ubuntu 18.04
July 29, 2019

Establishing a connection with a remote server without taking the proper security measures can lead to severe...
Read more
How to Start, Stop, or Restart Apache Server on Ubuntu
April 17, 2019

Apache is part of the popular LAMP stack of software. It is included with Ubuntu 18.04 by default. This...
Read more
How to Install MySQL 8.0 in Ubuntu 18.04
December 12, 2018

MySQL is an open-source relational database server tool for Linux operating systems. It is widely used in...
Read more