/etc/hosts entry for single IP server serving multiple domains
Solution 1:
I'm assuming this is for testing, otherwise you'd be setting up proper DNS records, not your hosts
file.
What you want is for every name you want to call your web server with, to resolve to your server's IP address.
If you are testing from the server itself, then you can make everything point to 127.0.0.1, but of course also making it point to your server's actual IP address would work.
If you are testing from another machine, then of course you want every name to resolve to the server's real IP address.
The syntax is straingthforward:
IP.of.server www.domain.name domain.name
IP.of.server www.otherdomain.name otherdomain.name
IP.of.server www.anotherdomain.name anotherdomain.name
IP.of.server www.yetanotherdomain.name yetanotherdomain.name
...and so on.
Update:
Of course, what ErikA says is completely right. Modifying the hosts
file is not needed for the server to work; it's only useful if/when you need to test it without having proper DNS records in place, or if you want to override them to connect f.e. to a test server instead of a production one.
Solution 2:
The only reason you should ever need to use your hosts file is if you don't have a functioning DNS environment or if you need to temporarily override some DNS name for testing purposes.
Modifying the hosts file is not needed for using Name Virtual Servers.
Solution 3:
The IP entry should be whatever Apache is listening on. So if you have <VirtualHost 1.2.3.4:80>
, your hosts entry should start with 1.2.3.4
.
If you're listening on all your interfaces, then lucky you! You get to pick one! And 127.0.0.1
will work just fine.
As for the part on the right, it must match what you type in the address bar. So if you want to be able to type www.example.com
in your address bar and have something happen, then you'll need that name in your hosts file telling your computer which IP that means. Apache can only handle the redirect if the traffic gets there to begin with; hence the hosts file entry.
And I'm sure I needn't say that this is only appropriate for a development setup. Host-IP address mappings should be done by a DNS server, not a static file.
Solution 4:
Apparently you do always need to fix the hosts file, regardless of DNS - otherwise when you re-start apache under heavy load, it takes up to 90 seconds to actually stop and re-start ( see Why httpd graceful restart takes such a long time? )