208.1 Basic Apache configuration (weight: 4)

Tech Tutorial: 208.1 Basic Apache Configuration #

Introduction #

Apache HTTP Server is one of the most widely-used web server software in the world. It plays a crucial role in serving web content and managing web server tasks. In this tutorial, we will cover the basics of Apache configuration which is essential for setting up websites and applications on the internet. By the end of this guide, you will have a foundational understanding of how to configure Apache to serve web content, handle various domains, and optimize performance.

Exam Objective #

The objective of this tutorial is to provide detailed insights into basic Apache configuration, focusing on practical, real-world examples and usage.

Key Knowledge Areas:

  • Configuring Apache to serve multiple websites
  • HTTPS configuration and SSL/TLS management
  • Basic performance tuning
  • Access control and authentication

Utilities:

  • apache2ctl
  • .htaccess
  • httpd.conf
  • ssl.conf
  • mod_ssl

Step-by-Step Guide #

1. Installing Apache #

Before diving into configurations, ensure that Apache is installed on your Linux system. Here’s how you can install Apache on a Debian-based system:

sudo apt update
sudo apt install apache2

For Red Hat-based systems, use:

sudo yum update
sudo yum install httpd

2. Configuring Apache to Serve Multiple Websites #

Apache allows you to serve multiple websites from the same server using virtual hosts. Each website will have its own configuration file.

Example: Create two virtual hosts for example.com and testsite.com.

  • Navigate to the configuration directory and create virtual host files:
cd /etc/apache2/sites-available/
sudo touch example.com.conf
sudo touch testsite.com.conf
  • Edit example.com.conf:
<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
  • Similarly, configure testsite.com.conf.

  • Enable the sites:

sudo a2ensite example.com.conf
sudo a2ensite testsite.com.conf
  • Reload Apache to apply changes:
sudo systemctl reload apache2

3. HTTPS Configuration and SSL/TLS Management #

To secure your website using HTTPS, SSL/TLS certificates are required. Let’s configure SSL for example.com.

  • Enable SSL module and the default SSL site:
sudo a2enmod ssl
sudo a2ensite default-ssl
  • Edit default-ssl.conf to point to your domain and SSL certificates:
<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerAdmin webmaster@example.com
        ServerName example.com
        DocumentRoot /var/www/example.com
        
        SSLEngine on
        SSLCertificateFile      /path/to/your/certificate.crt
        SSLCertificateKeyFile   /path/to/your/private.key
        SSLCertificateChainFile /path/to/your/chainfile.pem
    </VirtualHost>
</IfModule>
  • Reload Apache to apply SSL configuration:
sudo systemctl reload apache2

4. Basic Performance Tuning #

Modify Apache’s configuration to improve performance. Edit httpd.conf:

<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    MaxRequestWorkers    150
    MaxConnectionsPerChild  3000
</IfModule>

5. Access Control and Authentication #

Restrict access to a directory using .htaccess:

AuthType Basic
AuthName "Restricted Area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user

Create a password file using htpasswd:

sudo htpasswd -c /etc/apache2/.htpasswd username

Conclusion #

Configuring Apache correctly is crucial for ensuring that your web server performs well and remains secure. This guide has covered the basic aspects of Apache configuration, including serving multiple websites, securing connections with SSL, performance tuning, and setting up basic access control. With these configurations, you should have a solid foundation to build upon for more advanced Apache management tasks.