Web-server – Howto use Apache virtualHost with etc/hosts

apache-2.2hostsvirtualhostweb-server

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 ?

Best Answer

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.