11 C
Texas
angeloma
Senior Writer and partner

How to configure an Apache Virtual host on Debian 9?

VirtualHost allows you to assign different websites across domains to the same IP address. They allow easy access to a PHP application hosted on an Apache web server through a simple domain. For this reason, is very used for web developers and system administrators. In this article, I will show you how to configure an Apache virtual host on Debian 9.

0.What you need

Before you start, you must have certain requirements for everything to work perfectly.

  • Have, at least, a notion of how to use the command line interface.
  • A user who can use sudo.
  • A desire to learn. Vital to acquire the knowledge that I will give you.
  • A computer with Debian 9 installed. Obvious, isn’t it?

1. Install Apache web server

Generally, before you start installing and configuring a system, it is a good idea to perform a general system upgrade. With this, you will obtain the last security patches.

Open a terminal and run on it:

- Advertisement -
:~$ sudo apt update && sudo apt upgrade

After you enter your password, the update process will begin. As a result, you will have an updated and secure system.

1.- Upgrading the system
1.- Upgrading the system

Now, you can install apache2 from the official Ubuntu repositories. Run:

:~$ sudo apt install apache2

2.- Installing apache2 web server
2.- Installing apache2 web server

As a result, you can now go to your browser and set the IP address of the server and you will see a page similar to this one. Open it.

http://IP_server/

3.- Apache2 deafult page
3.- Apache2 default page

Good job. Apache is running smoothly on your computer.

2.- Create web directory for each Apache virtual host

In order to create a structure of efficient directories and that the Virtual hosts do not have problems, you must create a directory for each one of them. In this case, I will only create two virtual hosts. One called “osradar1.local” and other called “osradar2.local”.

Create the folders:

:~$ sudo mkdir -p /var/www/html/osradar1.local/public_html
:~$ sudo mkdir -p /var/www/html/osradar1.local/public_html

4.- Creating the folders for virtual host
4.- Creating the folders for the virtual hosts

With this in mind proceeds to change the owner of them. Run,

:~$ sudo chown -R $USER:$USER /var/www/html/osradar1.local/public_html
:~$ sudo chown -R $USER:$USER /var/www/html/osradar2.local/public_html

5.- Changing the folders owner
5.- Changing the owner of the folders

Finally, change the permissions on the folders.

:~$ sudo chmod -R 755 /var/www/html/

6.- Changing the permissions to the folders
6.- Changing the permissions to the folders

Now, create an example index.html file, to check the operation.

:~$ sudo nano /var/www/html/osradar1.local/public_html/index.html
:~$ sudo nano /var/www/html/osradar2.local/public_html/index.html

For the first hosts (osradar1.local) in index.html, type this:

<html>
<header>
<title>
osradar1.local website
</title>
</header>
<body>
This message is from osradar1.local website
</body>
</html>

And for the second host:

<html>
<header>
<title>
osradar2.local website
</title>
</header>
<body>
This message is from osradar2.local website
</body>
</html>

3.- Create the configuration file for each host

In this step, you must create the configuration files for each virtual host, but it’s not complicated because you can rely on the default configuration file.

In the first place, copy the default configuration file, and rename it for each virtual host.

:~$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/osradar1.local.conf
:~$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/osradar2.local.conf

7.- Copying the apache default configuration file
7.- Copying the apache default configuration file

Now, you must edit the files to adapt them to each virtual. For example, edit osradar1.local.conf.

:~$ sudo nano /etc/apache2/sites-available/osradar1.local.conf

In that file, you must modify ServerName, ServerAlias, ServerAdmin, and DocumentRoot.

8.- Editing Virtual host configuration file
8.- Editing Virtual host configuration file

Do the same for the other virtual host.

9.- Editing the second file
9.- Editing the second file

4. Enabling configuration files

Once the configuration files are ready, you must activate them. First, disable the default configuration file and then activate the newly created virtual hosts.

:~$ sudo a2dissite 000-default.conf
:~$ sudo a2ensite osradar1.local.conf
:~$ sudo a2ensite osradar2.local.conf

10.- Enabling new configuration file for the virtual hosts
10.- Enabling new configuration file for the virtual hosts

And it’s ready. Now let’s check that everything was successful. Restart apache2.

:~$ sudo systemctl restart apache2

5.- Test the virtual hosts

In the first place, edit the file /etc/hosts and add your IP with the names of the virtual hosts.

:~$ sudo nano /etc/hosts

11.- Editing hosts file<
11.- Editing hosts file

Now, go to your web browser and enter your virtual host.

12.- Checking the virtual hosts
12.- Checking the virtual hosts

13.- Checking virtual hosts
13.- Checking virtual hosts

And this is it. As you can see it’s really simple.

Tell us, how was your experience with Apache?

Please spread this article through your social networks.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article