1
votes

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.

2
The message "NameVirtualHost localhost:80 has no VirtualHosts" may be the result of having the same NameVirtualHost directive in your configuration. It may also be the result of having a more generic "NameVirtualHost localhost" (without a port) before. Please grep for /all/ NameVirtualHost occurances in your config. Does your localhost resolve to more than exactly one IP, e.g. an IPv4 address as well as an IPv6 address? Try using "NameVirtualHost 127.0.0.1:80" in combination with "<VirtualHost 127.0.0.1:80>" (the Apache docs already tell that using IP addresses is preferred over hostnames).Jonas
+1 for very elaborate explanation about the problemTreur

2 Answers

11
votes

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 ^^

0
votes

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.