How to Create Virtual Hosts in Linux Apache ?

Assume you have PHP Installed in your Server .

Create the directory virtual hosts inside the /var/www

Note* : – p stands for ‘switch parent directory’ .

 sudo mkdir  -p /var/www/example.tikam/public_html

Now Create index.html inside your public_html directory

sudo nano /var/www/example.tikam/public_html/index.html

Than , Add the following line to index.html

<head><title>Welcome to </title></head>
<body> is Ready </body>

Assign ownership of the directory:

sudo chown -R $USER:$USER /var/www/example.tikam/html

Assign permission to the directory:

sudo chmod -R 755 /var/www/example.tikam/

Create the copy the .conf file using following command

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.tikam.conf

And Edit the .conf file under the /etc/apache2/sites-avaliable/ directory

sudo nano /etc/apache2/sites-available/example.tikam.conf

Than , Add the following code to .conf file.

Note : Configure the information as per your requirements

<VirtualHost *:80>
 ServerAdmin webmaster@localhost
 DocumentRoot /var/www/example.tikam/public_html
 ServerName example.tikam
 ServerAlias www.example.tikam
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined

Add a2ensite

sudo a2ensite example.tikam.conf

Test for configuration errors:

sudo apache2ctl configtest

Output :

Syntax OK

Add add the site to the hosts file ‘/etc/hosts’
Example :    example.tikam

Restart the apache 2

sudo service apache2 reload

That’s All


Leave a Reply

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