Apache VirtualHost setup… 502 Bad Gateway

502-errorapache-2.2ubuntu-12.04virtualhost

I am trying to set up apache virtual hosts on Ubuntu 12.04 lts. I followed this guide here http://www.debian-administration.org/articles/412 except I used /var/www not /home/www. Ive set the document root folder to 755 permissions. It is a server I am running out of my house, so its possible it could be a problem with my router I would think? I am not using a proxy.Is there a certain way port forwarding needs to be set up to run vhosts? (And yes, It worked fine before vhosts. I get a 502 response error (bad gateway). Here are my two enabled sites

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName www.functioncreate.com
    ServerAlias functioncreate.com

    # Indexes + Directory Root.
    DirectoryIndex index.html
    DocumentRoot /var/www/functioncreate/htdocs/

    # CGI Directory
    ScriptAlias /cgi-bin/ /var/www/functioncreate/cgi-bin/
    <Location /cgi-bin>
            Options +ExecCGI
    </Location>

    # Logfiles
    ErrorLog  /var/www/fucntioncreate/logs/error.log
    CustomLog /var/www/functioncreate/logs/access.log combined
</VirtualHost>

And the second one:

#
#  Example.com (/etc/apache2/sites-available/www.example.com)
#
<VirtualHost *:80>
    ServerName www.labelleviemodesto.com
    ServerAlias labelleviemodesto.com

    # Indexes + Directory Root.
    DirectoryIndex index.html
    DocumentRoot /var/www/labellevie/htdocs/

    # CGI Directory
    ScriptAlias /cgi-bin/ /var/www/labellevie/cgi-bin/
    <Location /cgi-bin>
            Options +ExecCGI
    </Location>


    # Logfiles
    ErrorLog  /var/www/labellevie/logs/error.log
    CustomLog /var/www/labellevie/logs/access.log combined
</VirtualHost>

My httpd.conf has nothing in it. I have tried with "default" site enabled and disabled. The only things in apache that I have touched is the new sites I created in sites-available and then symlinked to sites-enabled via a2ensite (followed by an apache reload) and then creating virtual.conf in conf.d with this inside:

#
#  We're running multiple virtual hosts.
#
  NameVirtualHost *

Here is my error.log

Warning: DocumentRoot [/var/www/functioncreate] does not exist
Warning: DocumentRoot [/var/www/labellevie] does not exist
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Wed Feb 12 23:40:40 2014] [warn] NameVirtualHost *:80 has no VirtualHosts
[Wed Feb 12 23:40:40 2014] [notice] Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3.9 with Suhosin-Patch configured -- resuming normal operations
[Wed Feb 12 23:44:55 2014] [notice] Graceful restart requested, doing restart
Warning: DocumentRoot [/var/www/functioncreate] does not exist
Warning: DocumentRoot [/var/www/labellevie] does not exist
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Wed Feb 12 23:44:55 2014] [warn] NameVirtualHost *:80 has no VirtualHosts
[Wed Feb 12 23:44:55 2014] [notice] Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3.9 with Suhosin-Patch configured -- resuming normal operations
[Wed Feb 12 23:46:06 2014] [notice] Graceful restart requested, doing restart
Warning: DocumentRoot [/var/www/labellevie] does not exist
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Wed Feb 12 23:46:06 2014] [warn] NameVirtualHost *:80 has no VirtualHosts
[Wed Feb 12 23:46:06 2014] [notice] Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3.9 with Suhosin-Patch configured -- resuming normal operations
[Wed Feb 12 23:46:29 2014] [error] [client 10.0.0.14] File does not exist: /var/www/functionCreate/favicon.ico
[Thu Feb 13 00:22:45 2014] [notice] Graceful restart requested, doing restart
Warning: DocumentRoot [/var/www/labellevie] does not exist
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Thu Feb 13 00:22:45 2014] [warn] _default_ VirtualHost overlap on port 80, the first has precedence
[Thu Feb 13 00:22:45 2014] [warn] _default_ VirtualHost overlap on port 80, the first has precedence
[Thu Feb 13 00:22:45 2014] [warn] NameVirtualHost *:80 has no VirtualHosts
[Thu Feb 13 00:22:45 2014] [warn] NameVirtualHost *:80 has no VirtualHosts
(2)No such file or directory: apache2: could not open error log file /var/www/fucntionCreate/logs/error.log.
Unable to open logs
[Thu Feb 13 12:03:37 2014] [warn] pid file /var/run/apache2.pid overwritten -- Unclean shutdown of previous Apache run?
[Thu Feb 13 12:03:37 2014] [notice] Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3.9 with Suhosin-Patch configured -- resuming normal operations
[Thu Feb 13 12:03:57 2014] [notice] caught SIGTERM, shutting down
[Thu Feb 13 12:03:58 2014] [notice] Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3.9 with Suhosin-Patch configured -- resuming normal operations
[Thu Feb 13 12:04:23 2014] [error] [client 166.137.191.15] File does not exist: /var/www/favicon.ico
[Thu Feb 13 12:04:57 2014] [error] [client 166.137.191.15] File does not exist: /var/www/favicon.ico
[Thu Feb 13 12:05:40 2014] [error] [client 186.251.208.20] script not found or unable to stat: /usr/lib/cgi-bin/php
[Thu Feb 13 12:05:40 2014] [error] [client 186.251.208.20] script not found or unable to stat: /usr/lib/cgi-bin/php5
[Thu Feb 13 12:05:40 2014] [error] [client 186.251.208.20] script not found or unable to stat: /usr/lib/cgi-bin/php-cgi

[Thu Feb 13 12:05:40 2014] [error] [client 186.251.208.20] script not found or unable to stat: /usr/lib/cgi-bin/php5
[Thu Feb 13 12:05:40 2014] [error] [client 186.251.208.20] script not found or unable to stat: /usr/lib/cgi-bin/php-cgi
[Thu Feb 13 12:05:41 2014] [error] [client 186.251.208.20] script not found or unable to stat: /usr/lib/cgi-bin/php.cgi
[Thu Feb 13 12:05:41 2014] [error] [client 186.251.208.20] script not found or unable to stat: /usr/lib/cgi-bin/php4
[Thu Feb 13 12:09:18 2014] [notice] Graceful restart requested, doing restart
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Thu Feb 13 12:09:19 2014] [warn] NameVirtualHost *:80 has no VirtualHosts
(2)No such file or directory: apache2: could not open error log file /var/www/fucntionCreate/logs/error.log.
Unable to open logs

Best Answer

I have a few tips:

Check to see if there is a difference in behaviour on both sides of the router. Let's say your server's internal IP is 192.168.0.10. Edit the hosts file on your test machine (/etc/hosts on Mac and c:\windows\system32\driver\etc\hosts on Windows - remeber to use sudo on Mac or "run as admin" on Windows, otherwise you won't be able to edit the file) and add the following lines to it (substitute 192.168.0.10 with your actual internal/home network server IP):

192.168.0.10    www.labelleviemodesto.com labelleviemodesto.com
192.168.0.10    www.functioncreate.com functioncreate.com

Ping all of these domain names to make sure that you're hitting the right IP. nslookup won't work, since it will only query DNS, which will give you the proper (external) IP for the server and ignore the hosts file override. Ping, and all other programs, will honour the hosts file override. Obviously you need to do this on the inside of your private (home) network, say on wi-fi. If the ping targets the internal IP (192.168.0.10) for all 4 domains (this is important, since, for example, functioncreate.com may redirect to www.functioncreate.com and vice versa, so you want to make sure that no matter what, you are talking to the internal IP directly, without the router in the middle).

Anyway, bottom line: see if you still get the 502 error. If you do, then it's most likely coming from Apache and there's something wrong with your config files. If it works OK via the private IPs, but when you go from the outside and access this server through the router you get the 502 error, then the issue lies with your router configuration.

My guess: it's the router. You don't seem to have anything resembling reverse proxying in your Apache config.

Final note: Make sure you have only one and only one NameVirtualHost line in all your .conf files. If you have more than one, or, if your VirtualHost parameters don't correspond exactly to your NameVirtualHost parameter, you will get weird, unexpected results. For example, since you already have all your virtual hosts defined like so:

VirtualHost *:80

Make sure that the one and only NameVirtualHost line in all of your Apache config files looks like this:

NameVirtualHost *:80

You should not be seeing lines like these in your logs!

[Wed Feb 12 23:46:06 2014] [warn] NameVirtualHost *:80 has no VirtualHosts

Granted, this is not a straight answer, but I hope it will bring you closer to the answer. :)