views:

315

answers:

3

Hi,

I am attempting to run more than one virtualhost(?), for example: http:/localhost will point to one project, http:/newsite to another and http:/myfavourite again to a different project a different document root.

(each http:// is http:/ here because of hyperlink posting restrictions)

I have had no success looking where to edit the apache files in /etc/apache2. Am I looking for a vhosts file?

Any advice would be awesome, thanks.

A: 

localhost has nothing to do with apache, but is an alias to your machine (ip 127.x.x.x).

you’ll have to edit /etc/hosts to accomplish what you want.

why do you want to do that? isn’t http://localhost/newsite enough?

knittl
Setting up a proper vitual-host on your dev box gives you a more realistic dev environment than just using a directory in /var/www/.
Luke
+2  A: 

You could edit your /etc/hosts and add multiple names pointing to 127.0.0.1, then add VirtualHost entries for each of those names. Depending on your server, the config could be in /etc/apache2/conf/httpd.conf or in /etc/apache2/sites-available. If it's the latter, then here is the first google hit that I got for the config.

Adam Benzan
+2  A: 

Hi,

Here is a chapter of an e-book that explains how to create Virtual Hosts to do precisely what you want -- and the examples are using Ubuntu : Creating A Local Domain Using Apache Virtual Hosts

In a few words :

  • You first need to create the VirtualHost
  • Then, you have to edit your hosts file (under Linux, it's /etc/hosts) so the new "pseudo domain name" points to your machine.

For the VirtualHost, with Ubuntu, you'd create a new file in /etc/apache2/sites-available/ ; for instance named your-site.com ; it would contain something like this :

<VirtualHost *:80>
    ServerName your-site.com
    DocumentRoot /.../www/...

    <Directory /.../www/...>
        Options Indexes FollowSymLinks Includes
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

And you register this file so it's loaded by Apache, with this command :

sudo a2ensite your-site.com

And, then, reload Apache :

sudo /etc/init.d/apache2 reload


You then have to edit /etc/hosts to add a line like this :

127.0.0.1       your-site.com

So "your-site.com" actually points to your own computer.


What is important is that the name used to access your website in a browser is the one that is declared in the hosts file ; it also must be the same than the one used by the ServerName directivr in Apache's configuration.


When you have done that for one VirtualHost... It's just the same for every other one : only the name of the site, and it's DocumentRoot, change.


Hope this helps!

Pascal MARTIN