Skip to content

Apache2 Installation and Configuration User Manual

Overview

Apache2 is one of the most popular and versatile web servers available. This guide provides a concise walkthrough for installing and configuring Apache2 on a Linux system. It assumes a basic understanding of Linux commands and system administration.

Table of Contents

  1. Prerequisites
  2. Installation
  3. Basic Configuration
  4. Managing Apache2 Service
  5. Testing the Installation
  6. Troubleshooting
  7. Useful Commands and Files

1. Prerequisites

  • A Linux-based operating system (e.g., Ubuntu, Debian )
  • A user with sudo privileges
  • Basic knowledge of Linux commands and file editing

2. Installation

sudo apt update
sudo apt install apache2

3. Basic Configuration

Configuration Files

  • Main Configuration File: /etc/apache2/apache2.conf
  • Virtual Hosts Configuration: /etc/apache2/sites-available/
  • Additional Configuration Files: /etc/apache2/conf-available/

Key Configuration Directives

  • ServerName: Set the hostname of your server. apache ServerName www.example.com
  • DocumentRoot: Define the directory from which Apache will serve files. apache DocumentRoot /var/www/html
  • Directory Permissions: Configure permissions for directories. apache <Directory /var/www/html> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>

Virtual Hosts

Virtual Hosts allow you to run multiple websites on a single server.

Create a Virtual Host Configuration File:

  1. Create a new file in /etc/apache2/sites-available/

bash sudo nano /etc/apache2/sites-available/example.com.conf

  1. Add the following configuration:

```apache ServerAdmin webmaster@example.com ServerName example.com DocumentRoot /var/www/example.com

   <Directory /var/www/example.com>
       Options Indexes FollowSymLinks
       AllowOverride All
       Require all granted
   </Directory>

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

```

  1. Enable the Virtual Host (Ubuntu/Debian):

bash sudo a2ensite example.com.conf

  1. Restart Apache2 to apply changes:

bash sudo systemctl restart apache2

4. Managing Apache2 Service

Start, Stop, and Restart Apache2

sudo systemctl start apache2     # Start Apache
sudo systemctl stop apache2      # Stop Apache
sudo systemctl restart apache2   # Restart Apache
sudo systemctl reload apache2    # Reload configuration without restarting

Check Apache2 Status

sudo systemctl status apache2

5. Testing the Installation

  1. Check Apache2 Status: Ensure Apache2 is running. bash sudo systemctl status apache2

  2. Access Default Page: Open a web browser and navigate to http://localhost. You should see the Apache2 default welcome page.

  3. Check Configuration Syntax: Ensure your configuration files are correctly formatted. bash sudo apache2ctl configtest

6. Troubleshooting

  • Logs: Check Apache2 logs for errors.
  • Access Log: /var/log/apache2/access.log
  • Error Log: /var/log/apache2/error.log

  • Permissions: Ensure the Apache2 user has appropriate permissions to access the directories.

  • Firewall: Ensure that your firewall allows HTTP/HTTPS traffic. bash sudo ufw allow 'Apache Full'

7. Useful Commands and Files

  • View Apache2 Version: bash apache2 -v

  • List Enabled Modules (Ubuntu/Debian): bash sudo apache2ctl -M

  • Manage Modules:

  • Enable a Module: bash sudo a2enmod module_name
  • Disable a Module: bash sudo a2dismod module_name

  • Configuration Test: bash sudo apache2ctl configtest

  • Reload Apache2 (to apply configuration changes): bash sudo systemctl reload apache2