2

I am looking to be able to build a script that will display all the virtual hosts running under my Apache (Ubuntu) development machine.

To date, I've not found any examples or anything close to what I want, which simply is:

"Display all virtual hosts running on this machine". (ServerName, Alias, etc).

I was thinking I might be able to read /etc/apache2/sites-available folder which houses all the conf files for the vhosts, but before going down that road, I thought I'd ask here first.

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
Scott Fleming
  • 452
  • 6
  • 14
  • 1
    I think reading the conf file is the only way to go. – chrislondon Mar 26 '13 at 20:34
  • 1
    The vhost files could be anywhere or part of the main conf file. If apache doesn't have an api for getting the defined vhosts I don't see any easy way of going about this. Unless you're guaranteed a certain install profile. – Mike B Mar 26 '13 at 20:35

1 Answers1

4

You can use the apachectl (or apache2ctl) command to signal the httpd Apache server to dump virtual host information:

 apache2ctl -St

Here are links to using apachectl to send the signals and httpd flags.

Here's what it looks like on my Apache / Ubuntu system (I am a web developer with a bunch of virtual hosts):

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server 127.0.1.1 (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost 127.0.1.1 (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost adaleco (/etc/apache2/sites-enabled/adaleco:1)
         port 80 namevhost amcham (/etc/apache2/sites-enabled/amcham:1)
         port 80 namevhost cake (/etc/apache2/sites-enabled/cake:1)
         port 80 namevhost cakeplate (/etc/apache2/sites-enabled/cakeplate:1)
         port 80 namevhost coord (/etc/apache2/sites-enabled/coord:1)
         port 80 namevhost dru1 (/etc/apache2/sites-enabled/dru1:1)
         port 80 namevhost flaming (/etc/apache2/sites-enabled/flaming:1)
         port 80 namevhost flaming6 (/etc/apache2/sites-enabled/flaming6:1)
         port 80 namevhost food (/etc/apache2/sites-enabled/food:1)
         port 80 namevhost fun (/etc/apache2/sites-enabled/fun:1)
         port 80 namevhost marc (/etc/apache2/sites-enabled/marc:1)
         port 80 namevhost sugar (/etc/apache2/sites-enabled/sugar:1)
         port 80 namevhost vol (/etc/apache2/sites-enabled/vol:1)
         port 80 namevhost xmas (/etc/apache2/sites-enabled/xmas:1)
Syntax OK
John Jesus
  • 2,284
  • 16
  • 18
  • Hey, Scott, does this work for you or did you have something else in mind (not sure if by script you wanted a native PHP API)? – John Jesus Mar 27 '13 at 03:10
  • What I wanted was a php script that when activated on my local machine would simply spit out the display shown above, so I wrote a quick php script to read the sites-enabled directory, and just spit out the contents of each file. I think I'll rework it to use the apache2ctl command, though. Thanks, John. I'll post my script when I have it completed. – Scott Fleming Mar 29 '13 at 14:51
  • Is there any command argument for windows httpd.exe exists? – aniruddha Feb 17 '21 at 12:32