How to create apache virtual hosts in apache2(Ubuntu)

The layout of apache configurations in rehat distros and debian distros are entirely different. I’m here explaining how to create a virtual host in ubuntu.

In your home directory create a ‘public_html’ folder and create sub directories as follows.

mkdir -p public_html/{public,private,log,cgi-bin,backup}

And then create a sample test.html file in public_html directory. Make sure that the public_html directory has be read and executable permissions.

Now we need to add virtual host entry in sites-available directory.

nano /etc/apache2/sites-available/

# domain:
# public: /home/demo/public_html/

<VirtualHost *:80>

  # Admin email, Server Name (domain name) and any aliases

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html
  DocumentRoot /home/demo/public_html/

  # Custom log file locations
  LogLevel warn
  ErrorLog  /home/demo/public_html/
  CustomLog /home/demo/public_html/ combined



Now we need to enable the site.


Then restart the apache

/etc/init.d/apache2 reload

If you are creating this in your local machine add appropriate entry in /etc/hosts file

nano /etc/hosts localhost


Then navigate to it will show the test file contents.<!> And your virtual host is working fine.