Apache server is one of the most popular open-source web servers that is developed and maintained by Apache Software Foundation. Apache is by far the most commonly used Web Server application in Linux operating systems, but it can be used on nearly all OS platforms Windows, MAC OS, OS/2, etc. It enables the developers to publish their content over the internet

In this article, we will explain how to install and configure the Apache webserver on Debian 10 OS.

Install Apache 2 on Debian Linux

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

Step 1: Update system repositories

First, we will need to update the package repositories in our OS. For that, run the following command in Terminal as sudo:

$ sudo apt update

When prompted for the password, enter the sudo password.

Update Debian Buster repositories

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 the Apache is successfully installed on your system.

$ apache2 -version

Verify Apache installtion

Configure the Firewall Settings

If the 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 Web server Settings

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

Step 2: Verify Apache Web server

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

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 enables 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 [email protected]
  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.

Test apache configuration file for syntax errors

In some cases, you might receive the following error:

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 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 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.

How to Install and Configure Apache Web Server on Debian 10
Avatar

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.