How to Install and Configure Apache on Ubuntu

This comprehensive guide provides a straightforward, step-by-step walkthrough for installing and configuring the Apache HTTP server on an Ubuntu Linux system. You will learn how to update your package index, install the Apache service, adjust firewall settings to allow web traffic, manage the server’s background processes, and set up a basic virtual host to serve multiple domains. By the end of this article, you will have a fully functioning, secure web server ready to host your website content.

Prerequisites and Preparing Your System

Before initiating the installation process, ensure you have a server running Ubuntu along with a non-root user account configured with sudo privileges. It is vital to update your local package index to guarantee that you are downloading the most recent version of the Apache software and its dependencies.

To refresh your package lists, open your terminal window and execute the following command:

sudo apt update

Installing the Apache Web Server

With the package index updated, you can proceed to download and install Apache using Ubuntu’s default package manager, apt. The Apache package is maintained within Ubuntu’s official repositories, making the installation highly secure and straightforward.

Run the following command to begin the installation:

sudo apt install apache2

During this process, the system will calculate the required disk space and prompt you for confirmation. Press Y and then Enter to allow the installation to finish.

Configuring the UFW Firewall

Before you can access your web server from a browser, you must modify your Uncomplicated Firewall (UFW) settings to allow external access to the default web ports. Apache registers several different application profiles with UFW upon installation.

To view the available Apache firewall profiles, type:

sudo ufw app list

You will see output similar to this:

It is recommended to enable the most restrictive profile that still allows your intended traffic. For a basic setup without an SSL certificate, enable the ‘Apache’ profile using this command:

sudo ufw allow 'Apache'

You can verify the change by checking the firewall status:

sudo ufw status

Verifying the Web Server Status

Once the installation is complete and the firewall is configured, Ubuntu automatically starts the Apache service. You can check the status of the background process to ensure it is actively running by typing:

sudo systemctl status apache2

The output should indicate that the service is “active (running)”. To double-check functionality through a web browser, navigate to your server’s public IP address. If you do not know your IP address, you can find it by typing:

hostname -I

Type your server’s IP address into your web browser’s address bar. You should see the default Ubuntu Apache web page, which serves as a confirmation that the server is operating correctly.

Managing the Apache Process

Now that your web server is functional, you can manage its operation using basic management commands via systemctl. These commands allow you to stop, start, restart, or reload the server when making configuration adjustments.

Setting Up a Virtual Host

When using the Apache web server, you can use virtual hosts to encapsulate configuration details and host more than one domain from a single server instance. We will set up a domain called example.com, but you should replace this with your own actual domain name.

Apache on Ubuntu has one server block enabled by default, configured to serve documents from the /var/www/html directory. We will leave that intact and create a separate directory structure for our new site.

1. Create the Directory Structure

Create the directory for example.com, using the -p flag to create any necessary parent directories:

sudo mkdir -p /var/www/example.com/public_html

Assign ownership of the directory to your current non-root user so you can modify the files without sudo:

sudo chown -R $USER:$USER /var/www/example.com/public_html

2. Create a Sample Page

Create a basic index.html page using your preferred text editor to test the virtual host configuration:

nano /var/www/example.com/public_html/index.html

Inside the file, add the following simple HTML code:

<html>
    <head>
        <title>Welcome to Example.com!</title>
    </head>
    <body>
        <h1>Success! The example.com virtual host is working!</h1>
    </body>
</html>

Save and close the file when you are finished.

3. Create the Virtual Host File

Instead of modifying the default configuration file directly, create a new configuration file for your custom site within the Apache directory structure:

sudo nano /etc/apache2/sites-available/example.com.conf

Paste in the following configuration block, updating the domain names and paths to match your setup:

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file.

4. Enable the Virtual Host File

Enable your new virtual host file using the a2ensite tool:

sudo a2ensite example.com.conf

Next, disable the default site configuration file to prevent it from overriding your custom domain settings if necessary:

sudo a2dissite 000-default.conf

Test your configuration file for any syntax or structural errors:

sudo apache2ctl configtest

The output should read Syntax OK. If any errors are reported, review your configuration text for typos. Finally, restart Apache to apply your changes completely:

sudo systemctl restart apache2

Your Apache web server is now completely installed, configured, and capable of serving your dedicated web traffic on Ubuntu.