Apache VirtualHost isn't serving a page

6

2

I have a domain name that is linked to my ip address at home. However, I want to use the same server/ip to show more than one website.

I can't get Virtual Hosts to work though.

My conf.d/virtual.conf is:

NameVirtualHost 192.168.10.151:80

My sites-available/www.mydomain.com is:

Listen 80
<VirtualHost *>
        ServerAdmin     admin@mydomain.com
        ServerName      www.mydomain.com
        ServerAlias     mydomain.com

        #Indexes and Directory Root
        DirectoryIndex  index.html index.php
        DocumentRoot    /media/BigDisk/www/www.mydomain.com/

        # Log Files
        ErrorLog        /media/BigDisk/www/www.mydomain.com/logs/error.log
        CustomLog       /media/BigDisk/www/www.mydomain.com/logs/access.log combined
</VirtualHost>

And the site was enabled using a2ensite and apache reloaded without any warnings or errors, but when I go to www.mydomain.com I don't get anything ("Opps! Google Chrome could not connect...").

What am I doing wrong?

Update: Restarting Apache results in this:

malfist@webhost:~$ sudo /etc/init.d/apache2 restart
[sudo] password for malfist:
 * Restarting web server apache2                                                                                                                       apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Thu Sep 01 12:16:22 2011] [warn] NameVirtualHost 192.168.10.151:80 has no VirtualHosts
[Thu Sep 01 12:16:22 2011] [warn] NameVirtualHost *:80 has no VirtualHosts
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Thu Sep 01 12:16:22 2011] [warn] NameVirtualHost 192.168.10.151:80 has no VirtualHosts
[Thu Sep 01 12:16:22 2011] [warn] NameVirtualHost *:80 has no VirtualHosts
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
Action 'start' failed.
The Apache error log may have more information.

Malfist

Posted 2011-09-01T03:15:03.347

Reputation: 2 761

Answers

5

Check Name Resolution

First use nslookup or dig or host to check that www.mydomain.com points at the static external IP-address of your router.

Check router config

Then check that your router has port forwarding configured so that requests arriving on port 80 at the external interface are forwarded to port 80 at your server's private IP address.

Check ISP restrictions

Then check that your ISP's Terms and Conditions allow for you to run a HTTP service. Some ISPs block inbound HTTP connections.

LAN != Internet

Lastly, remember that if you are testing this from within your LAN you need a different IP-address for www.mydomain.com, you can add 192.168.10.151 www.mydomain.com to your hosts file (e.g. /etc/hosts or C:\WINDOWS\system32\drivers\etc\hosts). As Malfist pointed out in a comment: if the router supports NAT-reflection, this is not be necessary.


Update:

Check the Apache error logs (all of them)

"Could not connect" suggests a basic IP connectivity problem rather than an Apache vhost configuration problem. The latter is more likely to result in a HTTP response of 404 or 500. If the request reached Apache you should see an entry in the error log. If there is no entry there, it is a good indicator that Apache isn't receiving any request.

Follow up warnings and errors reported by Apache

NameVirtualHost *:80 has no VirtualHosts

See common misconfigurations

Multiple NameVirtualHost lines will yield a "NameVirtualHost *:80 has no VirtualHosts" warning. Apache will ignore the second directive and use the first defined NameVirtualHost line, though. This seems to happen when one is using multiple virtual host configuration files and doesn't understand that you only need to define a particular NameVirtualHost line once. As above, this can occur in the debian ports.conf file, especially after an upgrade.

Address already in use: make_sock: could not bind to address 0.0.0.0:80

Some other program is already listening on port 80. Use netstat -anp to find out what, then stop it.

Diagnostic tools

Ethernet Sniffer

I would use a network sniffer (e.g. tcpdump or wireshark) on the Apache server to see what incoming HTTP requests are arriving - if none, then you know it's an IP connectivity problem.


Update 2:

Wget

Another good diagnostic tool is to run this on the server

 wget --header="Host: www.mydomain.com" -O - http://localhost

This is the sort of output you should see

[root@mybox logs]# wget --header="Host: foo.bar" -O - http://localhost
--2011-09-01 18:56:08--  http://localhost/
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 125 [text/html]
Saving to: `STDOUT'

 0% [                                       ] 0           --.-K/s              <html>
 <head>
  <title>mybox</title>
 </head>
 <body>
  <h1>mybox</h1>
  <p>Nothing to see, move along</p>
 </body>
</html>
100%[======================================>] 125         --.-K/s   in 0s

2011-09-01 18:56:08 (10.8 MB/s) - `-' saved [125/125]

[root@mybox logs]# tail -n 1 access_log
127.0.0.1 - - [01/Sep/2011:18:56:08 +0100] "GET / HTTP/1.0" 200 125 "-" "Wget/1.11.4 Red Hat modified"

Add the -S option to wget to see the server's response headers.

If you get something like "connection refused", Apache isn't listening on port 80.

Netstat

The output from netstat -anp should include

tcp        0      0 :::80        :::*       LISTEN      12345/httpd

RedGrittyBrick

Posted 2011-09-01T03:15:03.347

Reputation: 70 632

My router supports NAT reflection, so I can point to my public IP and the router has no problem. The DNS entry for the domain is correct, and my ISP doesn't care. I was running fine (single domain on laptop) until I decided to do Virtual Hosts, and now it's not working. – Malfist – 2011-09-01T13:21:44.430

Answer updated. – RedGrittyBrick – 2011-09-01T14:00:22.103

I run a different version of chrome at work, and it gives the error message, "tcp_error", and "connection refused". Does that clarify anymore? I know the IP/DNS entry is correct, because I have port 8080 routed to another box inside the LAN, and I can reach it just fine. – Malfist – 2011-09-01T15:21:12.177

"connection refused" suggests a problem in your router configuration. If Apache is configured normally and is listening on port 80, your server won't refuse connections. Have you checked the error logs and used a network sniffer as I suggested? – RedGrittyBrick – 2011-09-01T15:27:41.760

The router allowed the connection until I switch apache to virtual hosts, and if I switch apache's configuration back, the page resolves successfully. – Malfist – 2011-09-01T15:29:12.523

There is nothing in the logs except when it was not in the vhost configuration. – Malfist – 2011-09-01T15:41:28.320

Try my wget suggestion, if that works, the fault is almost certainly not your Apache config. – RedGrittyBrick – 2011-09-01T15:50:40.987

"Connecting to localhost|127.0.0.1|:80... failed: Connection refused." And netstat -anp | grep 80 doesn't show anything listening on port 80. – Malfist – 2011-09-01T16:14:08.107

Updated question. Apache is now throwing exceptions when trying to start. It wasn't showing this earlier, I don't know why. Also, if it means anything, the apache server is running on Ubuntu 11.04 server inside a VirtualBox instance with a bridged network connection. – Malfist – 2011-09-01T16:19:16.990

I needed a ServerName in my config.d/virtual.conf and the NameVirtualHost needed to be a wildcard. – Malfist – 2011-09-01T16:26:56.463

1

NameVirtualHost  *:80

This is in /etc/apache2/ports.conf. If you delete it or comment it out and restart Apache, the warning disappears.

carlos

Posted 2011-09-01T03:15:03.347

Reputation: 11

1

I spent hours on this and all I needed to do is release my ip address ifconfig eth0 down. It became static, now it recognizes my virtual host name.

Redbike

Posted 2011-09-01T03:15:03.347

Reputation: 11