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?  
kenjaWAsked:
Who is Participating?
 
caterham_wwwConnect With a Mentor Commented:
Your <virtualhost> does not correspond with your NameVirtualHost directive
NameVirtualHost *:80
<VirtualHost *:80>
    DocumentRoot "/Applications/MAMP/htdocs/site1"
    ServerName dev.site1.com
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "/Applications/MAMP/htdocs/"
    ServerName dev.site2.com
</VirtualHost>

Open in new window

0
 
kenjaWAuthor Commented:
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.  
0
 
giltjrCommented:
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?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
kenjaWAuthor Commented:
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

0
 
giltjrCommented:
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.
0
 
kenjaWAuthor Commented:
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.  
0
 
kenjaWAuthor Commented:
Here is the full httpd.conf file in case it sparks any ideas.
httpd-conf.txt
0
 
giltjrCommented:
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.
0
 
ahoffmannCommented:
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)
0
 
kenjaWAuthor Commented:
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>
0
 
ahoffmannCommented:
silly question: does your browser send a HTTP/1.1 request with a proper host: header?
0
 
giltjrCommented:
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.
0
 
kenjaWAuthor Commented:
Thanks for everyone's help!  caterham_www came up with the solution.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.