How to Configure Apache to Serve a Static Site on Debian 12 Bookworm

This article provides a step-by-step guide on how to configure Apache to serve a static site on Debian 12 Bookworm.

Apache HTTP Server, or simply Apache, is one of the most widely used web servers in the world. Known for its stability, flexibility, and wide-ranging features, Apache is an ideal choice for hosting websites of all sizes, including lightweight static sites. If you’re running Debian 12 Bookworm and want to deploy a static website using Apache, this guide will walk you through the process from start to finish.

Whether you’re setting up a personal portfolio, a documentation site, or a small business landing page, a static site is simple, fast, and easy to maintain. In this tutorial, we will explore how to install Apache, configure it properly, and serve your static website efficiently on a Debian 12 Bookworm system.


Prerequisites

Before you begin, make sure you have the following:

  • A Debian 12 Bookworm server with a non-root user and sudo privileges.
  • Basic familiarity with the Linux command line.
  • A static website ready to be deployed (e.g., HTML, CSS, and JavaScript files).
  • Access to your domain name settings if you plan to use one.

Step 1: Update Your System

First, ensure your system’s package list is up to date.

sudo apt update && sudo apt upgrade -y

Keeping your packages up to date is crucial for security and compatibility, especially when dealing with server components like Apache.


Step 2: Install Apache Web Server

Apache is available in the default Debian repositories. You can install it using apt.

sudo apt install apache2 -y

Once the installation completes, Apache should start automatically. You can verify the status with:

sudo systemctl status apache2

You should see output indicating that the service is active (running).

To check if Apache is serving the default page, open your browser and visit:

http://your_server_ip

You should see the default Apache welcome page.


Step 3: Create a Directory for Your Static Site

Next, create a directory to hold your static website files. By default, Apache serves content from /var/www/html, but it’s best practice to create a separate directory for each site.

Let’s say your site will be called my-static-site.

sudo mkdir -p /var/www/my-static-site

Now, copy your static site files into this directory. If you have a local folder with your site content, use scp or any other method to transfer it. For example:

scp -r ./my-site-files/* user@your_server_ip:/var/www/my-static-site/

Ensure that Apache has the correct permissions to read the files:

sudo chown -R www-data:www-data /var/www/my-static-site
sudo chmod -R 755 /var/www/my-static-site

Step 4: Create a Virtual Host Configuration

Apache uses virtual hosts to manage multiple sites on the same server. Let’s create a new configuration file for your static site.

sudo nano /etc/apache2/sites-available/my-static-site.conf

Add the following content, adjusting the ServerName, DocumentRoot, and log file paths as needed:

<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /var/www/my-static-site

    <Directory /var/www/my-static-site>
        Options -Indexes +FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/my-static-site_error.log
    CustomLog ${APACHE_LOG_DIR}/my-static-site_access.log combined
</VirtualHost>

Note: Replace yourdomain.com with your actual domain name or IP address if you don’t have a domain.

Save and exit the editor.


Step 5: Enable the New Site

Once your configuration file is created, you need to enable the site:

sudo a2ensite my-static-site.conf

Disable the default site (optional but recommended if not needed):

sudo a2dissite 000-default.conf

Reload Apache to apply the changes:

sudo systemctl reload apache2

Now, when you visit http://yourdomain.com or your server IP, you should see your static website.


Step 6: Test and Troubleshoot

To verify that your site is working:

  1. Open a browser and go to http://yourdomain.com or http://your_server_ip.
  2. You should see your website homepage.
  3. Check the logs if something goes wrong:
sudo tail -f /var/log/apache2/error.log
sudo tail -f /var/log/apache2/access.log

These logs are invaluable for diagnosing problems such as permission errors or misconfigured directives.


Step 7: Optional – Enable Gzip Compression

To improve performance, especially for users on slower connections, enable gzip compression.

First, enable the deflate module:

sudo a2enmod deflate
sudo systemctl restart apache2

Add the following configuration to enable gzip compression for text files:

sudo nano /etc/apache2/conf-available/gzip.conf

Paste the following content:

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
</IfModule>

Enable and reload:

sudo a2enconf gzip
sudo systemctl reload apache2

You can test gzip using online tools like https://www.giftofspeed.com/gzip-test/.


Step 8: Optional – Set Up a Firewall (UFW)

If you are using UFW, make sure Apache traffic is allowed:

sudo ufw allow 'Apache Full'
sudo ufw enable

Check UFW status:

sudo ufw status

Step 9: Optional – Set Up SSL with Let’s Encrypt

It’s always a good idea to secure your site with HTTPS, even if it’s just serving static content.

  1. Install Certbot and the Apache plugin:
sudo apt install certbot python3-certbot-apache -y
  1. Run Certbot to obtain and install an SSL certificate:
sudo certbot --apache
  1. Follow the prompts to select your domain and enable HTTPS.

Certbot will automatically update your Apache configuration and reload the server. You can verify it by visiting https://yourdomain.com.

To renew automatically:

sudo certbot renew --dry-run

Conclusion

Serving a static website using Apache on Debian 12 Bookworm is straightforward and efficient. You’ve now learned how to:

  • Install and configure Apache
  • Create and serve content from a dedicated directory
  • Set up a virtual host
  • Enable performance enhancements like gzip
  • Optionally secure your site with HTTPS

Apache remains a reliable and robust web server, even for simple use cases like static sites. With its flexible configuration system and broad ecosystem, it provides a solid foundation whether you’re deploying a small landing page or laying the groundwork for something bigger.


Next Steps

Here are a few ideas on what to explore next:

  • Add a CDN like Cloudflare to boost performance and security.
  • Implement logging and monitoring for traffic analytics.
  • Automate deployments using Git hooks or CI/CD pipelines.
  • Explore other static site hosting options like Nginx for comparison.