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

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
    </Directory>
</VirtualHost>

Then in /etc/hosts I put the domains I want :

127.0.0.1 www.mysite.com

127.0.0.1 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 ?

share|improve this question
    
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
1  
Also an additional point, you can combine names if they are pointing to the same IP. So you could write 127.0.0.1 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

2 Answers 2

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.

share|improve this answer
    
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
share|improve this answer
    
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

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.