Apache - Create a VirtualHost

Apache VirtualHost record

You will need to do 3 easy steps in order to create an Apache virtual host in Ubuntu: create VirtualHost file or add a VirtualHost record in the default VirtualHost file, enable the site and restart Apache.

Create a VirtualHost File

If you choose to create a new VirtualHost file, navigate to the following path:

# cd /etc/apache2/sites-available

Create a new file with your site name, for example:

# touch domain.com

Create a VirtualHost record

Next, open the file and add the following VirtualHost record:

<VirtualHost *:80>
    ServerName domain.com
    DocumentRoot /home/www/domain.com

DocumentRoot is the path of the root directory of the site. The next step is to tell Apache that we have added a new site. We can achieve that with the following command:

# a2ensite domain.com

Another way for creating a VirtualHost is by adding a VirtualHost record in the default file. Just add the same record as you did above in the following file:

# nano /etc/apache2/sites-available/default

You don't need to enable anything because the default file is already enabled.

The last thing you should do is to add the site name in the hosts file. This will tell your OS that when you access domain.com to connect to

# nano /etc/hosts

Add the following line in the hosts file: domain.com

For the last, restart Apache

# service apache2 restart
Note: you will need a root permission for all those tasks.
- Posted by Ana to Apache