Setup Apache Virtual Hosts In LinuxMint / Ubuntu / Debian

May 29, 2018

Virtualhost Hosting multiple domains in single server called virtualhost, you can host N of virtualhost in single apache webserver. See the below digram it will clearly says we can run N of virtualhost in single apache webserver. Make sure your system should have installed LAMP in Ubuntu, LAMP in Linux Mint & LAMP in Debian.

Apache supports two types of virtualhost

  • Name-based Virtual Hosts (All the Websites sharing single IP address)
  • IP-based Virtual Hosts (Each Websites having Different IP address)

1) Name based Virtual Host.

In Name based virtual host, each and every websites sharing the single IP address. How its work ? For Name based virtual host you need to setup DNS properly, so that the domain map with shared IP. Example hosting environment, Whenever you buying domain and hosting they will ask you to point the domain to hosting provider server (Like, They will provide Two Nameservers for your domain to map their server)
setup-virtual-hosts-in-apache-on-linux-mint-17-ubuntu-14-04-debian-7-6-1

Advantage :

  • Easy to Manage
  • Easy to configure compare with IP based
  • Good for shared & reseller hosting environment

1) How to add Name based virtualhost in Apache

In Apache 2.2 and older we need to mention when we using Namebased Virtualhost, like NameVirtualHost IP:80 but Apache 2.4 its not necessary because Any address/port combination appearing in multiple virtual hosts is implicitly treated as a name-based virtual host, For more details

1a) Creating Virtual Directories

We have already mentioned in our Tested Environment table, we are going to test two domain so that we came to know whether its working as a Name based virtualhost. For that we need to create Virtual Directories under www folder.

# Create virtual directory #
$ sudo mkdir -p /var/www/support.2daygeek.com/public_html
$ sudo mkdir -p /var/www/dev.2daygeek.com/public_html

2a) Change the ownership permission

While creating virtual directory By default directory assigned to root user. If its root user permission nobody can’t modify anything. So we need to change the ownership permission to corresponding user to making changes themselves.


# Change the Ownership #
$ sudo chown -R username:username /var/www/support.2daygeek.com/public_html
$ sudo chown -R username:username /var/www/dev.2daygeek.com/public_html

3a) Setting up Proper permission to www directory

Set the proper permission to apache web root (/var/www) so that everbody can read the website.

$ sudo chmod -R 755 /var/www/

4a) Creating sample page for website’s

We need to create the sample page for each websites, so that we can check whether its working with apache or not.

# Create Sample Page for support.2daygeek.com #
$ sudo nano /var/www/support.2daygeek.com/public_html/index.html
<html>
 <head>
 <title>New virtual host created successfully - support.2daygeek.com</title>
 </head>
 <body>
 <h1>New virtual host created successfully - support.2daygeek.com</h1>
 </body>
</html>

# Create Sample Page for dev.2daygeek.com #
$ sudo nano /var/www/dev.2daygeek.com/public_html/index.html
<html>
 <head>
 <title>New virtual host created successfully - dev.2daygeek.com</title>
 </head>
 <body>
 <h1>New virtual host created successfully - dev.2daygeek.com</h1>
 </body>
</html>

5a) Creating Virtual Host Files

We need to create the virtual host file for each domain. By default ubuntu having default virtual host file 000-default.conf under /etc/apache2/sites-available. Just copy for your convenient like below. Make sure your vitualhost configuration file extension should be .conf

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/support.2daygeek.conf
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/dev.2daygeek.conf

6a) Modify virtual host configuration

Open your copied virtual host configuration file and modify like below content (as per your domain name). Make sure you need to modify the domain name and directory name according that.

# Modified Virtual Host for support.2daygeek.com #
<VirtualHost 10.0.2.15:80>
        ServerName support.2daygeek.com
        ServerAlias www.support.2daygeek.com
        ServerAdmin admin@2daygeek.com
        DocumentRoot /var/www/support.2daygeek.com/public_html
        ErrorLog /var/www/support.2daygeek.com/public_html/error.log
        CustomLog /var/www/support.2daygeek.com/public_html/access.log combined
</VirtualHost>

# Modified Virtual Host for dev.2daygeek.com #
<VirtualHost 10.0.2.15:80>
        ServerName support.dev.com
        ServerAlias www.dev.2daygeek.com
        ServerAdmin admin@2daygeek.com
        DocumentRoot /var/www/dev.2daygeek.com/public_html
        ErrorLog /var/www/dev.2daygeek.com/public_html/error.log
        CustomLog /var/www/dev.2daygeek.com/public_html/access.log combined
</VirtualHost>

7a) Enabling / Disabling Virtual Hosts

Use the below commands to Enable/Disable Virtual Hosts.

# Enable Virtual Hosts #
$ sudo a2ensite support.2daygeek.conf
$ sudo a2ensite dev.2daygeek.conf

# Disable Default Virtual Hosts #
$ sudo a2dissite 000-default.conf

8a) Run configtest.

Run the below configtest command to check whether any errors in the newly added configuration files

ircd hosting