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:
- amazingwebsite.com
- 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..
Best Answer
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.
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.