Tag Selector

Zaragoza Clouds

by Zaragoza Online

virtual host

Adding a Virtual Host to your Apache or LAMP Server PDF Print Email
Written by   
Friday, 26 September 2008 12:12

Adding a Virtual Host allows us to access a web directory on our web browser using a path other that http://localhost or http://127.0.0.1. like http://shantanu/  or http://ba-ba-black-sheep/

 Open up a terminal and type the following :

 

cd /etc/apache2/sites-available/

sudo touch ba-ba-black-sheep.conf

sudo vi ba-ba-black-sheep.conf

 Type in the settings for this Virtual Host in the just opened editor :

<VirtualHost 127.0.1.1:80>
ServerName ba-ba-black-sheep
ServerAdmin me@ba-ba-black-sheep
DocumentRoot /home/me/ba-ba-black-sheep/www
<Directory /home/me/ba-ba-black-sheep/www>
Options -Indexes
AllowOverride All
Order Allow, Deny
Allow From All
</Directory> 
</VirtualHost>

Now all this might seem a bit fancy so let me explain what we just did here.

  • First line <VirtualHost 127.0.1.1> specifies the ip address at which this host resides. This will usually be in 127.x.x.x range for local servers.
  • ServerName ba-ba-black-sheep sets the name of the server as it would finally appear e.g. http://ba-ba-black-sheep/.
  • ServerAdmin This e-mail address is being protected from spambots. You need JavaScript enabled to view it   is your email address to be used for server admin.
  • DocumentRoot /home/me/ba-ba-blacksheep/www/ is the webdirectory path i.e. the path where you will store all your web scripts for the web server. e.g. the php, html etc. files
  • <Directory /home/me/ba-ba-blacksheep/www/> initializes the block of rules for the web dir. these are generally the kind of rules you can also specify in the .htaccess files on you webroot.
  • Options -Indexes specifies that the webserver won't return a list of files in the web directory to the client(browser) if no index file is present
Add a symbolic link to the site name config :
cd ../sites-enabled

ln -s /etc/apache2/sites-available/ba-ba-black-sheep.conf ./ba-ba-black-sheep.conf

sudo vi ../apache2.conf

Go to the end of apache2.conf and add the following NameVirtualHost 127.0.1.1:80 at the following position
#Include the virtual host configurations

NameVirtualHost 127.0.1.1:80

Include /etc/apache2/sites-enabled/

 Run the following :
sudo vi /etc/hosts

Edit the configuration after 127.0.0.1 to add your site name: Note that you willprobably have your own device name instead of shantanu-laptop  in the file.

127.0.0.1      loaclhost
127.0.1.1      shantanu-laptop ba-ba-black-sheep
Thats it you should be all set now. Just restart apache.
sudo /etc/init.d/apache restart
At this point typing http://ba-ba-black-sheep/ would display the content stored in the webroot
 /home/me/ba-ba-black-sheep/www


Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! Yahoo! Joomla Free PHP
Last Updated on Friday, 26 September 2008 13:23
 


Taxonomy by Zaragoza Online