Search code examples
windowsapachewindows-7virtualhostwampserver

Access virtual host from another machine over LAN


  • I am using Windows 7 with Wamp 2.2 server.
  • I have setup 2 virtual hosts: www.project1.com and www.project2.com.
  • I have modified the "hosts", the httpd.conf, and the httpd-vhosts.conf files, to the changes I mentioned below.

Using my browser, when I type www.project1.com or www.project2.com, I successfully get my web pages opened on the laptop that has the server installed on.

Changes in the "hosts file": I've appended the followings to the end of the file:-

127.0.0.1       localhost
127.0.0.1       www.project2.com
127.0.0.1       www.project1.com

Changes in the httpd.conf file:-

Include conf/extra/httpd-vhosts.conf

Changes in httpd-vhosts file:-

NameVirtualHost *:80

<Directory "D:/websites/">
    AllowOverride All
    Order Deny,Allow
    Allow from all
    </Directory>
<VirtualHost 127.0.0.1>
    DocumentRoot "D:/websites/wamp/www/"
    ServerName localhost
</VirtualHost>


<VirtualHost 127.0.0.1>
    DocumentRoot "D:/websites/project1/"
    ServerName www.project1.com
</VirtualHost>


<VirtualHost 127.0.0.1>
    DocumentRoot "D:/websites/project2/"
    ServerName www.project2.com
</VirtualHost>


Now; since I can open these web pages from a browser in PC_1 (the one with the server), how can I access these web pages from a browser in PC_2? (I mean any PC connected to PC_1 via LAN.)


Solution

  • In your virtualhost directive, change 127.0.0.1 to *:80 and as Gabriel mentioned, add an entry to the hosts file in the other machine, adding your domain to be associated with the IP of your server.

    When you put an explicit IP into the directive, apache will only listen on that IP - but the wildcard will tell it bind to all IPs available to it.

    <VirtualHost *:80>
        DocumentRoot "D:/websites/project1/"
        ServerName www.project1.com
    </VirtualHost>
    

    If your server is on 192.168.1.70 for example, then in the other machines on your lan, the hosts entry will look like:

    192.168.1.70     www.project1.com
    

    Restart apache and it should work fine.

    As a note, when you are using virtualhosts, apache will use the first definition as a default for when it can't make a match between the domain passed in the HTTP request header and the sites setup in the config, which is why your default page was appearing.

    You told apache to bind to all IPs with the NameVirtualHost *:80 directive, but then didn't setup a site for that external IP. Hope that helps!