May 212012

When you start operating Apache HTTPD webserver with a lot of virtual hosts, the configuration can become quite unwieldy and difficult to debug. It can become an unfortunate process of trial and error to work out whether all of your virtual hosts are up and running, and which one is the default (it’s the first one alphabetically, unless specified). In fact, working out the name of your default virtual host is often the key to solving many Apache problems.

But there is a rather nice and quick debugging trick for getting Apache to just dump out a list of all its running virtual hosts. The command line is simply this:

  # httpd -D DUMP_VHOSTS

Which outputs:

VirtualHost configuration: (/etc/httpd/conf.d/secure_website.conf:4)
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
      default server (/etc/httpd/conf.d/website.conf:1)
      port 80 namevhost (/etc/httpd/conf.d/website.conf:1)
      port 80 namevhost (/etc/httpd/conf.d/website.conf:10)
      port 80 namevhost (/etc/httpd/conf.d/website.conf:19)
      port 80 namevhost (/etc/httpd/conf.d/website.conf:29)

Hurrah! That’s a list of the URLs of all virtual hosts, and the conf.d files in which they’re configured.

And better yet, if you run this command with the “-t” switch, it will instead parse the configuration files, and not the running configuration:

  # httpd -t -D DUMP_VHOSTS

So you can test your configuration (note that it also performs a syntax check) before loading it.

Finally, when you’re satisfied that your changes are correct, reload the configuration:

  # apachectl configtest
  # apachectl restart

Or, on Fedora:

  # apachectl configtest
  # service httpd reload

Special bonus Apache debug information!
If you run httpd on the command line with the “-L” switch, you get a list of all recognised directives, their descriptions, and the contexts in which they are allowed. It’s extremely handy to have this information to hand, and to not have to revert to the manual every time.

  # httpd -L
<Directory (core.c)
  Container for directives affecting resources located in the specified directories
  Allowed in *.conf only outside <Directory>, <Files> or <Location>
<Location (core.c)
  Container for directives affecting resources accessed through the specified URL paths
  Allowed in *.conf only outside <Directory>, <Files> or <Location>
<VirtualHost (core.c)
  Container to map directives to a particular virtual host, takes one or more host addresses
  Allowed in *.conf only outside <Directory>, <Files> or <Location>

Matt Parsons is a freelance Linux specialist who has designed, built and supported Unix and Linux systems in the finance, telecommunications and media industries.

He lives and works in London.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>