Apache2 Vhosts and DnsMasq, unable to access website from another computer over LAN : ERR_CONNECTION_REFUSED

apache-2.2dnsmasqlocal-area-networkvirtualhost

I'm currently setting up a web server over my LAN to satisfy some needs like using Phabricator or other intranet services. To do this, I installed apache2, php5, mysql and DNSMASQ which allows me to manage a DHCP and DNS server. But now I'm stucked with virtual hosts that are working just fine over the local machine but cannot be accessed from another computer on the local network, I'm getting "ERR_CONNECTION_REFUSED".

To test my settings, I declared a virtual host like this :

Creating the root folder of our website

$ sudo mkdir -p /var/www/test.devbox/public_html
$ chown -R $USER:$USER /var/www/test.devbox/public_html

Placing an index file and add random text

$ nano /var/www/test.devbox/public_html/index.html

Now we create our virtual host file

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/test.devbox.conf
$ sudo nano /etc/apache2/sites-available/test.devbox.conf

Virtual Host Config file

<VirtualHost *:80>
    ServerAdmin admin@test.devbox
    ServerName test.devbox
    ServerAlias www.test.devbox
    DocumentRoot /var/www/test.devbox/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Then we enable it using

$ sudo a2ensite test.devbox

Then I edited my hosts file to fit the servername directive in my test.devbox.config file (server side)

$ sudo nano /etc/hosts

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

192.168.1.254   bbox

127.0.1.1       MyPie
127.0.0.1       MyPie
127.0.0.1       devbox
127.0.0.1       opcv.devbox www.opcv.devbox
127.0.0.1       test.devbox www.test.devbox

Here is also my dnsmasq.conf file

$ sudo nano /etc/dnsmasq.conf

domain-needed
expand-hosts
bogus-priv
no-resolv
no-poll
localise-queries
no-negcache

# DNS
#interface=eth0
local=/devbox/
domain=devbox

cache-size=10000

server=8.8.4.4
server=8.8.8.8
#server=194.158.122.10
#server=194.158.122.15

# DHCP
dhcp-authoritative
dhcp-range=192.168.1.100,192.168.1.200,10h
dhcp-option=3,192.168.1.254

I then restarted everything

$ sudo reboot 
   or
$ sudo /etc/init.d/dnsmasq restart
$ sudo service apache2 reload/restart

And at this point I don't know where I should look, since the virtual host work locally but cannot be accessed from an other computer on the same network.

Using NSLOOKUP from an other machine I actually get

c:\WINDOWS\system32>nslookup test.devbox
Server:   UnKnown
Address:  192.168.1.10 (which is the server)

Nom :    test.devbox
Address:  127.0.0.1

And when I try to access http://test.devbox, I get ERR_CONNECTION_REFUSED.
I don't know where to look anymore.

Here is the answer of the apache2ctl command

$ sudo apache2ctl -S

AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/ports.conf:5
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
VirtualHost configuration:
*:80                   is a NameVirtualHost
     default server 127.0.0.1 (/etc/apache2/sites-enabled/000-default.conf:1)
     port 80 namevhost 127.0.0.1 (/etc/apache2/sites-enabled/000-default.conf:1)
     port 80 namevhost example.devbox (/etc/apache2/sites-enabled/example.devbox.conf:1)
             alias www.example.devbox
     port 80 namevhost opcv.devbox (/etc/apache2/sites-enabled/opcv.devbox.conf:1)
     port 80 namevhost phabricator.devbox (/etc/apache2/sites-enabled/phabricator.devbox.conf:1)
     port 80 namevhost test.devbox (/etc/apache2/sites-enabled/test.devbox.conf:1)
             alias www.test.devbox
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex rewrite-map: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33

Thank and I hope we'll be able to solve this issue together.

Best Answer

As can be seen from the output of nslookup you have you DNS entry configured wrong, it points test.devbox to 127.0.0.1. It should point to the 192.168.x.x IP of the server instead.

Once fixed this (As per your comments) to do what you want first you must ensure that each client machine that you want to navigate your sites uses your server for DNS resolution.

What happens is, on client machine A you open your browser and put:

http://test.devbox/

In the location bar.

The browser in client machine A asks the DNS server for test.devbox's IP address and get's 192.168.1.10 as a result.

The browser opens a TCP connection to 192.168.1.80 port 80 (HTTP) and send this request (I'm omitting uninteresting headers here):

GET / HTP/1.1
Host: test.devbox
[...]

The http server at that address responds as you've configured it.

So in short, if you're using separate Server and Client machines and want to use a FQDN instead if an IP to navigate your site, you either add the relevant entries at /etc/hosts of the machines (or equivalent under windows) or you ensure that both machines use a DNS server under your control. And no, 127.0.0.1 cant be accessed outside the local machine :-)

Related Topic