Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a few local versions of development websites on my local mac, and want to provide access to them from a browser through vhosts, as well as using the live (web) versions from time to time.

I have read many examples of people doing similar things by changing the URL, and having apache listen to the unique URL to serve from a local location. I have always done it using the same URL, but a different port, and while it works seamlessly on windows, I can't get it working on the mac.

(Let's say) I have two websites:

  1. amazingwebsite.com
  2. facebookiller.org

I want to access the local versions by using the same URL, by enabling the browser's proxy (with one click) which I have set to 8080. apache is set to Listen *:8080 in httpd.conf.

In httpd-vhosts.conf (which is getting loaded) I have:

NameVirtualHost *:8080

<VirtualHost *:8080>
    ServerAdmin webmaster@amazingwebsite.com
    ServerName amazingwebsite.com
    ServerAlias www.amazingwebsite.com
    DocumentRoot "/Users/username/Development/Projects/amazingwebsite"

    <Directory "/Users/username/Development/Projects/amazingwebsite/">
        Options Includes Indexes Multiviews
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>

The 'facebookkiller.org' vhost is basically the same - just different local location.

My /private/etc/hosts is now set to:

##
# 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   amazingwebsite.com
127.0.0.1   facebookkiller.org

and after an apache restart (web sharing off/on), apachectl -S reports:

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:8080                 is a NameVirtualHost
         default server amazingwebsite.com (/private/etc/apache2/other/httpd-vhosts.conf:4)
         port 8080 namevhost amazingwebsite.com (/private/etc/apache2/other/httpd-vhosts.conf:4)
         port 8080 namevhost facebookkiller.org (/private/etc/apache2/other/httpd-vhosts.conf:19)
Syntax OK

which looks ok to me.

The behaviour?:

  • amazingwebsite.com:8080 => local installation (correct)
  • www.amazingwebsite.com:8080 => times out (incorrect)
  • amazingwebsite.com => quickly can't connect, browser can't find - (incorrect)
  • www.amazingwebsite.com => goes to web version (correct)
  • facebookkiller.org:8080 => local installation (correct)
  • www.facebookkiller.org:8080 => times out (incorrect)
  • facebookkiller.com => browser can't find (incorrect)
  • www.facebookkiller.com => goes to web version (correct)

So my ServerAlias isn't working, or there is something wrong with my hosts file - or both!

I've spent ages on this, and could really do with some help - thanks..

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

The vhosts are working, you haven't written your local /etc/hosts correctly. What's happening is that your timeout-then-working is because a 'www.' lookup is most likely resulting in a real DNS CNAME pointer back to non-www. version.

127.0.0.1   localhost google.com www.google.com amazon.com www.amazon.com

That's how you use a hosts file - one IP listed one time, all aliases on the same line and you must list all subdomains needed, www. e.g. that will send traffic to your local Apache instance.

share|improve this answer
    
I tried this, and got no difference. Lots of tutorials on the web suggest the other way.. After looking at so many blogs on the subject, I can't even think how I got it working on the PC (turned off in another country right now). –  DefenestrationDay Jul 6 '10 at 3:47
    
did you actually try it with the www. entries? regardless of other factors on implementation that's a key point in the setup and should be done. –  troyengel Jul 6 '10 at 3:54
    
You're going to have problems with accessing both the local and the remote site due to DNS resolution conflicts. The /etc/hosts file is going to trump any other sort of DNS resolution. So, if you do set the www entries in your /etc/hosts, you're going to be able to access only the local installations. You're never going to be able to access both doing it this way. –  photoionized Apr 8 '11 at 22:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.