I'm trying to have

http://www.mysite.com and http://test.mysite.com

running on a production webserver, and now I'm doing some basic tests in local machine before transfering the datas on the production webserver.

If possible I would like to use apache's virtual hosts and the /etc/hosts only.

I use apache's ServerAlias directive inside virtualHost like :

<VirtualHost *:80>
    DocumentRoot /path/to/mysite
    ServerName www.mysite.fr
    ServerAlias *.mysite.com
    <Directory /path/to/mysite>
        Options FollowSymLinks MultiViews
        AllowOverride All  
        Order allow,deny 
        Allow from all

Then in /etc/hosts I put the domains I want : www.mysite.com test.mysite.com

When I test this in local, it works ! ( I can access either www.mysite.com and test.mysite.com ) When I test it in my remote webserver, it does just ignore my settings, and only the default www.mysite.com is available.

What am I missing ? Or my question should be, is it possible to have both urls (www and test) accessible on a webserver, using just virtual host and /etc/hosts file ?

  • You say that you "only have one machine to manage", but then mention testing on a remote webserver. I'm not sure what you mean and think that needs some clarification. If you have more than one machine, the /etc/hosts has to be the same on all of them. This obviously becomes a pain, and is why DNS exists. – Alex Jul 10 '11 at 18:14
  • 2
    Also an additional point, you can combine names if they are pointing to the same IP. So you could write www.mysite.com test.mysite.com instead of each on their own line. – Alex Jul 10 '11 at 18:15
  • thank you for that, I didn't know about it. I edited my post in order to make it more comprehensive. – ling Jul 10 '11 at 18:24

How are you trying to access test.mysite.com?

For this to work on the Internet you need to add a DNS A record for test.mysite.com on whatever server / service hosts your DNS or add a record for your remote server's public IP to your /etc/hosts file.

/etc/hosts is only for the local machine.

On your local setup, in /etc/hosts:

[YOUR REMOTE SERVER'S PUBLIC IP] www.mysite.com test.mysite.com

However, this will only work for YOU (or any system where you manually add the record to the proper hosts file) until you add a DNS A record for your remote server's public IP for test.mysite.com.

  • Ok thanks, so what you say is that one CANNOT use /etc/hosts on a remote server to create subdomain for internet users ? I thought it was possible. Do you know good documentation where this point is explained ? – ling Jul 10 '11 at 22:05
  • That is correct. /etc/hosts is for your machine only. I'm not sure of any good documentation online, however from a book that I have: "One major drawback to /etc/hosts is that it's a purely local file; setting a mapping in one computer's /etc/hosts file only affects name lookups performed by that computer" - amzn.com/0470404833 – tfitzgerald Jul 10 '11 at 23:28

Yes, the configuration that you described is possible s long as the following is in place:

  • Host files (or DNS in most cases) at the systems with the web browsers
  • Corresponding virtual host configurations at the web server
  • Yes, thank you, but I tried to reproduce this configuration with Host files AT THE WEB SERVER. Unfortunately, it seems that this is not possible, but I didn't read that anywhere, or I misunderstood that /etc/hosts. I may find some more documentation about that point then. (if you know some, please let me know ) – ling Jul 10 '11 at 22:11
  • With host files at the web server, it will only be effective when the web browser itself is also on the server. The purpose of the host file to to map a text name to an IP address. The DNS (text) name in this case also forms the URL to access the web server, which assists in determining the virtual host being accessed. – user48838 Jul 10 '11 at 23:18

This might help your plight.

If you want to serve multiple domains with static IPs, you need multiple interfaces. Unless you use virtual interfaces.

< disclaimer: this is only an example. Use at your own risk. >

Open: /etc/network/interfaces

auto eth0
iface eth0 inet dhcp

auto eth0:1
iface eth0:1 inet static
     #if you don't have a gateway setup, comment "gateway" line

auto eth0:2
iface eth0:2 inet static
     #if you don't have a gateway setup, comment "gateway" line


Go to: /etc/hosts www.mysite.com test.mysite.com

If you setup a DNS then you can use the same IPs to map your system. I hope this helps. Contact me if its not the clearest information. Remember to use distinct IP and domain names; DO NOT REUSE AN IP ADDRESS. DO NOT REUSE A DOMAIN NAME (NICKNAME). This first is the only match according /etc/hosts.

  • Did you read the question? This doesn't answer it. There is no need to go back to HTTP 1.0 times, before 1997. We don't even need this with TLS anymore, now that we have Server Name Indication (SNI). – Esa Jokinen May 12 '17 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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