Use the LAN based IP address to access local Apache on laptop when working outside the network?

12,689

Solution 1

Option 1

Set ServerName to localhost like this:

ServerName localhost

Then access the server via

http://localhost

It will not matter what IP you have.

Option 2

First you setup VirtualHost like this:

<VirtualHost *:80>
  DocumentRoot "D:/Projects"
  ServerName projecthost
</VirtualHost>

Then on windows you can always set an IP to which "projecthost" will point to. Edit C:\Windows\system32\drivers\etc\hosts file and add:

projecthost 127.0.0.1

or

projecthost 192.168.11.74

Then access the server via

http://projecthost

Solution 2

This doesn't work because the 'ServerName' parameter to Apache says effectively: If you get a request, make sure that the URL specified by the user has 192.168.11.74 as the destination. When you are outside of your company network and not using that IP address (because you were assigned another), then your request will either:

  • Reach the Apache server but have a different IP address (either 127.0.0.1 or whatever IP you were assigned by the foreign network), or
  • Not reach the Apache server because your computer doesn't know where 192.168.11.74 is anymore (it has forgotten its former identity because it is irrelevant to its new network location)

If you only need the one web site (likely at this stage), I would recommend removing the ServerName line because it doesn't help you (as recommended by Manuel Sousa) and using 127.0.0.1 locally, and 192.168.11.74 when you are on your work network.


An example of a sub-optimal approach that might achieve the stated goal, you can try to configure your network card to have an additional IP address (192.168.11.74) while you are not at work. Challenges:

  • This might not work if your external network has a dynamic IP address.
  • This will likely interfere with your computer functioning on your work network.

Again, I think this approach is a bad idea, but you can judge for yourself: http://social.technet.microsoft.com/Forums/windows/en-US/691d5aa4-09a3-4390-856a-26c10d773a3b/how-to-assign-differentmultiple-ip-address-to-nic-network-card?forum=w7itpronetworking

Solution 3

If you only intend to use one virtualhost you can use the apache default virtualhost. See: Apache Default Virtual Host

If you're hosting multiple virtualhosts edvinas provides the answer. Create a name for each project, and create a entry on your hosts file with that name pointing to 127.0.0.1 which is always available.

Share:
12,689

Related videos on Youtube

SAGE
Author by

SAGE

Updated on September 18, 2022

Comments

  • SAGE
    SAGE over 1 year
    <VirtualHost *:80>
      DocumentRoot "D:/Projects"
      ServerName 192.168.11.74
    </VirtualHost>
    

    Why doesn't this work ? How to access websites on the local box (Windows, Apache, MySql, PHP) which are based on IP address assigned by company's internal network which is not accessible to outside. When the laptop gets connected to internal network, all websites works, but when outside of the company network, these IP based url doesn't work even if all the code-base, development environment resides in the local Apache server on the laptop itself.

    Is there a way to point 192.168.XX.XXX to 127.0.0.1

    • Michael Hampton
      Michael Hampton about 10 years
      The long-term solution is Mobile IPv6; with this your laptop will always have the same IPv6 address everywhere you go. If you already have IPv6 at your company, you should see if your IT department can set this up.
    • SAGE
      SAGE about 10 years
      I am not sure about this IPv6 at present in the company. I will definitely ask, but is there any other way without changing anything at the company level or permanent change to do this. I am looking for a solution that can be applied on the laptop and when get into the company network it should work as it works usually. I was reading this.. Apache IP-based Virtual Host but not sure if it is the right way. Doesn't work either.
    • quadruplebucky
      quadruplebucky about 10 years
      @MichaelHampton s/long-term/very-long-term-not-gonna-happen-real-soon/
    • Michael Hampton
      Michael Hampton about 10 years
      @quadruplebucky It will be soon enough. If you really want to talk about it, though, comments aren't ideal; try chat.
    • phoops
      phoops about 10 years
      Did you bother trying Option 2 in my answer as this is the way it should be really done. You need to use VirtualHosts and DNS. This way you can also simulate different environments.
  • SAGE
    SAGE about 10 years
    the internal ip address(192.168.11.74), 127.0.0.1, localhost: all points to the same one project directory under which I keep all projects as separate directories. Can you pls provide me what should be the content into the vhost file.
  • SAGE
    SAGE about 10 years
    So we cant have IP based ServerName in the virtual host configuration in Apache ? Like this: <VirtualHost *:80> DocumentRoot "D:/Projects" ServerName 192.168.11.74 </VirtualHost> So any request made on that laptop with specific IP based url will point to the local Apache where the project has been setup ? Let me read the Apache Default Virtual Host. Thanks !!
  • Manuel Sousa
    Manuel Sousa about 10 years
    You can think of the ServerName as what you type at the browser URL and get redirected to that website. If you type 192.168.11.74 and the servername is 192.168.11.74 then you get to the right place, as long as 192.168.11.74 works (happens on your office network) but doesn't when it doesn't resolve. 127.0.0.1 always resolves to your local computer so you can use that one and access the site as 127.0.0.1, but other people won't be able to access it. That's the reason why default virtualhost is probably the best solution, but it all depends on what your requirements are.
  • SAGE
    SAGE about 10 years
    It doesn't work. Also, when the IP(192.168.11.74) is being typed in the browser, it should not change the URL rather, it should point to local Apace server which is accessed by 127.0.0.1 or localhost
  • SAGE
    SAGE about 10 years
    It doesn't work. Also, I don't think we can point one IP to another in this manner in the hosts file.