?
Solved

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

Posted on 2009-02-14
14
Medium Priority
?
379 Views
Last Modified: 2012-05-06
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?  
0
Comment
Question by:kenjaW
  • 6
  • 4
  • 2
  • +2
14 Comments
 

Author Comment

by:kenjaW
ID: 23642826
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
 
LVL 57

Expert Comment

by:giltjr
ID: 23644796
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
 

Author Comment

by:kenjaW
ID: 23646041
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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
LVL 57

Expert Comment

by:giltjr
ID: 23646064
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
 

Author Comment

by:kenjaW
ID: 23646181
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
 

Author Comment

by:kenjaW
ID: 23646204
Here is the full httpd.conf file in case it sparks any ideas.
httpd-conf.txt
0
 
LVL 57

Expert Comment

by:giltjr
ID: 23646765
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 23648700
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
 

Author Comment

by:kenjaW
ID: 23667239
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 23667835
silly question: does your browser send a HTTP/1.1 request with a proper host: header?
0
 
LVL 27

Accepted Solution

by:
caterham_www earned 2000 total points
ID: 23668822
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
 
LVL 57

Expert Comment

by:giltjr
ID: 23669407
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
 

Author Closing Comment

by:kenjaW
ID: 31546991
Thanks for everyone's help!  caterham_www came up with the solution.  
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 24289482
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month16 days, 8 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question