How to Configure Apache Virtual Hosts In Debian/Ubuntu

What is Apache Virtual Host?

Virtual Host term refers to the method of running more than one website such as host1.domain.com, host2.domain.com, or www.domain1.com, www.domain2.com, etc., on a single system. There are two types of Virtual Hosting in Apache, namely IP-based virtual hosting and name-based virtual hosting. With IP-based virtual hosting, you can host multiple websites or domains on the same system, but each website/domain has a different IP address. With name-based virtual hosting, you can host multiple websites/domains on the same IP address. Virtual hosting can be useful if you want to host multiple websites and domains from a single physical server or VPS. I hope you got the basic idea of Apache virtual hosts.

NB: You need to have apache2 already installed and also have website directory inside webroot directory (/var/www/html), here I have already downloaded WordPress in it.

Create a configuration file for each host

we need to create configuration files for each virtual host. First, I do this for the maheshpalamuttath.info site.

Copy the default virtual host file called 000-default.conf contents to the new virtual host files like below.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/maheshpalamuttath.info.conf

Please be mindful that you must save all configuration files with .conf extension at the end, otherwise, it will not work.

Now, modify the configuration files to match with our virtual hosts.

Edit maheshpalamuttath.info.conf file:

sudo nano /etc/apache2/sites-available/maheshpalamuttath.info.conf

Edit/modify ServerAdmin, ServerName, ServerAlias and DocumentRoot values match to the virtual host.


Save and close the file.

Enable virtual host configuration files

After making the necessary changes, disable the default virtual host config file i.e 000.default.conf, and enable all newly created virtual host config files as shown below.

sudo a2dissite 000-default.conf
sudo a2ensite maheshpalamuttath.info.conf

Restart the apache webserver to take effect the changes.

sudo systemctl reload apache2
sudo systemctl restart apache2

That’s it. We have successfully configured virtual hosts in Apache. Let us go ahead and check whether they are working or not.

Test Virtual hosts

Open /etc/hosts file in any editor:

sudo nano /etc/hosts

Add all your virtual websites/domains one by one like below.

Please note that if you want to access the virtual hosts from any remote systems, you must add the above lines in each remote system’s /etc/hosts file.

Save and close the file.

Open up web browser and point it to your domain, I can access my WordPress website from www.maheshpalamuttath.info



0 Komentar