How to add Virtual Hosts to Apache on Ubuntu

Apache virtual host, as explained by Apache, refers to the practice of running more than one web site (such as www.site1.com and www.site2.com) on a single machine. Virtual hosts can be “IP-based” or “name-based”. By default, Ubuntu already has this capability enabled, so things are much easier to configure these days..


cd /etc/apache2/sites-available


sudo vim vishalvyas.com.conf and enter your VirtualHost directive. Below I've put the most basic example, see Apache docs for details and additional features:

<VirtualHost *:80>
   ServerName vishalvyas.com
   DocumentRoot /home/vishal/vyas_html
</VirtualHost>

Save & exit.

sites-available: this directory has configuration files for Apache2 Virtual Hosts. Virtual Hosts allow Apache2 to be configured for multiple sites that have separate configurations.

sites-enabled: like mods-enabled, sites-enabled contains symlinks to the /etc/apache2/sites-available directory. Similarly when a configuration file in sites-available is symlinked, the site configured by it will be active once Apache2 is restarted.

sudo vim /etc/hosts and add your new domain to the 127.0.0.1 localhost line so it looks like this:
127.0.0.1 localhost vishalvyas.com

Save & exit.

Enable your new virtualhost:
sudo a2ensite vishalvyas.com.conf

Reload the Apache configuration:
sudo /etc/init.d/apache2 reload

Repeat for each of your VirtualHosts.

Thanks,
Vishal

0 comments:

Post a Comment