How to Install and Configure Apache Web Server on Debian 11 by Marouane May 9, 2024 linux

How to Install and Configure Apache Web Server on Debian 11

The Apache webserver is one of the most popular open-source web servers developed and maintained by the Apache Software Foundation. Apache is by far the most widely used web server application in Linux operating systems, but it can be used on almost all operating system platforms such as Windows, MAC OS, OS/2, and so on. It allows developers to publish their content over the Internet.

This article explains how to install and configure Apache web server on Debian 11 (Bullseye). The same steps also work under the older Debian 10 version and have been tested there as well.

Install Apache 2 on Debian Linux

Follow the steps below to install Apache2 on your system using the official Ubuntu repositories.

Step 1: Update system repositories

First, we need to update the package sources in our operating system. To do this, run the following command in the terminal as sudo:

$ sudo apt update

When prompted for the password, enter the sudo password.

Update Debian 11 Packages

Step 2: Install Apache 2 with the apt command

Next in this step, install the Apache2 web server using the following command:

$ sudo apt install apache2

Install Apache2

You will be provided with a Y/n option to continue the installation. Hit y to continue.

Step 3: Verify the Apache installation

Once the installation is completed, you can view the Apache version installed by running the following command in Terminal. This way you can also verify that Apache is successfully installed on your system.

$ apache2 -version

Check installed Apache2 version

Configure the Ubuntu Firewall

If the UFW firewall is running on your system, you will need to allow certain web ports so that external users can access it. For that, run the following commands in Terminal:

$ sudo ufw allow 80/tcp
$ sudo ufw allow 443/tcp

Then run the following command to verify if the ports have been allowed.

$ sudo ufw status

Configure the Apache Webserver

Step 1: Verify that the Apache service is running

After the installation, the Apache web service starts running automatically. However to make sure, run the following command in Terminal:

$ sudo systemctl status apache2

The result should be similar to this:

Apache server status

Step 2: Verify Apache Webserver is working

You can verify if the Apache web server is working fine by requesting a web page from the Apache webserver.

Execute the below command in Terminal to find the IP address of your server.

$ hostname -I

Once you find the IP address, type http:// followed by the IP address of your web server as follows:

http://server_IP

By entering the above IP address, you will see the following default Apache page.

Apache default page

Set Up Virtual Hosts in Apache

Virtual hosts in Apache enable you to run multiple websites on a single server. We will set up here virtual host in the Apache webserver. For that, we will first create a website named testdomain.info using the server block that is available in Apache by default.

Step 1: Set up a domain name

First, we will create a directory at /var/www for our virtual host testdomain.info.For that, we will use the following command:

(Note: Replace testdomain.info with your own domain name.)

$ sudo mkdir -p /var/www/testdomain.info/html

Now change the ownership and permissions using the following commands:

$ sudo chown -R $USER:$USER /var/www/testdomain.info/html
$ sudo chmod -R 755 /var/www/testdomain.info

Now we will create a sample index page to test our testdomain.info site. To do so, we will create an HTML file using the nano editor as follows:

$ nano /var/www/testdomain.info/html/index.html

Add the following lines for the index page:

<html>
<head>
<title>Welcome to the page testdomain.info!</title>
</head>
<body>
<h1>You got Lucky! Your testdomain.info server block is up!</h1>
</body>
</html>

Sample web page in HTML

Once done, press Ctrl+O to save and then Ctrl+X to exit the file.

Now we will create a virtual host file that will serve the contents of your server using the following command:

$ sudo nano /etc/apache2/sites-available/testdomain.info.conf

Now add the following configuration details for your domain name:

<VirtualHost *:80>
  ServerAdmin admin@testdomain.info
  ServerName testdomain.info
  ServerAlias www.testdomain.info
  DocumentRoot /var/www/testdomain.info/html
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Apache virtual host file

Once done, press Ctrl+O to save and then Ctrl+X to exit the file.

Step 2: Enable the domain configuration file

Now enable the virtual host file using the following command:

$ sudo a2ensite testdomain.info.conf

Enable virtual host

Now let’s disable the default Apache configurations using the following command:

$ sudo a2dissite 000-default.conf

Disable default vhost

To apply the changes, restart Apache2 service using the following command:

$ sudo systemctl restart apache2

Step 3: Test for errors

Now test the configuration for any syntax errors:

$ sudo apache2ctl configtest

In case there is no error, you will receive the following output.

Apache config test

In some cases, you might receive the following error (in this case it happened during our tests on a Debian 10 system):

Solve apache configuration errors

To resolve this error, edit the servername.conf file by executing the following command:

$ sudo nano /etc/apache2/conf-available/servername.conf

Add the following line in it:

ServerName testdomain.info

Set a server name
Once done, press Ctrl+O to save and then Ctrl+X to exit the file.

After that run the following command:

$ sudo a2enconf servername

Restart apache to apply the changed config

Now reload the Apache2:

$ systemctl reload apache2

Once done, again run the following command to test the configuration file:

$ sudo apache2ctl configtest

Now you will see the error has been removed.

Step 4: Test if Apache is serving your domain name

Now open the browser and navigate to :

http://testdomain.info

Replace testdomain.info with your own domain name.

The following index page shows now you are able to access all your websites.

Domain test

Some Common Apache Management Commands

Here are some of the most common commands that can be used for managing Apache services:

Use the following command to start the Apache server:

$ sudo systemctl start apache2

Use the following command to stop the Apache server:

$ sudo systemctl stop apache2

Use the following command to restart the Apache server:

$ sudo systemctl restart apache2

Use the following command to reload the Apache server:

$ sudo systemctl reload apache2

Use the following command to always start the service on boot:

$ sudo systemctl enable apache2

Use the following command to disable the Apache server:

$ sudo systemctl disable apache2

In this article, we have learned how to install and configure the Apache web server on a Debian 11 or Debian 10 OS. We have done some basic configurations that include changes to the firewall, setting up the virtual host, and how to manage the Apache services using some commands. I hope it has given you a basic overview of how to use Apache to host the websites properly.

Copyright © Marouane All Rights Reserved

Buy Me a Coffee