Cantech Knowledge Base

Your Go-To Hosting Resource

How to Install Apache Webserver on Debian 12?

Apache Web Server is an open-source and free-to-use popular web server. It is highly reliable and flexible. Also, it supports multiple programming languages, so developers love it.

Moreover, Debian 12 is a stable Linux distribution. Running Apache on Debian 12 is a good choice because it ensures a secure and efficient web server. The package manager in Debian makes it easy to manage Apache and its dependencies. Further, it provides a strong foundation for Apache and receives regular updates and security patches.

Pre-installation Steps

Before starting with the installation steps, first, you will need a Debian 12 server running with a cloud provider like Cantech. Then, you must have created a domain A record pointing to the server’s public IP (E.g. app.example.com).

Also, log into your server as a non-root user with sudo privileges to maintain a secure environment. Further, update your server to ensure you are using the latest software versions.

Installing Apache Web Server

Apache is available in Debian’s default package repositories so installation is easy.

  • Before that, update your package list with the command –
$ sudo apt update
  • Next, install Apache with –
$ sudo apt install apache2 -y
  • After that, check if Apache has been installed correctly by running –
$ apachectl -v

It should show you the version of Apache installed; Example –:

Server version: Apache/2.4.58 (Ubuntu)

Server built: 2024-07-17T18:55:23

Setting Up the Server’s Firewall

Your server’s firewall might block incoming traffic to Apache by default.

  • So, allow access on the standard HTTP port (port 80) with –
$ sudo ufw allow 80
  • After this, you should reload the firewall to apply the changes:
$ sudo ufw reload

Starting Apache

You can now start the Apache web server as you have installed Apache and configured your firewall. For that, run –

$ sudo systemctl start apache2

Test that Apache is running properly. Open any browser and enter your server’s IP address (e.g., http://SERVER-IP). You should see the default Apache welcome page.

Enabling Apache to Start at Boot

Use the below command to ensure Apache starts automatically when your server boots –

$ sudo systemctl enable apache2

Now, Apache will always start when the server is rebooted. This will save you from having to manually start it each time.

Managing Apache Service

Apache uses its own system service to handle the application processes and run on your server. This service ensures that Apache runs smoothly. It manages all the tasks in the background.

You can easily manage the Apache service with just a few commands. You can make sure it starts whenever your server restarts.

You can control the Apache service with the following commands –

  • Check status:
$ sudo systemctl status apache2
  • Stop Apache:
$ sudo systemctl stop apache2
  • Restart Apache:
$ sudo systemctl restart apache2
  • Reload Apache (without interrupting active connections):
$ sudo systemctl reload apache2

Creating a New Apache Virtual Host

Apache uses virtual hosts to manage different websites or applications on the same server. So, create a new virtual host to serve a website on your domain (e.g. app.example.com).

  1. Disable the Default Virtual Host:
$ sudo a2dissite 000-default.conf

2. Create a New Virtual Host Configuration:

Use the nano editor to create a configuration file for your domain with the below command –

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

(/etc/apache2/sites-available directory has configuration files that provide addresses/domains for serving web applications)

Here’s a sample configuration:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName app.example.com
    ServerAlias app.example.com

    DocumentRoot /var/www/app.example.com
    DirectoryIndex index.php index.html

    ErrorLog ${APACHE_LOG_DIR}/app.example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/app.example.com-access.log combined
</VirtualHost>

This tells Apache to serve files from /var/www/app.example.com when requests are made to app.example.com.

3. Test Configuration:

Check for errors in your configuration file before you enable the new site –

$ sudo apache2ctl configtest

You will get the below if If everything looks fine –

Syntax OK

4. Enable Your Site:

$ sudo a2ensite app.example.com

5. Create the Web Root Directory:

$ sudo mkdir /var/www/app.example.com

6. Set Permissions:

Change the ownership with the below to make sure Apache can access the directory –

$ sudo chown -R www-data:www-data /var/www/app.example.com

Also, set proper permissions:

$ sudo chmod -R 755 /var/www/app.example.com

The permission mode 755 gives specific access to the directory and all its files.

The number 7 means that the www user has full access. This includes reading, writing, and executing files.

The number 5 means the www user group can read and execute files but cannot make any changes.

Similarly, other system users also have read and execute permissions, but they cannot modify the files.

7. Create a Sample HTML Page:

You can create a simple index.html file to test the site:

$ sudo nano /var/www/app.example.com/index.html

Add some content:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Greetings from Vultr</title>
</head>
<body>
    <h1>Greetings from Vultr</h1>
</body>
</html>

Now, restart Apache to apply changes:

$ sudo systemctl restart apache2

Visit http://app.example.com in your browser, and you should see the page you just created.

Securing Apache with SSL

Apache receives connection requests on the standard HTTP port 80 through your domain. The virtual host configuration manages these requests.

Moreover, HTTP does not provide encryption by default so it is less secure for transmitting data.

Therefore, you can enable HTTPS to protect all communication between the user’s browser and the Apache web server with a valid SSL certificate. This ensures that data remains encrypted and safe.

You can follow the steps below to generate a trusted SSL certificate from Let’s Encrypt and secure your Apache web server.

1. Install Snapd:

$ sudo apt install snapd -y

2. Install Certbot:

$ sudo snap install certbot --classic

3. Generate SSL Certificate:

Use Certbot to generate the SSL certificate:

$ sudo certbot --apache -d app.example.com -m [email protected] --agree-tos

Certbot will handle everything. It will also ensure the configuration of Apache to use the SSL certificate.

4. Check SSL Renewal:

It is important to ensure that your SSL certificate will renew automatically. Run this command to simulate the renewal process:

$ sudo certbot renew --dry-run

5. Allow HTTPS Port:

Also, make sure the firewall allows traffic on port 443 (HTTPS):

$ sudo ufw allow 443

$ sudo ufw reload

6. Test HTTPS:

Finally, visit https://app.example.com in your browser for testing. Everything is done successfully if you see a secure connection with a padlock icon in the address bar.

Conclusion

If configured properly, Apache on Debian 12 will handle web requests smoothly. You can customise its settings based on your needs due to its immense flexibility. Also, you can host multiple websites with separate virtual hosts. Explore the official Apache documentation for more advanced configurations.

May 16, 2025