views:

76

answers:

2

I am rapidly coming to the conclusion that this is not feasible, although for some reason, people - most likely who have not implemented the 'advice' they give, seem to think this is possible.

The scenario is quite straightforward. I am developing symfony websites on my local dev machine, running Ubuntu 10.0.4 LTS. I want to be able to run and test multiple sites locally.

Assuming I have the following sites:

  • site1.localhost
  • site2.localhost
  • site3.localhost

Following the documentation here, here and here (none of which work for me), I have done the following:

A. I modified my /etc/hosts file with the first entry to be:

127.0.0.1 site1.localhost site2.localhost hpdtp-ubuntu910 localhost php.localhost

B. I modified my /etc/apache2/ports.conf file (first lines) as follows:

NameVirtualHost localhost:80 Listen 80

C. I have created configuration sites for each of the websites (site1.localhost and site2.localhost). Each configuration is a separate file in /etc/apache2/sites-available

One such configuration file (for site1.localhost) in /etc/apache2/sites-available/site1 looks like this:

<VirtualHost localhost:80>
  ServerName site1.localhost
  DocumentRoot "/home/morpheous/work/webdev/frameworks/symfony/sites/site1/web"
  DirectoryIndex index.php

  <Directory "/home/morpheous/work/webdev/frameworks/symfony/sites/site1/web">
   AllowOverride All
   Allow from All
  </Directory>

  Alias /sf /lib/vendor/symfony/symfony-1.3.6/data/web/sf
  <Directory "/lib/vendor/symfony/symfony-1.3.6/data/web/sf">
      AllowOverride All
      Allow from All
  </Directory>

</VirtualHost>

D. I have disabled the default apache site by using (it kept showing up instead)

E. Since I can't enable all the sites I enable to work (like the documentation links above purport), I have settled for enabling one site at a time, so that Apache dosen't get its confused as to which site to run. When I need to run another site, I disable the current one and enable the one I want to. This is (obviously?) far from ideal - but even this setup is not working - for the reasons listed below.

i). When I restart Apache, I get the following warning:

  • Reloading web server config apache2 [Sun Jul 18 10:32:23 2010] [warn] NameVirtualHost localhost:80 has no VirtualHosts

ii). when I navigate to http://site1.localhost I get the following error message in FF:

Oops! This link appears to be broken

iii). My apache related errors are appearing in /var/log/apachche2/other_vosts.log

morpheous@hpdtp-ubuntu910:~$ tail /var/log/apache2/other_vhosts_access.log
site1.localhost:80 127.0.0.1 - - [18/Jul/2010:10:08:38 +0100] "GET / HTTP/1.1" 404 506 "-" "Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.2.6) Gecko/20100628 Ubuntu/10.04 (lucid) Firefox/3.6.6 GTB7.1"
site1.localhost:80 127.0.0.1 - - [18/Jul/2010:10:09:30 +0100] "GET / HTTP/1.1" 404 506 "-" "Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.2.6) Gecko/20100628 Ubuntu/10.04 (lucid) Firefox/3.6.6 GTB7.1"
site1.localhost:80 127.0.0.1 - - [18/Jul/2010:10:09:31 +0100] "GET /favicon.ico HTTP/1.1" 404 505 "-" "Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.2.6) Gecko/20100628 Ubuntu/10.04 (lucid) Firefox/3.6.6 GTB7.1"
site1.localhost:80 127.0.0.1 - - [18/Jul/2010:10:09:36 +0100] "GET /favicon.ico HTTP/1.1" 404 505 "-" "Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.2.6) Gecko/20100628 Ubuntu/10.04 (lucid) Firefox/3.6.6 GTB7.1"

My questions are:

  1. Can Apache be setup to handle multiple virtual hosts on localhost? Afterall, all examples I have seen so far involve setting Apache with public facing ip addresses

  2. If Apache can indeed handle multiple sites on localhost, which of the steps above am I doing incorrectly?. AFAICT, I have followed the documentation to the letter.

+5  A: 

Yes, you can have several VirtualHosts, on your local machine -- It's exactly the same as when working on a remote server, except the IP address and domain-name are not the same.

What I generally do is :

Edit the hosts file

To add the new domain-names that I want served from my local computer.

For example, I would add :

127.0.0.1       tests
127.0.0.1       blog

And so on, with one line for each domain-name I want.


Add new VirtualHosts to Apache's configuration

Then, I add new VirtualHosts to Apache's configuration.

First site : tests


For example, for my tests domain-name, I would use :

<VirtualHost *:80>
        ServerName tests
        DocumentRoot /home/squale/developpement/tests
        <Directory /home/squale/developpement/tests>
                AllowOverride All
                Options Indexes FollowSymLinks MultiViews
                allow from all
        </Directory>
        ErrorLog /var/log/apache2/error.log
</VirtualHost>

A couple of things to note here :

  • The VirtualHost is on *:80
    • So, it'll listen to any address
  • The mapping to the domain-name is made by the ServerName directive


Of course, up to you to make sure that this VirtualHost is seen by Apache -- either by placing it's configuration in a loaded file (not recommended), or by :

  • Placing that configuration in a new file in /etc/apache2/sites-available/
  • Using a2ensite to enable the site
    • Which will create a symbolic-link in /etc/apache2/sites-enabled/, pointing to your new configuration file.


Second site : blog

And here the configuration I'd use for a second domain-name, that correspond to blog :

<VirtualHost *:80>
        ServerName blog
        DocumentRoot /home/squale/developpement/blog.pascal-martin.fr/www
        <Directory /home/squale/developpement/blog.pascal-martin.fr/www>
                AllowOverride All
                Options Indexes FollowSymLinks MultiViews
                allow from all
        </Directory>
        ErrorLog /var/log/apache2/error.log
</VirtualHost>

It's basically exactly the same thing ; only two differences :

  • The ServerName directive correspond to my second domain-name -- of course
  • And the DocumentRoot is not the same


Other files ?

I don't really change anything else to Apache's default configuration (I'm on Ubuntu, if that matters).

For example, I don't change anything in the ports.conf file : I still have the default NameVirtualHost and Listen directives :

NameVirtualHost *:80
Listen 80

Only important modifications I do are to enable some modules, like rewrite, expires, ... But that's not much related to your VirtualHosts problem ;-)


Results ?

If I open my browser and go to http://tests/, I get the things I would expect from my tests domain -- a list of directories and files, here.

And if I open my browser and go to http://blog/, I get the development instance of my blog -- well, and error page, saying that I forgot to set up the database ^^

Pascal MARTIN
@pascal: According to your reply, Apache, can deal with multiple sites, but you provided an example of /etc/hosts with only one site (tests). In my case, I am 'mapping' (if thats the correct word) site1.localhost, site2.localhost, localhost etc to 127.0.0.1 (hence multiple sites). I think Apache is getting confused with the multiple sites being mapped to 127.0.0.1. Additionally, (apart from listening on all IP addresses on the network), the only difference between your virtual host configuration and mine is the line "Options Indexes FollowSymLinks MultiViews". I wonder if this is causing err?
morpheous
@pascal: You did not mention anything about the edit I made to /etc/apache2/ports.conf. Do I need to take that edit out or is it ok?
morpheous
@morpheus : I've edited my answer to show the configuration for a second web-site ;;; Apache is not confused by multiple sites being mapped to 127.0.0.1 : this works perfectly ;-) ;;; there is at least another difference between your vhost configuration and mine : you have `<VirtualHost localhost:80>`, and I have `<VirtualHost *:80>` ;;; I don't think I modified anything in ports.conf.
Pascal MARTIN
@pascal: +1 for helping me get rid of the annoying Apache warning. I followed your instructions regarding changing the Virtual host IP to match all IP addresses and that fixed thatApache warning. Now all that remains is to be able to navigate to http://site1.localhost in my browser (that currently dosen't work). I notice you have edited your answer to show how to add multiple hosts. I will try that. If it woks I will accept your answer as the final solution.
morpheous
@pascal: I changed the entries in my /etc/hosts from the "one line" syntax I was using, to the multi-line syntax you used (i.e. each site on a separate line) - and that still did not work :( When I go to http://site1.localhost I still get the error message in FF: "Ooops this link appears to be broken" - AND my errors are still being reported in /var/log/apache2/other_vhosts.log .... :(
morpheous
Using several lines in the hosts files, instead of only one will change nothing : it's just easier to read-write and to remove one site ;;; in your ports.conf, are you still using `NameVirtualHost localhost:80` ? If so, try `NameVirtualHost *:80` *(that's why I have in mine)*
Pascal MARTIN
The last bit about the error logs, I think this can be fixed by the CustomLog directive, so its not a problem. The main problem remains that I cannot access the site through the browser...
morpheous
@Pascal: I have already edited my /etc/apache2/ports.conf file to match the default (what you are using) - thats how I managed to get rid of the Apache warning.
morpheous
ok... Just to be sure : you said your VirtualHosts configurations are in sites-available ; you didn't forget to run a2ensite (or to create a symlink in sites-enabled) ?
Pascal MARTIN
@pascal: I apologize profusely for wasting your time. The fault was on my side. I placed an ErrorLog in my VirtualHost declaration and was able to capture the logs directly. I had a DirectoryIndex index.php line in the declaration. The error log 'reminded' me that apache could not 'stat' index.php. I had forgotten to place the file there! After fixing that, all the problems have gone away - I can now also run multiple sites with no problem. Pascal, this is not the first time you have helped me when I was in a tight spot. Is there any way I can give you some of my points to show my appreciation
morpheous
huhu :-D That's a nice one ^^ Glad you found out the cause of your problem :-)
Pascal MARTIN
A: 

Did you create a link from /etc/apache2/sites-available/site1 to /etc/apache2/sites-enabled/site1 ?

I'm using nginx instead of apache but ubuntu has standarized way of configuration. Usually sites-available includes all your configurations which are not included by apache. It only looks for configurations linked in sites-enabled directory.

I also don't think you need to enable one site a time. All should work. All have different domain names.

kuba
@kuba: For Ubuntu, the sym links are automatically created/destroyed when the scripts a2ensite and a2dissite (enable/disable respectively) are run. This is what I meant by enabling/disabling a site.
morpheous