Link to home
Start Free TrialLog in
Avatar of kenjaW
kenjaW

asked on

How do I configure virtual hosts file to support multiple development websites?

I'm trying to configure my OS X machine as a local development machine.  I'm developing for several websites and want to associate dev.<domain>.com with my local apache server.  

My hosts file has been modified to include all the hosts

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0     localhost
127.0.0.1       dev.site1.com
127.0.0.1       dev.site2.com
127.0.0.1       dev.site3.com
127.0.0.1       dev.site4.com

Those domains now resolve to my local server, but I want to associate each of those sites with a specific directory, say /htdocs/site1, /htdocs/site2, etc.

I've successfully associated one domain at a time with the correct directory, but I can't figure out the syntax to get them all working at the same time.  

Here is the virtual host statement from my httpd.conf file:

<VirtualHost *>
    DocumentRoot "/Applications/MAMP/htdocs/site1"
    ServerName dev.site1.com
</VirtualHost>

If I comment out that statement then all the domains in the hosts file go to the web root directory.  

How do I get all the domains pointed to the right directory?  
Avatar of kenjaW
kenjaW

ASKER

To test multiple sites I've done the following:

<VirtualHost *>
    DocumentRoot "/Applications/MAMP/htdocs/site1"
    ServerName dev.site1.com
</VirtualHost>

<VirtualHost *>
    DocumentRoot "/Applications/MAMP/htdocs/site2"
    ServerName dev.site2.com
</VirtualHost>

Doing that works for the first one, but the second one is ignored.  If I type in the domain for site2, it loads site1.  If I swap the order, then site 2 always loads, even when site1 is entered.

I've also tried:

<VirtualHost dev.site1.com>
    DocumentRoot "/Applications/MAMP/htdocs/site1"
    ServerName dev.site1.com
</VirtualHost>

<VirtualHost dev.site2.com>
    DocumentRoot "/Applications/MAMP/htdocs/site2"
    ServerName dev.site2.com
</VirtualHost>

The same thing happens there, too.  
Avatar of giltjr
Your second setup for VirtualHost definitions should work.  You do have:

     namevirtualhost *:80

or:

     namevirtualhost 127.0.0.1:80

in your httpd.conf file, right?  If you go to a shell window and enter httpd -S what do you get?
Avatar of kenjaW

ASKER

I didn't have the namevirtualhost *:80 directive in my httpd.conf file.  I commented it out at one point while testing.  Putting it back in didn't make any difference, though.  It still only recognizes the first virtual host that is defined and ignores anything after the first.  

httpd -S  returns:
VirtualHost configuration:
Syntax OK

You need to have the namevirtualhost *:80 to do virtual hosting.  

It appears as if apache is reading a different configuration file than you are updating.  If it was reading the file you are updating, then it would list out the virtual hosts you have defined.

I have never used Apache under OS X, but as OS X is Unix based it would normally use the files in /etc/http/conf and /etc/http/conf.d.
Avatar of kenjaW

ASKER

Apache is definitely is reading the httpd.conf file I'm updating.  As I mentioned, when I change the order of the virtual hosts in that file it changes which virtual host is recognized.  I suppose it is possible that another file is also being loaded that is causing trouble, but I have no idea where to start on that.    

I searched my system and I only have one httpd.conf and that's the one I'm editing.  
Avatar of kenjaW

ASKER

Here is the full httpd.conf file in case it sparks any ideas.
httpd-conf.txt
I looked at your conf file quickly, fist thing I noticed is you are running V2.0 of Apache.  I tested on a 2.0 system and you do NOT get a list of the virtual hosts, this must be bee something they added in 2.2.
following should work:

NameVirtualHost *:80

    DocumentRoot "/Applications/MAMP/htdocs/site1"
    ServerName dev.site1.com


    DocumentRoot "/Applications/MAMP/htdocs/site2"
    ServerName dev.site2.com


for testing, please ensure that you have removed *all* other virtual host configurations, including those in included files
also ensure that your OS X is configured to read /etc/hosts if you have the FQDN defined their (IIRC, OS X's default configuration is *not* to use /etc/hosts)
Avatar of kenjaW

ASKER

Alas, none of the suggestions have lead to a solution.  Not sure why my particular setup has issues.  Notice that in my case, the Document Root for the second site is a directory above the document root of the first one.  See below.  I don't know why that would be an issue, but that is the only difference I can discern.  

NameVirtualHost *:80
<VirtualHost dev.site1.com>
    DocumentRoot "/Applications/MAMP/htdocs/site1"
    ServerName dev.site1.com
</VirtualHost>
<VirtualHost dev.site2.com>
    DocumentRoot "/Applications/MAMP/htdocs/"
    ServerName dev.site2.com
</VirtualHost>
silly question: does your browser send a HTTP/1.1 request with a proper host: header?
ASKER CERTIFIED SOLUTION
Avatar of caterham_www
caterham_www
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ahoffmann's and caterham_www's posts are both correct.

An alternative to catherham_www posting is to setup entries for dev.site1.com and dev.site2.com in your local hosts file that point to 127.0.0.1 or any other IP address that is on your local PC.
Avatar of kenjaW

ASKER

Thanks for everyone's help!  caterham_www came up with the solution.