How to Create Virtual Host in Apache on Ubuntu Debian and Linux Mint?


 Virtual Hosting helps you to run multiple domains on a single server. Shared hosting servers allow you to host thousands websites  on a single server using this Virtual Hosting. Today in this article i have listed the steps to run this Virtual hosting on Apache2 server on Ubuntu, Debian and Linux Mint systems.

Step 1: Installation of Apache:

To run this virtual host you should have apache in your system, Follow the below step to install apache in your system. If you have already installed apache in your system then you can skip this step.

$ sudo apt-get update

$ sudo apt-get install apache2

Step 2: Creating Your First Virtual Host:

All kind of virtual host configuration is stored under /etc/apache2/sites-available directory. You can find default virtual host file during the new apache installation. And create new virtual Host configuration file by copying default file.

$ cd /etc/apache2/sites-available/

$ sudo cp default ubuntu.example.com.conf

Edit the new virtual host configuration file and update as per your requirement.

For example i have taken ubuntu.example.com.

So type the following command.

$ vim ubuntu.example.com.conf

You will find the configuration file of above domain like this.

<VirtualHost *:80>

ServerAdmin webmaster@ubuntu.example.com

ServerName ubuntu.example.com

DocumentRoot /var/www/ ubuntu.example.com/httpdocs

<Directory />

Options FollowSymLinks

AllowOverride None

</Directory>

<Directory /var/www/>

Options Indexes FollowSymLinks MultiViews

AllowOverride None

Order allow,deny

allow from all

</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

<Directory "/usr/lib/cgi-bin">

AllowOverride None

Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

Order allow,deny

Allow from all

</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

And now create a directory structure in your system, as per defined in the above virtual host and assign owner ship and file permission.

$ mkdir -p /var/www/ubuntu.example.com/httpdocs

$ chmod 755 /var/www/ubuntu.example.com/httpdocs

$ chown www-data.www-data /var/www/ubuntu.example.com/httpdocs

Now upload the project files on /var/www/ubuntu.example.com/httpdocs/ directory. For this example i have created a index.html file for testing purpose like below.

$ echo “<h1>ubuntu.example.com</h1> /var/www/ubuntu.example.com/httpdocs/index.html

Step 3: Enabling the virtual Host

Now we have created our virtual host in apache. To enable the virtual the virtual host. When you reload, Apache will load the configuration file.

$ a2ensite ubuntu.example.com

When you execute the above command soft link of Virtual Host configuration file is created in /etc/apache2/sites-enabled/ directory.

To activate the new configuration run the following command.

$ service apache2 reload

Now your new domain will be enabled and you can access http://ubuntu.example.com in your browser. If you don’t have dns configured for your domain, do a local mapping by adding an entry in /etc/hosts files.

...

192.168.1.100  ubuntu.example.com

...

You can create still more virtual host by repeating the Step 2 and Step 3.

 

4:04 pm