How to Set Up an Apache Web Server on Linux

Apache is the most powerful, flexible, and widely-used open-source software that serves web content over the internet. The server works as a delivery man by serving content available as HTML files when the client makes any request with the website domain.

Most importantly, web servers, including Apache, support multiple operating systems like Linux, Windows, Solaris, macOS, etc. Hence, you can easily turn a computer into a server hosting multiple websites.

The Apache HTTP server allows loading modules with extensive support for software and application integration. This article details the installation and configuration of an Apache HTTP server in Linux.

Step 1: Install Apache Server on Linux

Before beginning with the Apache installation, you must update the Linux package database from the official repositories. It is a necessary step that helps avoid any security loopholes and includes the latest features to the packages.

Update the system repository list and install Apache using the following commands:

On Ubuntu and Debian:

sudo apt-get update
sudo apt-get install apache -y

On CentOS:

sudo yum update
sudo yum install httpd -y

On Fedora:

sudo dnf update
sudo dnf install httpd -y

To install Apache on Arch Linux, run:

sudo pacman -Syu
sudo pacman -S apache

Note that Ubuntu/Debian distributions refer to Apache as apache2, while CentOS and Fedora refer to it as httpd.

Step 2: Verify Apache Service Status

On Debian-based distributions, the Apache service starts automatically. You can go to the browser and enter your local IP address to access the server’s landing page. If you are unsure about your server’s address, run the hostname -i command to print the details.

hostname -i


The page confirms successful installation.


Alternatively, run the following command to verify the installation:

apache2 -version



If you’re running CentOS, the service won’t start automatically. You can start the service manually by executing the command:

sudo systemctl start httpd

Check the service status, as below:

sudo systemctl status httpd

Step 3: Configure Firewall to Allow Apache Server Access

Another mandatory step in Apache configuration is enabling the UFW firewall in Linux to accept or allow traffic to access the server via default port 80. During installation, the service registers with the firewall with some application profiles. The list of application profiles helps you to enable/disable Apache access.

Use the following command to list all Apache application profiles:

sudo ufw app list


Available applications:
Apache Full
Apache Secure

The available profiles represent:

  1. Apache: Only opens port 80 to enable unencrypted communication over the internet
  2. Apache Full: Opens both ports 80 and 443 for unencrypted and secure communication
  3. Apache Secure: Enables secure server access via HTTPS by allowing traffic on port 443

Since we don’t have SSL/TLS enabled for the server, we will allow UFW access on only port 80 as follows:

sudo ufw allow 'Apache'

Now check the firewall status by running:

sudo ufw status
Apache Firewall Setting

Step 4: Understand Apache Directories and Files

After successful server installation and configuration, every beginner must know how the server manages its websites and their content. The /var/www/html directory manages all the websites you want to host on your server.

By default, the directory contains the web page you have seen earlier. Apache allows you to create different subdirectories in this folder to host multiple websites.

In Ubuntu and Debian-based distributions, the main configuration directory for the Apache server is /etc/apache2, while for CentOS, it’s /etc/httpd. Hence, all the configuration files for the server are available inside these directories. Some of the most known files/directories are:

  • /var/log/apache2/error.log: Logs all the errors encountered
  • /var/log/apache2/access.log: Logs all the access requests made to the server
  • /etc/apache2/sites-available: Directory that contains virtual hosts
  • /etc/apache2/sites-enabled: Stores ready to serve websites per virtual host. It cannot work without linking the configuration file inside the sites-available directory using the a2ensite command.

An Example to Set Up a Virtual Host

Apache server installation creates a default directory of /var/www/html in all Linux distributions. This directory contains all the files for your website, but it cannot work if you want to host multiple websites on the same server.

To serve multiple domains, you can use virtual hosts and create a domain directory inside the /var/www folder, as follows:

sudo mkdir /var/www/host_example

Change the ownership and file permissions of the directory using chown.

sudo chown -R $current_user:$current_user /var/www/host_example
sudo chmod -R 755 /var/www/host_example

Now open the /var/www/host_example/html/content.html file in your favorite editor and copy/paste the following HTML:

<title>Welcome to host_example!</title>
<h1>You are running host_example on Ubuntu 18.04!</h1>

Apache creates a configuration folder that serves as a storage place to contain a record of the virtual hosts. The default configuration file is /etc/apache2/sites-available/000-default.conf. However, you can create a new file according to your domain name and copy/paste the configuration block available in the default file.

Edit the file with a text editor of your choice and update it with your domain name and the new directory as follows:

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

Activate Your Domain Configuration File

The domain configuration file host_example.conf activation requires the use of a2ensite.

sudo a2ensite host_example.conf

The above output displays the requirements for disabling the default configuration file (000-default.conf):

sudo a2dissite 000-default.conf

Now restart the apache service to load the changes.

sudo systemctl restart apache2

Go to the browser and navigate to the domain name to check if it is serving your website:


Test for Configuration Errors

The apache2ctl utility allows you to check for any configuration errors for the Apache server. The following command must return the Syntax OK output to verify the successful no-error configuration:

sudo apache2ctl configtest


Syntax OK

Hosting Multiple Websites on Linux Servers Using Apache

The tutorial above shows the modularity and ease of installing and configuring an Apache server. The versatility of the server allows you to configure the setup and host websites as per your requirement. The use case of the virtual host setup shows how the configuration files work and interact.

You may have also noticed that the specifics/folders may change depending on your Linux distribution and Apache version. Lastly, the Apache management commands are there to manage, start or reload the server services in an optimized way. You can also find some other Linux servers to host your websites on.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button