Skip navigation.

Working With Virual Hosts On Your Desktop

Bhargi told us how to configure virtual hosts with Apache and Uuntu in a few steps at http://lampcomputing.com/node/38. That post indeed gives us insight to Apache and Ubuntu.

When we are working in teams, we build websites on development servers first. After thorough testing and client's approval we move them to live web servers. When I work on websites alone, I use my desktop as my development server. When I set up too many websites on my desktop, it is a pain in the neck to switch between them frequently. Each time I switch between them, I have to edit the httpd.conf file and restart Apache. With virtual hosts the problem is eliminated.

The term Virtual Host refers to the practice of running more than one web site (such as www.company1.com and www.company2.com) on a single machine. When you configure virtual hosts your web server runs many websites at any given instance. You don't need to edit Apache configuration file and restart Apache every time you switch to a different website.

Here's what I did to set-up virtual hosts on my Fedora desktop

Step 1: Open the Apache configuration file to edit it

#vi /etc/httpd/conf/httpd.conf

Step 2: Add the below directive to the Apache configuration file since we are using name-based virtual hosts. This directive tells the server to use any and all IP addresses on the machine.

NameVirtualHost *:80

Step 3: Add the VirtualHost block to each different website in the Apache configuration file

<VirtualHost *:80>
ServerName website1.binaryvibes.co.in
DocumentRoot /var/www/html/website1
</VirtualHost>

The above block tells the server to run website1.binaryvibes.co.in using the document root /var/www/html/website1.

Step 4: Add as many virtual hosts as you want using VirtualHost blocks like below:

<VirtualHost *:80>
ServerName website2.binaryvibes.co.in
DocumentRoot /var/www/html/website2
</VirtualHost>

Save the file and exit.

Step 5: Restart Apache

#/etc/init.d/httpd restart

The virtual host configuration is done. To use the URLs http://website1.binaryvibes.co.in and http://website2.binaryvibes.co.in from the web browser you have add the hostnames in your network configuration.

Step 6: Open /etc/hosts

#vi /etc/hosts

Step 7: Add the host names

127.0.0.1    website1.binaryvibes.co.in
127.0.0.1    website2.binaryvibes.co.in

save the file and exit.

You will now be able to access http://website1.binaryvibes.co.in and http://website2.binaryvibes.co.in from your browser. At any given instance your local web server runs both website1.binaryvibes.co.in and website2.binaryvibes.co.in. How convenient it is to work on multiple websites with virtual hosts!

I use subdomains of binaryvibes.co.in while testing websites on my desktop because they don't exist in the Internet.



in my case,i would like to

in my case,i would like to create the Vhost under /home/MyUser/work/project/public_html...

when i restart httpd service after i set the vhost, i get document_root /home/MyUser/work/project/public_html does not exist...

Can anyone please help!!

Regards,
Accilies

Does the directory exist?

Hi Accilies,

Does /home/MyUser/work/project/public_html exist?

If the directory exists, my next suspect is lack of Selinux policy or file system permissions.

With warm regards,
Sudheer

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Use the special tag [adsense:format:group:channel] or [adsense:block:location] to display Google AdSense ads.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.