Access virtual host from another machine over LAN

64,119

Solution 1

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!

Solution 2

In addition to danp's answer, you can access the virtual host without having to change the client machine's etc/hosts file by assigning a port to the virtual host. This is ideal if you want to access the server with a mobile or tablet device:

  1. Edit server's httpd.conf file at:

    \wamp\bin\apache\apache2.2.x\conf\httpd.conf
    

    Search for "Listen" (around line 61). You should see the following that allows for Apache to listen for port 80:

    Listen 0.0.0.0:80
    Listen [::0]:80
    

    Add the following lines to add listening for port 81 (or any port):

    Listen 0.0.0.0:81
    Listen [::0]:81
    
  2. Edit the httpd-vhosts.conf file at:

    \wamp\bin\apache\apache2.2.x\conf\extra\httpd-vhosts.conf
    

    Change your "Virtual Host" tag to port 81:

    <VirtualHost *:81>
        DocumentRoot "D:/websites/project1/"
        ServerName www.project1.com
    </VirtualHost>
    
  3. Restart Apache server.

  4. On the client machine/tablet/mobile, on the web browser, enter the server's IP address (192.168.0.10, or whatever IP) followed by the port number in the following format:

    http://192.168.0.10:81
    

Solution 3

There are two computer in local network.

A computer(192.168.1.70) Setup(D:\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf):

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

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

B computer Setup(c:/windows/system32/drives/etc/hosts):

192.168.1.70    www.project1.com
192.168.1.70    www.project2.com

B access A,My project is working.

Solution 4

A couple of updated points to consider for the selected answer:

  1. NameVirtualHost is no longer used after Apache version 2.3.11 and can be omitted.

    In 2.3.11 and later, any time an IP address and port combination is used in multiple virtual hosts, name-based virtual hosting is automatically enabled for that address.

  2. Because we are talking about hosting a website over LAN, let's set a requirement* to only accept connections from IP addresses on your local network. For example, on a common Linksys router, the default IP assigned to each device on the network is between 192.168.1.100 to 192.168.1.255. You can allow connection from all devices on the LAN with an IP address 192.168.1.XXX by using Require ip 192.168.1 (notice the final octet is left off the IP to allow the entire range).

    This allows you to configure access per project so that one may be available over LAN and another is only available locally.

    # This will allow all LAN connections to www.project1.com
    <VirtualHost *:80>
        DocumentRoot "D:/websites/project1/"
        <Directory  "D:/websites/project1/">
            Require local
            Require ip 192.168.1
        </Directory>
        ServerName www.project1.com
    </VirtualHost>
    
    # This will allow only the machine hosting the website to access www.project2.com
    <VirtualHost *:80>
        DocumentRoot "D:/websites/project2/"
        <Directory  "D:/websites/project2/">
            Require local
        </Directory>
        ServerName www.project2.com
    </VirtualHost>
    

    While your site will not be served publicly without the router forwarding traffic on port 80 to your host, I believe this is considered best practice. It is especially necessary if you need to control which projects are available to devices on the LAN.

  3. Reminder: Your host machine should be configured to use a static IP address instead of being assigned one by your router's DHCP. Since we are editing the hosts file of other devices to point to the server's IP, we don't want it to change.

* I'm including this because it is common to have access restrictions on a local development server and you will need to specifically make it available to your local network.

Share:
64,119
jatin3893
Author by

jatin3893

Graduate in Computer Science, Interested in Computer Graphics

Updated on May 09, 2020

Comments

  • jatin3893
    jatin3893 about 4 years
    • 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.)