How To Install the Apache Web Server on Ubuntu

Apache HTTP server is a widely-used web server with powerful features, including dynamic module loading, robust media support, and seamless integration with popular software, making it an ideal platform for hosting websites and web applications.

Prerequisites

Before you start with this guide on installing the Apache web server on Ubuntu, ensure you have a regular user account with sudo privileges set up on your server. It’s also recommended to enable a basic firewall to protect against non-essential ports. You can follow our Initial server setup guide for Ubuntu to learn how to create a user account and configure a firewall.

Once you have a user account set up, log in as your non-root user to get started with the Apache installation process.

Step 1: Installing Apache

Update System Packages: Make sure your server’s packages are up to date by running the following command.

sudo apt update

Install Apache: Install the Apache web server with the following command.

sudo apt install apache2

Step 2: Adjusting the Firewall

Before testing Apache, adjusting your firewall settings to allow outside access to the default web ports is important. If you’ve followed the prerequisites in this tutorial, you should already have a UFW firewall set up to restrict access to your server.

When you install Apache, it registers itself with UFW and provides several application profiles that allow you to enable or disable access to Apache through the firewall. This makes it easy to manage your firewall settings and control which traffic can access your Apache server.

To see the available UFW application profiles for Apache, type the following command in your terminal:

sudo ufw app list

This will display a list of the available profiles for your Apache installation, which should include:

  • Apache: This profile opens port 80 for normal, unencrypted web traffic.
  • Apache Full: This profile opens port 80 for normal, unencrypted web traffic and port 443 for TLS/SSL encrypted traffic.
  • Apache Secure: This profile only opens port 443 for TLS/SSL encrypted traffic.

Choose the profile that best fits your needs and enable it using the sudo ufw allow command.

Since we haven’t set up SSL for our server yet in this tutorial, we only need to allow traffic on port 80 by running the following command:

sudo ufw allow 'Apache'

After running this command, verify the changes by checking the firewall status:

sudo ufw status

The output should show that Apache traffic is now allowed

Step 3: Checking your Web Server

To check if Apache is running after installation, enter the command:

sudo systemctl status apache2

If the service runs, the output will indicate it’s active.

You can also confirm by accessing the default Apache landing page through your IP address. You can enter the IP address into your web browser.

http://your_server_ip

To find your IP address, you can use the following commands:

  • hostname -I : This will display a list of IP addresses associated with your server.
  • curl -4 icanhazip.com : This will return your public IP address as seen by another location on the internet.

You should see the default Apache web page, confirming that Apache is working correctly.

Step 4: Setting Up Virtual Hosts

You can use virtual hosts to host multiple domains from a single server using Apache. Here are the steps to set up a virtual host for a domain called “example.com” on Ubuntu.

  1. Create a directory for example.com:
sudo mkdir /var/www/example.com
  1. Assign ownership of the directory to the current user:
sudo chown -R $USER:$USER /var/www/example.com
  1. Set the correct permissions for the directory:
sudo chmod -R 755 /var/www/example.com
  1. Create a sample index.html page:
sudo nano /var/www/example.com/index.html

Add the following sample HTML:

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

  1. Create a virtual host file:
sudo nano /etc/apache2/sites-available/example.com.conf

Paste in the following configuration block:

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

Save and close the file.

  1. Enable the virtual host:
sudo a2ensite example.com.conf
  1. Disable the default site:
sudo a2dissite 000-default.conf
  1. Test for configuration errors:
sudo apache2ctl configtest
  1. Restart Apache:
sudo systemctl restart apache2

After following these steps, Apache should be serving example.com. You can test this by navigating to http://example.com in your web browser.

Managing the Apache

These are the basic management commands for Apache using systemctl:

To stop the Apache web server:

sudo systemctl stop apache2

To start the Apache web server:

sudo systemctl start apache2

To restart the Apache web server:

sudo systemctl restart apache2

To reload the Apache web server without dropping connections:

sudo systemctl reload apache2

To disable Apache from starting automatically at boot:

sudo systemctl disable apache2

To enable Apache to start automatically at boot:

sudo systemctl enable apache2

Important Apache Files and Directories

Understanding the directory structure and files Apache uses is important for effectively managing and configuring your web server. Here is a summary of the key directories and files used by Apache:

Content:
/var/www/html: The directory stores the actual web content. This is where you would place your HTML, CSS, and other files that make up your website.

Server Configuration:
/etc/apache2: The directory contains all the Apache configuration files.

/etc/apache2/apache2.conf: This main configuration file loads many other configuration files.

/etc/apache2/ports.conf: This file specifies the ports that Apache will listen on. By default, Apache listens on port 80 for HTTP traffic and port 443 for HTTPS traffic.

/etc/apache2/sites-available: Configuration for each site is done in this directory, and then enabled by linking to the /etc/apache2/sites-enabled directory with the a2ensite command.

/etc/apache2/sites-enabled: This directory contains the enabled virtual hosts.

The /etc/apache2/conf-available and /etc/apache2/conf-enabled directories are used to store configuration fragments that do not belong in a virtual host. Files in the conf-available directory can be enabled with the a2enconf command and disabled with the a2disconf command.

The /etc/apache2/mods-available and /etc/apache2/mods-enabled directories contain the available and enabled modules, respectively. Modules can be enabled and disabled using the commands a2enmod and a2dismod.

Server Logs:
/var/log/apache2/access.log: This file records every request to your web server.

/var/log/apache2/error.log: This file records all errors. The LogLevel directive in the Apache configuration file specifies how much detail the error logs will contain.