How to setup virtual host on apache web server in ubuntu.

To encapsulate configuration details we can use virtual host feature. We can host more than 1 website also on ubuntu server.

Apache on ubuntu has a server block enabled by default and default folder is /var/www/html

if we need to host a single website then above path is okay and we can continue our development on default folder.

But in case we need to host multiple website then we should follow virtual host feature

Step 1 : So lets create directory for our domain name

sudo mkdir /var/www/domainname

Step 2 : Lets assign the ownership of the directory with the $user environment variable

sudo chown -R $USER:$USER /var/www/your_domain

The permissions of your web roots needs to be correct if you haven’t modified your umask value, which sets default file permissions. To ensure that your permissions are correct and allow the owner to read, write, and execute the files while granting only read and execute permissions to groups and others, run below command

sudo chmod -R 755 /var/www/your_domain

Step 3 : Create a web page on above domain

sudo nano /var/www/your_domain/index.html

enter some html into this file and save and close it

Step 4 : Now Apache need to host this page . it’s necessary to create a virtual host file with the correct directives.

Instead of modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf

directly, let’s make a new one at /etc/apache2/sites-available/your_domain.conf

Step 5 : enter below data into file

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

updated  DocumentRoot to our new directory

updated ServerAdmin to an email that the domainname site administrator can access.

Added two directives: ServerName, which establishes the base domain that should match for this virtual host definition,

ServerAlias, which defines further names that should match as if they were the base name.

Save and close the file

Step 6 : Lets enable the file with a2ensite tool 

sudo a2ensite your_domain.conf

Disable the default site defined in 000-default.conf:

sudo a2dissite 000-default.conf

Lets test for configuration errors

sudo apache2ctl configtest

Output should be “syntax ok”

restart apache to implement changes

sudo systemctl restart apache2

Apache should now working for new hosted domainname


* The Content stated above is for informational purpose only. Expert Software Team is not responsible if any part of content found meaningless in any manner or condition.