VirtualHost In Localhost Is Not Accessible From Localhost When Offline, How To Fix It?

6,943

If you're setting up just a development machine you don't need outside url's to lead into your machine as you should be developing in such a way that the url doesn't matter. I have multiple projects going at the same time and all of them work fine with no internet connection. This is how I setup my platform.

localhost does not need to have internet to work.

Open your hosts file and add your sites to your development machine.

sudo nano /etc/hosts

For each site you're going to build, add a host record for it like below. enter image description here

Now in your apache virtual host files you can set them up accordingly.

As an example here is the config file for my elim.localhost site. Which I store as /etc/apache2/sites-available/elim.localhost.conf

enter image description here

after adding this information to the file I issue this command:

sudo a2ensite elim.localhost.conf && sudo service apache2 reload

Now in the browser, access my site I can type:

elim.localhost

I repeat this process for each project. Based on the setup at the time of this post the following urls can be used on my development machine, all going different places.

  • elim.localhost
  • 18we.localhost
  • 29pines.localhost

If you're using phpmyadmin for your work on a mysql database server, localhost/phpmyadmin should still work with no problems.

This is confirmed to work even with the network card disconnected from the network.

Share:
6,943

Related videos on Youtube

GaryP
Author by

GaryP

An Aspiring Web Designer, learning by his own.

Updated on September 18, 2022

Comments

  • GaryP
    GaryP over 1 year

    I am setting up my own local development web server, mainly for theme development.

    I've successfully setup LAMP in Ubuntu 13.04 64bit Desktop, apache works fine.

    The machine where LAMP is installed is also my deskstop station.

    So I want to access the VirtualHost both from the localhost itself and from another machine within my lan.

    I've setup my first virtual host named devone.loc
    I've already inserted the name inside my hosts file.
    Everything is working and accessible.
    It's also accessible within any machines in my lan, I have setup it up in DNSmasq in my DD-WRT router.

    But then, when I disable my network connection, the virtual host is
    no longer accessible. But the default virtual host is still accessible without network.

    What seems to be the problem here? I also need to access the virtual host within the localhost since it's also my desktop station.

    I've attached my virtual host conf file below:

      <VirtualHost *:80>
        DocumentRoot /var/www2
        ServerName devone.loc
        ServerAlias www.devone.loc
        <Directory /var/www2>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
        </Directory>
      </VirtualHost>
    


    You can easily recreate my problem, when you're a virtualhost user.
    Disable all your Network, and using firefox (in my case, FF v23) browse your
    local dev site and it will throw server not found error.

    Browsing my site offline in my localhost's virtualhost is very important for me,
    I use it to demo or for a project presentation for a client.

    Additional Info

    I forgot to mention that Webmin is also installed in my machine.


    Update
    I've setup another web server in another linux machine using LinuxMint 15, server is XAMPP 1.8.2-1, the end result is the same.

    I try to setup wordpress while my network is disabled but It's throwing server not found error or when the directory is empty it's throwing Access Forbidden.

    When I check or ticked the Work Offline menu of firefox, I get access to the wordpress installation menu, but only the index part, clicking next is no longer accessible again.

    I've done several combinations of directives, but I still can't get it to work.

    So, the main problem is...

    My VirtualHost is not reachable/accessible when all network is disabled. I'ts only reachable/accessible when network is enabled.

    Update - I got it FIXED

    This issue is actually connected to Firefox Settings, changing the setting did fixed my problem.

    network.dns.disableIPv6 is the Firefox's preference name that I changed it's boolean value to false

    So, what I did:

    1. Opened up Firefox and typed in about:config in the address bar.
    2. I got prompted with a warning... This might void your warranty, I clicked I'll be careful, I promise!
    3. In the search bar just below the tabs, I searched for network.dns.disableIPv6, and when It appeared, I just double clicked it to change it's value to false.
    4. I rebooted my machine and browse to my project's site, and bam! Finally opened up.

    For newbies like me, hope this will help you out.

    I can now sleep peacefully... thanks to this post.

    I had sleepless nights searching about this issue, different search strings and phrases yields different search results. I finally reached that right page. I also discovered that this issue was also asked in Super User.

    • GaryP
      GaryP over 10 years
      I hope someone can explain, why the default local-host site is working even when network is disabled and the created virtual-host is not. Even without the above fix/tweak. I am referring to the default apache web-page. The It works web-page.
    • zhongfu
      zhongfu about 9 years
      You might want to put your solution into an answer and mark it as accepted.