Introduction

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

Apache Web Server is a software package that turns a computer into an HTTP server. That is, it sends web pages – stored as HTML files – to people on the internet who request them. It is open-source software, which means it can be used and modified freely.

How to Install Apache on Ubuntu 18.04

Prerequisites

  • A system running Ubuntu 18.04 LTS (Bionic Beaver)
  • An internet connection
  • Access to a user account with sudo privileges (sudo allows temporary administrator privileges, once you provide your password)

Tools / Software

  • A command-line utility (Use keyboard shortcut CTRL-ALT-T, or right-click the desktop and left-click Open Terminal).
  • A firewall – the default UFW (Uncomplicated Firewall) in Ubuntu is fine
  • The APT package manager, installed by default in Ubuntu

How to Install Apache On Ubuntu

Before installing new software, it’s a good idea to refresh your local software package database to make sure you are accessing the latest versions. This helps cut down on the time it takes to update after installation, and it also helps prevent zero-day exploits against outdated software.

Open a terminal and type:

sudo apt-get update

Let the package manager finish updating.

Step 1: Install Apache

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

sudo apt-get install apache2

The system prompts for confirmation – do so, and allow the system to complete the installation.

Step 2: Verify Apache Installation

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

http://local.server.ip

The web browser should open a page labeled “Apache2 Ubuntu Default Page,” as in the image below:

Apache Ubuntu Default Page


Replace ‘local.server.ip’ with the IP address of your server. If you don’t have it, you can use https://whatismyipaddress.com/ or https://www.whatismyip.com/ to find the IP address of the system you’re working on.


Step 3: Basic Apache Service Controls

When managing a web server, it’s helpful to have some level of control over the service. You’ll probably find yourself reloading or restarting Apache quite frequently, as you make configuration changes and test them. However, it’s also helpful to be able to stop (and start) the Apache service as needed.

This operation uses the systemctl command, with a series of switches:

      • 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

It’s best to run only one of these commands at a time.


Step 4: Apache Files, Directories and Modules

Now that you have Apache installed, there are a couple of other things you will need to be aware of to make content available. Most of all, this means dealing with directories and configuration files.

Directories

After installing, Apache by default creates a document root directory at /var/www/html. 

Any HTML files that you place into this directory are available to Apache to distribute over the network. Which means, this is the place where you copy the web page files you want to publish.

Configuration Files

Ubuntu server admins store the content for their websites in the directory /var/www/html/ You can create subdirectories within this location for each different website hosted on your server.

Apache creates log files for any errors it generates in the file/var/log/apache2/error.log.

It also creates access logs for its interactions with clients in the file/var/log/apache2/access.log.

Like many Linux-based applications, Apache functions through the use of configuration files. They are all located in the directory/etc/apache2/. Here’s a list of other essential directories:

  • /etc/apache2/apache2.conf – This is the main Apache configuration file and controls everything Apache does on your system. Changes here affect all the websites hosted on this machine.
  • /etc/apache2/ports.conf – The port configuration file. You can customize the ports Apache monitors using this file.  By default, Port 80 is configured for Http traffic.
  • /etc/apache2/sites-available – Storage for virtual host files. A virtual host is a record of one of the websites hosted on the server.
  • /etc/apache2/sites-enabled – This directory holds websites that are ready to serve clients. The a2ensite command is used on a virtual host file in the sites-available directory to add sites to this location.

There are many 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.

Modules

If you intend to work with software modules – applications that expand or enhance the functionality of Apache – you can enable them using:

sudo a2enmod name_of_module

To disable the module:

sudo a2dismod name_of_module

Step 5: Configure Your Firewall

Although the installation process is complete, there is one more additional step. It is a good idea to configure your firewall to allow users to connect to your server.

To start configuring the default UFW, you start by displaying the current status. Type the following in a terminal:

sudo ufw status verbose

The terminal should respond by listing its status. Usually, it is set to deny incoming traffic, and allow outgoing.

Use the following command to allow normal web traffic:

sudo ufw allow http

If you have other applications or services to allow, make sure you configure your firewall to allow traffic. For example, using the sudo ufw allow ssh command will enable secure, encrypted logins over the network.

Glossary

      • UFW – Uncomplicated Firewall, a software application that blocks network traffic (usually for security)
      • SSH – Secure Shell, used for encrypted logins over a network
      • APT – Ubuntu’s default package manager, used for installing and updating software packages
      • GUI – Graphical User Interface – the “point and click” interface of the operating system

Conclusion

This tutorial helped you install Apache on Ubuntu using a set of simple commands. In addition, you now know host to configure basic settings for your installation.