Create Individual Virtual Hosts Entry

For this, We have assumed that you have  your local site setup completed where “/var/www/example.com/public_html” is the folder of site and
“/var/www/example.com/public_html/index.html”  is the index page for that site.

Step 1 : Create New Virtual Host Files

           First Start by copying the file for the domain:

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

           Open the new file in your editor with root privileges:

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

           The file will look something like this (I've removed the comments here to make the file more approachable):

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

           First, we need to change the ServerAdmin directive to an email that the site administrator can receive emails through.

ServerAdmin admin@example.com

           After this, we need to add two directives. The first, called ServerName, establishes the base domain that should match for this virtual host definition.   
           This will most likely be your domain. The second, called ServerAlias, defines further names that should match as if they were the base name.
           This is useful for matching hosts you defined, like www:

ServerName example.com
ServerAlias www.example.com 

           The only other thing we need to change for a basic virtual host file is the location of the document root for this domain. 

DocumentRoot /var/www/example.com/public_html

           In total, our virtualhost file should look like this:

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

           Save and close the file.

Step 2 : Enable the New Virtual Host Files

           Now that we have created our virtual host files, we must enable them. 

           We can use the a2ensite tool to enable each of our sites like this:

sudo a2ensite example.com.conf

           When you are finished, you need to restart Apache to make these changes take effect:

sudo service apache2 restart

Now Test your Results !!

Add new comment