How to Install Apache2 on Debian 12

20 Jan 2026
By Rosyid Majid

How to Install Apache2 on Debian12

Apache2 is one of the most widely used web servers in the world. It is commonly used to host websites built with PHP, HTML, and various web frameworks.
In this article, I will walk through the steps to install Apache2 on Debian 12, create a VirtualHost, and secure the website using HTTPS with Let’s Encrypt.

Apache2 is one of the most widely used web servers in the world. It is commonly used to host websites built with PHP, HTML, and various web frameworks.
In this article, I will walk through the steps to install Apache2 on Debian 12, create a VirtualHost, and secure the website using HTTPS with Let’s Encrypt.

This setup is suitable for learning environments, internal servers, and production-ready web deployments.

1. Installing Apache2

The first step is to update the system and install Apache2:

sudo apt update
sudo apt upgrade -y
sudo apt install apache2 -y

After the installation is complete, Apache2 will automatically start. You can verify that the service is running by checking its status:

sudo systemctl status apache2

If the service is active, Apache2 is ready to be configured.

2. Important Apache2 Directory Structure

Understanding Apache’s directory structure is essential for proper configuration and troubleshooting.

Path Purpose
/etc/apache2/apache2.conf Main configuration
/etc/apache2/sites-available/ VirtualHost files folder
/etc/apache2/sites-enabled/ Symlink for active sites
/var/www/html/ Default document root

3. Creating a New VirtualHost

In this example, a local domain server.lan is used.

a. Create the web directory

sudo mkdir -p /var/ww/server.lan
sudo chown -R $USER:$USER /var/www/server.lan

This directory will serve as the document root for the website.

b. Create the VirtualHost configuration file

sudo nano /etc/apache2/sites-available/server.lan.conf

Add the following configuration:

<VirtualHost *:80>
    ServerName server.lan
    ServerAlias www.server.lan
    DocumentRoot /var/www/server.lan

    <Directory /var/www/server.lan>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example_error.log
    CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>

This configuration defines the domain, document root, access permissions, and log files for the VirtualHost.

4. Enabling the VirtualHost

Untuk mengaktifkan VirtualHost yang baru dibuat :

sudo a2ensite /etc/apache2/sites-available/server.lan.conf
sudo systemctl reload apache2

At this stage, Apache will begin serving content from /var/www/server.lan.

5. Enabling .htaccess (Optional)

To use .htaccess for URL rewriting or framework configurations (such as WordPress or Laravel), ensure the following directive is set in the <Directory> block:

AllowOverride All

6. Enabling the Rewrite Module

Apache’s rewrite module is required for .htaccess and clean URLs:

sudo a2enmod rewrite
sudo systemctl restart apache2

7. Checking Apache Configuration

Before moving to HTTPS, it is good practice to validate the configuration:

sudo apache2ctl configtest

If the output is:

Syntax OK

the configuration is valid and safe to use.

8. Configuring HTTPS with Let’s Encrypt

To make your website secure, you can use a free SSL certificate from Let’s Encrypt:

sudo apt install certbot python3-certbot-apache -y sudo certbot --apache

Follow the on-screen instructions to enable HTTPS for your domain.

Congratulations! You have successfully installed Apache2 on Debian 12, created a VirtualHost, and secured your website with HTTPS. Apache2 is a flexible web server, and this tutorial serves as a foundation for hosting PHP websites, WordPress, Laravel, or other web frameworks.

By understanding these basics, you are ready to build a professional and secure web server.