How to Install Apache Web Server in Linux

Apache Web Server is one of the most popular open-source web server solutions, widely used worldwide for hosting websites. Known for its robustness, flexibility, and compatibility with various operating systems, including Linux, Apache is a reliable choice for web server setups.

Install Apache Web Server in Linux

This guide will walk you through installing the Apache web server on popular Linux distributions like Ubuntu, Debian, CentOS, Fedora, and Arch Linux, whether you’re a beginner or an experienced system administrator.

Prerequisites

Before you begin the installation, there are a few things to keep in mind:

  • You should have a fresh or updated installation of a Linux distribution.
  • Ensure you have sudo or root privileges on the system.
  • Your system’s package manager should be updated to install the latest packages.

Install Apache Web Server on Various Linux Distributions

Apache is one of the most popular web servers that powers websites across the globe. Depending on your Linux distribution, the installation process can differ. In this guide, we’ll walk you through installing Apache Web Server on Ubuntu/Debian, CentOS/RHEL/Fedora, and Arch Linux, ensuring smooth deployment regardless of your system.

Install Apache Web Server on Ubuntu/Debian

For Ubuntu and Debian-based systems, installing Apache is simple. Follow the command below:

  • Update the package list
sudo apt update
  • Install Apache2 package
sudo apt install apache2
  • Verify Apache Installation

Check that Apache is installed and running:

sudo systemctl status apache2

To check the Apache version:

apache2 -v
  • Allow Apache Through the Firewall (UFW)

If you are using UFW (Uncomplicated Firewall) on Ubuntu or Debian, allow Apache traffic:

sudo ufw allow in "Apache Full"

Install Apache Web Server on CentOS/RHEL/Fedora

On CentOS, RHEL, and Fedora, Apache is known as httpd. Here’s how to install it:

  • Update the System and Install Apache

For CentOS/RHEL:

sudo yum install httpd

For Fedora:

sudo dnf install httpd
  • Start and Enable Apache Service

Start the Apache service:

sudo systemctl start httpd

Enable Apache to start on boot:

sudo systemctl enable httpd
  • Check Apache Status

To ensure Apache is running:

sudo systemctl status httpd
  • Configure the Firewall (firewall)

If you are using firewalld, allow HTTP traffic:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

Install Apache Web Server on Arch Linux

Apache installation on Arch Linux follows a similar process.

  • Update the System
sudo pacman -Syu
  • Install Apache
sudo pacman -S apache
  • Start and Enable Apache Service

Start Apache:

sudo systemctl start httpd

Enable Apache on boot:

sudo systemctl enable httpd
  • Check Apache Status

To verify that Apache is running:

sudo systemctl status httpd
  • Configure Firewall

If you’re using ufw or firewalld, allow HTTP traffic on Arch:

  • For ufw: sudo ufw allow in "Apache Full"
  • For firewalld: sudo firewall-cmd --permanent --add-service=http

Check Out | Installing Docker on Ubuntu

Create a new Apache Virtual Host

Create a configuration file specifying your web application’s domain and directory to set up a custom Apache virtual host. This process allows you to disable the default virtual host and configure Apache to listen for incoming requests on your desired domain. Follow the steps below to configure Apache for your custom virtual host setup.

  • Create a New Virtual Host Configuration File

First, create a new Apache virtual host configuration file in the /etc/apache2/sites-available/ directory. For this example, we will name it website.conf. Open the file with a text editor, such as nano:

$ sudo nano /etc/apache2/sites-available/website.conf
  • Add Virtual Host Configuration

In the website.conf file, add the following configuration. Replace app.example.com with your domain name and webmaster@example.com with your web administrator email: apacheconfCopyEdit

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName app.example.com

    DocumentRoot /var/www/html/website
    DirectoryIndex index.html index.php

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /var/www/html/website>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

This configuration tells Apache to:

  • Listen for requests on port 80 (default HTTP port).
  • Use your specified domain (app.example.com).
  • Serve the content from the /var/www/html/website directory.
  • Use index.html or index.php as the default file to serve.
  • Log errors and access requests to specified log files.
  • Apply additional directory-specific settings, such as allowing .htaccess overrides.
  • Save and Close the File

After adding the configuration, save and close the file by pressing CTRL + X, then Y, and finally Enter.

  • Disable the Default Apache Virtual Host

To prevent Apache from using the default virtual host, disable it with the following command:

$ sudo a2dissite 000-default
  • Enable the New Virtual Host Configuration

Enable the new virtual host configuration by running:

$ sudo a2ensite website
  • Test Apache Configuration

Run the following command to test if your Apache configuration contains any errors:

$ sudo apachectl configtest

You should see the output Syntax OK if there are no errors.

  • Create the Web Root Directory

If it doesn’t exist yet, create the web root directory specified in the configuration:

$ sudo mkdir -p /var/www/html/website
  • Add a Sample HTML File

Next, create a simple index.html file in the /var/www/html/website directory:

$ sudo nano /var/www/html/website/index.html

Add the following HTML content to display a “Greetings from YouStable” message:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Apache Web Server</title>
</head>
<body>
    <h1>Greetings from YouStable</h1>
</body>
</html>

Save and close the file.

  • Set Permissions for the Web Root Directory

Grant ownership privileges to the Apache web server user (www-data) for the web root directory:

$ sudo chown -R www-data:www-data /var/www/html/website
  • Restart Apache to Apply Changes

Finally, restart the Apache service to apply the configuration changes:

$ sudo systemctl restart apache2

Your Apache web server is configured to serve a website from your custom virtual host using the domain app.example.com on port 80. When you access this domain, it should display the “Greetings from YouStable” message.

Check Out | How to Easily Install PIP on Ubuntu in 2025

Conclusion

Installing Apache on Linux is straightforward, whether you’re using Ubuntu, Debian, CentOS, Fedora, or Arch Linux. Once installed, you can start configuring Apache to host your websites and applications. From modifying virtual hosts to enabling SSL, Apache provides a wide range of features catering to beginners and advanced users.

Following this guide, you’ve successfully set up Apache on your Linux system. For further customization, check out Apache’s official documentation and explore advanced features like setting up SSL, configuring mod_rewrite, and enabling PHP.

Leave A Comment