Kahibaro
Discord Login Register

5.1.1 Apache basics

Overview of Apache HTTP Server

Apache HTTP Server (often just "Apache") is one of the most widely used web servers. In this chapter, the focus is on:

You will see Apache again in later chapters (e.g. for SSL/HTTPS and as a reverse proxy). Here we stay at a practical, foundational level.


Installing Apache

Package names differ slightly between distributions:

Typical installation commands:

  sudo apt update
  sudo apt install apache2
  sudo dnf install httpd
  sudo zypper install apache2

After installation, the main service name is usually:

Managing the Apache Service

Use systemctl to start, stop, and enable Apache at boot. Adjust apache2 vs httpd according to your distro.

  sudo systemctl start apache2        # or httpd
  sudo systemctl stop apache2
  sudo systemctl restart apache2
  sudo systemctl reload apache2
  sudo systemctl enable apache2
  systemctl status apache2

If Apache fails to start or reload, status output and logs (see later) are your first checks.


Default Document Root and Test Page

After installation and service start, point your browser to:

Common default document roots:

You can test Apache by creating a simple page:

echo 'Hello from Apache' | sudo tee /var/www/html/index.html

Then load http://localhost/ again. You should see that text.


Apache Configuration Layout

Exact paths differ by distribution, but the general ideas are consistent.

Main Configuration File

This file usually:

Avoid putting everything in this file. Instead, use the distro’s layout (sites, conf.d, etc.).

Included Directories

Common patterns:

Always check apache2ctl -M or httpd -M and main config for Include / IncludeOptional lines to understand what is actually loaded.


Basic Configuration Directives

Apache configuration is directive‑based. A directive usually has the form:

DirectiveName value1 value2 ...

Basic, commonly seen directives:

  Listen 80
  Listen 0.0.0.0:80
  Listen [::]:80
  ServerName www.example.com
  DocumentRoot /var/www/html

Directives often appear inside configuration blocks like <VirtualHost> or <Directory>, which limit their scope.


The `apachectl` / `apache2ctl` Utility

Apache provides a control script that can test configurations and control the server. The name depends on the distro:

Useful commands:

  sudo apache2ctl configtest

or

  sudo apachectl configtest

This should output Syntax OK if there are no parsing errors.

  apache2ctl -M
  # or
  httpd -M

These tools do not replace systemctl, but complement it.


Modules in Apache

Apache’s functionality is mostly provided by modules. Common ones:

Enabling/Disabling Modules (Debian/Ubuntu)

Debian/Ubuntu provide helper tools:

  apache2ctl -M
  sudo a2enmod rewrite
  sudo a2dismod rewrite

Enabling a module usually creates symlinks from mods-available/ to mods-enabled/.

After changing modules, reload or restart:

sudo systemctl reload apache2

Modules on RHEL/Fedora and Others

On RHEL/Fedora and many other distros, modules are loaded via LoadModule directives in:

Example:

LoadModule rewrite_module modules/mod_rewrite.so

To "disable" a module, comment out or remove its LoadModule line and reload Apache.


Basic Site (Virtual Host) Configuration

Multiple websites can be served from a single Apache instance using virtual hosts. Apache supports:

Here we focus on simple name‑based virtual hosts on port 80.

Common Structure of a Virtual Host

A direct <VirtualHost> block might look like:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog  ${APACHE_LOG_DIR}/example_error.log
    CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>

Key points:

NameVirtualHost *:80 is no longer needed in modern Apache 2.4+.

Debian/Ubuntu: `sites-available` and `sites-enabled`

Typical workflow:

  1. Create a new directory for your site content:
   sudo mkdir -p /var/www/example.com/public_html
   sudo chown -R $USER:$USER /var/www/example.com
  1. Create virtual host config file:
   sudo nano /etc/apache2/sites-available/example.com.conf

Example contents:

   <VirtualHost *:80>
       ServerName example.com
       ServerAlias www.example.com
       DocumentRoot /var/www/example.com/public_html
       ErrorLog  ${APACHE_LOG_DIR}/example_error.log
       CustomLog ${APACHE_LOG_DIR}/example_access.log combined
   </VirtualHost>
  1. Enable the site:
   sudo a2ensite example.com.conf
  1. Test config and reload:
   sudo apache2ctl configtest
   sudo systemctl reload apache2

Apache uses symlinks in sites-enabled/ to determine which sites are active.

Disable a site with:

sudo a2dissite example.com.conf
sudo systemctl reload apache2

RHEL/Fedora: `conf.d`

On RHEL/Fedora, you typically place virtual host definitions into /etc/httpd/conf.d/. Example:

sudo nano /etc/httpd/conf.d/example.com.conf

Insert similar <VirtualHost> content, then:

sudo apachectl configtest
sudo systemctl reload httpd

Directory and Access Control Basics

Apache has directives that control how it serves specific directories and URLs. Two common approaches:

`<Directory>` Blocks

<Directory> targets filesystem paths. For example, to allow .htaccess to override certain settings:

<Directory /var/www/example.com/public_html>
    AllowOverride All
    Require all granted
</Directory>

Key points:

`.htaccess` Files (Basics)

.htaccess files are per‑directory configuration files read on every request, if allowed by AllowOverride.

Example: simple redirect in .htaccess:

RewriteEngine On
RewriteRule ^old-page$ /new-page [R=301,L]

Use .htaccess only when you cannot edit the main server config (e.g. shared hosting). For performance and clarity, prefer <Directory> blocks in main configs where you have access.


Logs and Simple Troubleshooting

Apache logs are crucial for diagnosing problems.

Default Log Locations

Common defaults:

You’ll usually see:

Example (Debian/Ubuntu):

sudo tail -f /var/log/apache2/error.log

Use this while testing new configs or debugging startup failures.

Reading Errors on Startup

When Apache fails to start or reload:

  1. Run:
   sudo apache2ctl configtest

or

   sudo apachectl configtest
  1. Check error.log for:
    • Syntax errors (missing </VirtualHost>, typos in directives).
    • Port conflicts (e.g. “Address already in use: AH00072”).
    • Missing files/directories (e.g. non‑existent DocumentRoot).

Correct the issue, test again, then reload.


Basic Security and Performance Considerations (High Level)

Full hardening is covered elsewhere, but for Apache basics, be aware of:

  Options -Indexes

Practical Checklist

To recap, a basic Apache setup usually involves:

  1. Install Apache via your system’s package manager.
  2. Start and enable the service with systemctl.
  3. Confirm default site works via browser.
  4. Create a new DocumentRoot for your site.
  5. Create a <VirtualHost> configuration file:
    • Set ServerName, ServerAlias, and DocumentRoot.
    • Optionally define dedicated ErrorLog and CustomLog.
  6. Enable the site (a2ensite on Debian/Ubuntu, or add .conf in conf.d on RHEL/Fedora).
  7. Test configuration (apache2ctl configtest or apachectl configtest).
  8. Reload Apache.
  9. Check logs if anything misbehaves.

These basics provide a foundation upon which later chapters (virtual hosts in depth, SSL/HTTPS, reverse proxy configurations, performance tuning, and security hardening) will build.

Views: 73

Comments

Please login to add a comment.

Don't have an account? Register now!