Maintaining Apache Web Server

by Thomas Urban

Apache is a very popular server software for providing web sites and portals. A single instance is capable of hosting a huge amount of so called virtual hosts each addressable by one or more names in turn. Another common pattern in running Apache web server is using separate files for each virtual host.

With the following bash command it is possible to fetch a processable list of all host names currently configured for addressing virtual hosts of an Apache server instance:

grep -hirE 'ServerName|ServerAlias' /etc/apache2/vhosts.d/http/* | tr ' ' '
' | awk '$1 && $1 !~ /^Server/ {print $1}'
The line break is intended and required for replacing single space characters by single line breaks.

The resulting list might be used e.g. for detecting host names not in use anymore. This might be due to missing any DNS records resolving those hostnames. The next example is embedding previous command for listing hostnames that fail in DNS resolving:

for domain in $(grep -hirE 'ServerName|ServerAlias' /etc/apache2/vhosts.d/http/* | tr ' ' '
' | awk '$1 && $1 !~ /^Server/ {print $1}'); do getent hosts "$domain" &>/dev/null || echo $domain; done

By slightly adjusting this second command it is also possible to check all your configured hostnames basic availability by means of being requestable over HTTP by invoking cURL:

for domain in $(grep -hirE 'ServerName|ServerAlias' /etc/apache2/vhosts.d/http/* | tr ' ' '
' | awk '$1 && $1 !~ /^Server/ {print $1}'); do curl "http://$domain" &>/dev/null || echo $domain; done

According to the number of hostnames listed in output of first command the second and third example might take a while to complete.

Detecting Moved Sites

Previous commands were used to list hostnames of hosted web sites that aren't available anymore. However either command failed to check if a site is still hosted by locally tested Apache instance. While second command above was using getent for detecting hostnames not resolved by DNS anymore the following command is using getent for listing IP every hostname is resolved into:

grep -hirE 'ServerName|ServerAlias' /etc/apache2/vhosts.d/http/* | tr ' ' '
' | awk '$1 && $1 !~ /^Server/ {print $1}' | xargs -- getent hosts

The next command provides a brief summary of all IP addresses hostnames are currently resolving into:

grep -hirE 'ServerName|ServerAlias' /etc/apache2/vhosts.d/http/* | tr ' ' '
' | awk '$1 && $1 !~ /^Server/ {print $1}' | xargs -- getent hosts | awk '{print $1}' | sort | uniq

With the following command you can get all IPs of your server for comparing with list of previous command:

LC_ALL=POSIX ifconfig | awk '$1 == "inet" {print $2}'

Go back