Ubuntu: Creating virtual host

Virtual Hosting allows web servers to host more than one website on a single machine. It allows you to access the local repository using addresses such as http://dev.mysite.com instead of http://localhost/mysite/.

1. Create the required website in root folder such as in ubuntu by default /var/www. For example create “mysite” folder in /var/www.

2. Go to /etc/apache2/sites-available/
create dev.mysite.com.conf file and paste below code


<VirtualHost 127.0.0.1:80>
ServerName dev.mysite.com
ServerAlias www.dev.mysite.com
ServerAdmin tilkor.software@gmail.com
DocumentRoot /var/www/html/mysite

    <Directory /var/www/html/mysite>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>
</VirtualHost>


3. Go to /etc/apache2/sites-enabled/
Create symbolic link by run below code
sudo ln -s ../sites-available/dev.mysite.com.conf dev.mysite.com.conf

4. Add website in hosts by editing
$sudo /etc/hosts and add below line
127.0.1.1 dev.mysite.com

5. Restart or reload apache
$sudo /etc/init.d/apache2 restart
or
$sudo /etc/init.d/apache2 reload

Error:

You might probably be faced the same following error while you were restarting the Apache server on Ubuntu.

$sudo /etc/init.d/apache2 restart
* Restarting web server apache2 apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName
… waiting for apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

To fix that problem, you need to edit the httpd.conf file. Open the terminal and type,

$sudo gedit /etc/apache2/httpd.conf
By default httpd.conf file will be blank. Now, simply add the following line to the file.

ServerName localhost
Save the file and exit from gedit.

Finally, restart the server.

$sudo /etc/init.d/apache2 restart
sometimes it is not working then restart your computer

Congratulations, you have done it.