23

I want to to retrieve a list of the virtual hosts which are currently loaded and listening for requests i.e not just grepping the config files.

It looks like apache2ctl -S does this but I am not 100% sure if that is just returning what is contained in the config files.

codecowboy
  • 1,307
  • 7
  • 18
  • 31

5 Answers5

41

The command a2query -s works perfectly for me.

vishalknishad
  • 519
  • 4
  • 3
  • This was the answer for me +1 – Neo May 26 '18 at 21:02
  • Thanks that worked for me. works the same to list modules and configurations: `a2query [-m [MODULES] -s [SITE] -c [CONF] ]` – bl3ssedc0de Jan 12 '22 at 06:48
  • In Red Hat Enterprise or CentOS this command, that lists the virtual hosts which are currently loaded is ```httpd -S``` because the deamon itself is not called Apache, but ```httpd```. – michal roesler Apr 24 '23 at 13:54
8

The apachectl -S documentation says this

-S Show the settings as parsed from the config file (currently only shows the virtualhost settings).

So you will only see those virtualhosts that are loaded in the config which is read when httpd starts.

user9517
  • 115,471
  • 20
  • 215
  • 297
5

You can use this command :

apache2ctl -t -D DUMP_VHOSTS

It will list all the enabled websites, the path to the conf file and the port that is used by the website

DonJulio
  • 51
  • 1
  • 2
5

I didn't found official documentation about it.

The command apache2ctl -S check site-enabled*.conf + httpd.conf files and show you if the syntax is correct and the list of virtual host (and some more informations).

But It doesn't check if virtualhosts are running, you can try to stop your Apache and launch again the command, the result will be the same.

So apache2ctl -S result is based on configuration files.

Froggiz
  • 3,043
  • 1
  • 19
  • 30
2
apache2ctl -S | grep -o -E 'alias (.*)|(namevhost|server) (.*)\s' | cut -d ' ' -f 2

Will output the list of configured domains and aliases

Works fine on Apache/2.4.38 (Debian)

  • 1
    I like this, but it's interesting that it (correctly) shows 20 vhosts on my system with 14 unique domain names (because HTTP and HTTPS), while `certbot --apache` lists 14 names that don't entirely match. `apache2ctl` *only* lists the `ServerName` values, skipping the `ServerAlias` (while certbot somehow knew to skip the vhosts I had explicitly broken by prepending the domain name with **xxx_**) – Auspex Dec 09 '22 at 11:46