Debian

How to Install and Configure Apache Web Server with Virtual Host on Debian 10

Install and Configure Apache Web Server on Debian 10

Apache is an extensively used open-source web server also referred to as an Apache HTTP server. It is a free cross-platform web server that is supported on various operating systems including Microsoft Windows, Linux, macOS, Solaris, etc. Apache web server is highly customizable, easy to install, ships different features, and can integrate with other modules.

Today, we will explain the installation of the Apache webserver on the Debian 10 system. In this article, the latest available Apache version is 2.4.38 (Debian) which we will install on our Debian system.

Prerequisites

All command must run under the sudo privileges.

Install Apache web server on Debian 10

Step 1: Update package repository

Open the command line application and type the following command on the terminal to make sure that all packages are updated in your system.

$ sudo apt update

Step 2: Install Apache2

In this step, we will install the Apache web server by running the below-mentioned command in the terminal:

$ sudo apt install apache2

The system will ask you about the installation confirmation. You need to enter the ‘y’ and then press ‘Enter’ in order to continue the installation of Apache on your system.

Once the installation of the Apache web server is complete, run the following command to check the installed version:

$ apache2 -version

Step 3: Firewall configurations

If ufw is not installed then first install it and then configure it. So that, allow access to port 80 on the firewall by running the below-given command:

$ sudo ufw allow 80/tcp

Now, execute the following command to view the firewall status:

$ sudo ufw status

As you can see in the above screenshot, the status is inactive on this system. Therefore, enable it by using the following command and then, again check status:

$ sudo ufw enable

Step 4: Verification of Apache services

Before starting the configurations, make sure that Apache services are running on your system. Type the following command to check apache services status:

$ sudo systemctl status apache2

If apache services are not running like the above output then, start the apache services by executing the following command and again check the status:

$ sudo systemctl start apache2

Apache services are now running on your system.

To check apache is running properly on your system or not, request a web page from the Apache server. To request a web page, find your IP address by executing the following command:

$ hostname -I

Now, open the browser Mozilla Firefox and type your IP address in the address bar as follows:

http://your- machine-IP-address

http://10.0.2.15

When you navigate the above-mentioned URL, the following apache welcome page will display in your browser that represents Apache is working fine on your Debian system.

How to create virtual hosts in Apache web server?  

We will discuss in this step, how you can create virtual hosts through which you can host multiple websites on a single web server. In the rest of the article, we will show you how you can set up virtual hosts in Apache web servers.

Step 1: Create webroot directory for the specific domain

So first, we will create a ‘webroot’ directory using the domain ‘’info.net’.

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

The next step is to assign the certain required permissions for ownership to this directory by using the variable name $USER.

$ sudo chown -R $USER:$USER /var/www/html/info.net/

Assign permission for the domain as follows:

$ sudo chmod -R 755 /var/www/info.net

Step 2: Create an HTML web page for the website

Open one of your favorite text editors and create a file with the name ‘index.html’.

$ sudo nano /var/www/html/tecmint.com/index.html

Now, copy and paste the following html lines of code in this file:

<html>

    <head>

        <title>Welcome to info.net</title>

    </head>

    <body>

        <h1>Apache installation guide!</h1>

    </body>

</html>

If you are using a nano editor, press ‘Ctrl + O’ to save this content and then press ‘Ctrl+x’ to exit from the editor.

Step 3: Make a virtual host file

In this step, you will create a virtual host file for the ‘info.net’ domain by using the following command:

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

Paste the below-mentioned lines in this file:

<VirtualHost *:80>

ServerAdmin admin@info.net

ServerName info.net

ServerAlias info.net

DocumentRoot /var/www/info.net/html

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Enable the virtual host file by running the following command:

$ sudo a2ensite info.net.conf

Execute the following command to disable the default site configurations:

$ sudo a2dissite 000-default.conf

Now, test for errors by using the below given command:

$ sudo apache2ctl configtest

If you may receive the error like the above, then you need to change the server name into the following locations:

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

Add the ServerName info.net as follows and exit from the configurations.

Enable the changes by running the following command:

$ sudo a2enconf servername

Restart the apache services by executing the below-mentioned command:

$ sudo systemctl restart apache2

Now, again test the configurations by typing the following command:

$ sudo apache2ctl configtest

Now, the following output on the terminal will be displayed:

Now, open the browser and use this link http://info.net in the address bar.

Remove Apache Web Server

If you no longer need Apache web server and want to remove it along with its dependent packages from your system, run the following command on terminal.

$  sudo apt-get purge apache2

Conclusion

We have implemented some useful steps for the installation and configuration of Apache web server on Debian 10. We have explored how you can create virtual hosts on an Apache server and troubleshoot errors. I hope now you can install and configure apache servers on your Debian system easily. These steps are almost the same for Ubuntu and LinuxMint distributions.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *