How to List Active Apache Virtual Hosts Using Command Line (apache2ctl -S Explained)


2 views

When working with Apache HTTP Server, administrators often need to verify which virtual hosts are actually loaded and listening for requests. While examining config files (typically in /etc/apache2/sites-available/) gives you the configured hosts, it doesn't confirm their active status.

The most reliable method is using Apache's built-in tool:

sudo apache2ctl -S
# or alternatively:
sudo apache2 -S

This command outputs three crucial pieces of information:

  • Main server configuration path
  • Loaded virtual host configurations
  • Listening IP:port combinations

Here's what a typical output looks like:

VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server example.com (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost example.com (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost api.example.com (/etc/apache2/sites-enabled/api.conf:5)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"

Key elements to note:

  • The asterisk (*) indicates listening on all interfaces
  • Port number (80 in this case) shows the listening port
  • Configuration file paths reveal where each host is defined

For systems without apache2ctl, you can use:

apachectl -t -D DUMP_VHOSTS
# Or using httpd directly:
httpd -S

Cross-validate with system network tools:

sudo netstat -tulpn | grep apache
# Or using ss:
sudo ss -tulpn | grep apache

To extract just the virtual host names:

apache2ctl -S 2>&1 | grep "port 80 namevhost" | awk '{print $NF}'

This pipeline:

  1. Runs the status command
  2. Filters for active name-based virtual hosts
  3. Extracts the last field (hostname) from each line

If you encounter issues:

  • Ensure you have sudo privileges
  • Verify Apache is running (sudo systemctl status apache2)
  • Check for syntax errors (sudo apache2ctl configtest)

When managing Apache web servers, it's crucial to distinguish between what's configured in files versus what's actually loaded and active in the running server. While configuration files contain all potential virtual hosts, the running instance may have some disabled or in error states.

The most reliable way to list currently active virtual hosts is:


apachectl -S
# or alternatively:
httpd -S

This command shows the parsed and loaded configuration, including:

  • VirtualHost IP:port combinations
  • Server names for each host
  • Configuration file locations
  • Active ports and their corresponding hosts

Here's a sample output from a production server:


VirtualHost configuration:
*:443 is a NameVirtualHost
default server example.com (/etc/apache2/sites-enabled/example.com.conf:1)
port 443 namevhost example.com (/etc/apache2/sites-enabled/example.com.conf:1)
port 443 namevhost api.example.com (/etc/apache2/sites-enabled/api.conf:3)
*:80 is a NameVirtualHost
default server example.com (/etc/apache2/sites-enabled/example.com.conf:15)
port 80 namevhost example.com (/etc/apache2/sites-enabled/example.com.conf:15)
port 80 namevhost status.example.com (/etc/apache2/sites-enabled/status.conf:1)

Listing Only Server Names


apachectl -S 2>&1 | grep "namevhost" | awk '{print $4}'

Checking Which Config Files Are Loaded


apachectl -S 2>&1 | grep "conf:" | awk -F'(' '{print $2}' | cut -d':' -f1 | sort -u

Verifying Listening Ports


ss -tulpn | grep apache
# or:
netstat -tulpn | grep apache

While you could scan config files with:


grep -hr "ServerName" /etc/apache2/sites-enabled/

This approach misses important runtime context:

  • Disabled virtual hosts that are still in config files
  • Configuration errors that prevented some hosts from loading
  • Runtime modifications through .htaccess files
  • Port binding conflicts

If you get "NameVirtualHost has no VirtualHosts" warnings:


# Check for mismatched ports:
apachectl -S | grep "VirtualHost"
# Verify your Listen directives:
grep "Listen" /etc/apache2/ports.conf