Articles on: Dedicated Servers / VPS

How To Install the Apache Web Server on Ubuntu

The Apache HTTP server stands as the most commonly utilized web server globally. It offers numerous impressive capabilities such as the ability to load modules dynamically, strong support for various media, and extensive compatibility with other widely-used software.

Note: This Article was created using Ubuntu 20.04 as the Operating System.


Ensure that the local package index on the server is updated to reflect the latest upstream changes by running the command sudo apt update.
Install UFW, or Uncomplicated Firewall for configuring the Firewall. Usually it is already installed on Ubuntu but incase it has been uninstalled previously, you can install it by running the command sudo apt install ufw.
Install Apache2 by running the command sudo apt install apache2.

Adjusting the Firewall

Prior to conducting tests on Apache, it's imperative to adjust the firewall configurations to permit external entry to the standard web ports. Assuming that you followed the instructions in the prerequisites, you should have a UFW firewall configured to restrict access to your server.

Throughout the installation process, Apache integrates with UFW to furnish several application profiles, facilitating the activation or deactivation of access to Apache via the firewall.

List the UFW application profiles by entering the command sudo ufw app list.

It should return a list of application profiles like the following:
Available applications:
  Apache Full
  Apache Secure

As indicated by the returned result, there are three profiles available for Apache:
Apache: This profile opens only port 80 (normal, unencrypted web traffic)
Apache Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic)

It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Since we haven’t configured SSL for our server yet in this guide, we will only need to allow traffic on port 80 which you can do by running the command sudo ufw allow 'Apache'.

You can verify if the change has been made by running the command sudo ufw status. It should return a list of allowed HTTP traffic like the following example:
Status: active

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

As indicated by the returned result, the profile has been activated to allow access to the Apache Web Server.

Checking your Web Server

At the end of the installation process, The server starts Apache. The web server should already be up and running.

Check with the systemd init system to make sure the service is running by entering sudo systemctl status apache2. It should return with a message like this:
● apache2.service - The Apache HTTP Server

     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2024-03-14 18:38:12 UTC; 18h ago
     Main PID: 29435 (apache2)
     Tasks: 55 (limit: 1137)
     Memory: 8.0M
     CGroup: /system.slice/apache2.service
          ├─29435 /usr/sbin/apache2 -k start
          ├─29437 /usr/sbin/apache2 -k start
          └─29438 /usr/sbin/apache2 -k start

As confirmed by this output, the service has started successfully. However, the best way to test this is to request a page from Apache.

You can access the default Apache landing page to confirm that the software is running properly through your IP address. If you do not know your server’s IP address, you can get it a few different ways from the command line. Try entering the command hostname -I, You will then get back a few addresses separated by spaces. You can try each in your web browser by going to http://<Your Server IP> to determine if they work. You should see the default Ubuntu Apache web page:
Default Ubuntu Apache Web Page

This page indicates that Apache is working correctly. It also includes some basic information about important Apache files and directory locations.

Managing the Apache Process

Now that you have your web server up and running, let’s go over some basic management commands using systemctl.

To stop your web server, enter sudo systemctl stop apache2.
To start the web server when it is stopped, enter sudo systemctl start apache2.
To stop and then start the service again, enter sudo systemctl restart apache2.
If you are simply making configuration changes, Apache can often reload without dropping connections. To do this, enter the command sudo systemctl reload apache2.
By default, Apache is configured to start automatically when the server boots. If this is not what you want, disable this behavior by entering sudo systemctl disable apache2.
To re-enable the service to start up at boot, enter sudo systemctl enable apache2. Apache should now start automatically when the server boots again.

Setting Up Virtual Hosts (Recommended)

When using the Apache web server, you can use virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server. We will set up a domain called, but you should replace this with your own domain name.

Apache on Ubuntu has one server block enabled by default that is configured to serve documents from the /var/www/html directory. While this works well for a single site, it can become unwieldy if you are hosting multiple sites. Instead of modifying /var/www/html, let’s create a directory structure within /var/www for a site, leaving /var/www/html in place as the default directory to be served if a client request doesn’t match any other sites.

Create the directory for your_domain by entering the command sudo mkdir /var/www/

Next, assign ownership of the directory with the $USER environment variable by entering the command sudo chown -R $USER:$USER /var/www/

The permissions of your web roots should be correct if you haven’t modified your umask value, which sets default file permissions. To ensure that your permissions are correct and allow the owner to read, write, and execute the files while granting only read and execute permissions to groups and others, you can enter the command sudo chmod -R 755 /var/www/

Next, create a sample index.html page using nano or your favorite editor. If you want to use nano, enter the command sudo nano /var/www/your_domain/index.html. Once inside, we can add some HTML code like the following:
        <title>Welcome to your-domain!</title>
        <h1>Success!  The virtual host is working!</h1>

Save and close the file when you are finished.

In order for Apache to serve this content, it’s necessary to create a virtual host file with the correct directives. Instead of modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf directly, let’s make a new one at /etc/apache2/sites-available/your-domain.conf by entering the command sudo nano /etc/apache2/sites-available/your-domain.conf.

Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain name:
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/your-domain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Notice that we’ve updated the DocumentRoot to our new directory and ServerAdmin to an email that the your-domain site administrator can access. We’ve also added two directives: ServerName, which establishes the base domain that should match for this virtual host definition, and ServerAlias, which defines further names that should match as if they were the base name.

Save and close the file when you are finished.

Let’s enable the file with the a2ensite tool by entering the command sudo a2ensite your_domain.conf and disable the default site defined in 000-default.conf with the command sudo a2dissite 000-default.conf.

Next, let’s test for configuration errors by entering the command sudo apache2ctl configtest, It should return with the phrase Syntax OK.

After that, Restart Apache to implement your changes by entering the command sudo systemctl restart apache2. Apache should now be serving your domain name. You can test this by navigating to, where you should see something like this:

Getting Familiar with Important Apache Files and Directories

Now that you know how to manage the Apache service itself, you should take a few minutes to familiarize yourself with a few important directories and files.


/var/www/html: The actual web content, which by default only consists of the default Apache page you saw earlier, is served out of the /var/www/html directory. This can be changed by altering Apache configuration files.

Server Configuration

/etc/apache2: The Apache configuration directory. All of the Apache configuration files reside here.
/etc/apache2/apache2.conf: The main Apache configuration file. This can be modified to make changes to the Apache global configuration. This file is responsible for loading many of the other files in the configuration directory.
/etc/apache2/ports.conf: This file specifies the ports that Apache will listen on. By default, Apache listens on port 80 and additionally listens on port 443 when a module providing SSL capabilities is enabled.
/etc/apache2/sites-available/: The directory where per-site virtual hosts can be stored. Apache will not use the configuration files found in this directory unless they are linked to the sites-enabled directory. Typically, all server block configuration is done in this directory, and then enabled by linking to the other directory with the a2ensite command.
/etc/apache2/sites-enabled/: The directory where enabled per-site virtual hosts are stored. Typically, these are created by linking to configuration files found in the sites-available directory with the a2ensite. Apache reads the configuration files and links found in this directory when it starts or reloads to compile a complete configuration.
/etc/apache2/conf-available/, /etc/apache2/conf-enabled/: These directories have the same relationship as the sites-available and sites-enabled directories, but 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.
/etc/apache2/mods-available/, /etc/apache2/mods-enabled/: These directories contain the available and enabled modules, respectively. Files ending in .load contain fragments to load specific modules, while files ending in .conf contain the configuration for those modules. Modules can be enabled and disabled using the a2enmod and a2dismod command.

Server Logs

/var/log/apache2/: This directory should contain at least 2 files: access.log, wherein default, every request to your web server is recorded in this log file unless Apache is configured to do otherwise, and error.log, where by default, all errors are recorded in this file. The LogLevel directive in the Apache configuration specifies how much detail the error logs will contain.

If you require any further assistance, reach out to us via live chat or by creating a ticket!

Created by: Alecz R.

Updated on: 25/03/2024

Was this article helpful?

Share your feedback


Thank you!