Linux

How to configure the Apache Web server on Linux step by step?

Step-by-Step Guide to Configure Apache Web Server on Linux

Configuring the Apache web server on Linux allows you to serve websites efficiently. Follow these steps carefully to ensure a successful installation and configuration on any modern Linux distribution.

Required Prerequisites

  1. User Access: It’s essential to log in as a non-root user with sudo privileges to ensure security and proper permissions during installation.

Step 1: Install Apache

Begin the installation process by accessing your terminal and executing the following command, which utilizes the built-in package manager:

bash
sudo apt update
sudo apt install apache2

This command updates your package lists and downloads the Apache web server from the default repositories.

Step 2: Adjust Firewall Settings

After installation, ensure that your firewall settings allow traffic on the web server’s default ports (80 for HTTP and 443 for HTTPS). Use the following commands to allow web traffic through the firewall:

bash
sudo ufw allow ‘Apache Full’

You can check the status of the firewall with:

bash
sudo ufw status

Step 3: Verify Apache Installation

To confirm that Apache is running correctly, access your web browser and enter the server’s ip address or http://localhost. If the installation was successful, you should see the default Apache welcome page.

You can also check the service status in the terminal by running:

bash
sudo systemctl status apache2

Step 4: Manage Apache Processes

You can control Apache’s operation using the systemctl command. Here are some commonly used commands:

  • Start Apache:
    bash
    sudo systemctl start apache2

  • Stop Apache:
    bash
    sudo systemctl stop apache2

  • Restart Apache:
    bash
    sudo systemctl restart apache2

These commands manage the Apache service, allowing you to start, stop, and restart the server as needed.

Step 5: Familiarize Yourself with Apache Configuration

Understanding the important directories and configuration files is crucial for effective management:

  • Main Configuration File: Located at /etc/apache2/apache2.conf, this is the primary configuration file where server settings are defined.
  • Available Sites: The directory /etc/apache2/sites-available/ contains configuration files for individual sites that you may want to host.
  • Enabled Sites: The symbolic links are located in the /etc/apache2/sites-enabled/ directory.

Step 6: Enable Necessary Apache Modules

Apache can be extended with various modules. To enable a specific module, such as rewrite:

bash
sudo a2enmod rewrite

Always remember to restart Apache after making changes:

bash
sudo systemctl restart apache2

Step 7: set up Virtual Hosts

Creating virtual hosts allows you to host multiple websites on one server. Here’s a simple configuration example for a domain named example.com.

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

  2. Add the following content:
    apache
    <VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example.com/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

  3. Enable the new virtual host:
    bash
    sudo a2ensite example.com.conf

  4. Restart Apache once more:
    bash
    sudo systemctl restart apache2

Frequently Asked Questions

1. How can I check the version of Apache installed?
You can verify the installed version of Apache with the following command:
bash
apache2 -v

2. What should I do if Apache doesn’t start?
If Apache fails to start, review the logs for error messages. Run:
bash
sudo journalctl -xe

This will show detailed logs that can help diagnose the issue.

3. Is it possible to run multiple versions of Apache on the same server?
Running multiple versions of Apache simultaneously is complex and not typically supported. It is advisable to use one version at a time or employ containerization technologies.

About the author

Daniel Whitaker

Daniel Whitaker

Daniel Whitaker is a Linux specialist, open-source advocate, and technical writer with over 15 years of experience working with Linux systems and infrastructure. He has contributed numerous articles and tutorials across various technology websites, focusing on Linux administration, shell scripting, system optimization, and open-source tools. Known for his clear explanations and practical guides, Daniel enjoys helping developers and system administrators better understand and master the Linux ecosystem.