19 November 2009 - 20:13find out if your Apache is really serving the right hosts

From time to time it can happen that websites that used to be on your webservers are being moved by your clients without notifying you. Therefore, you will have to search for «zombie websites».

I have the following setup: There is an Apache webserver that has a lot of websites. Each website has at least two Virtual Hosts, a first for the main website and a second one fetching all the aliases and redirecting them to the first. Each website has its own config file.

So, if I want to know whether a website is still being served by my machine, I run the following:

for host in `grep "Server\(Alias\|Name\)" _config/apache/*
| awk '{ for(i=3 ; i<=NF ; i++)printf "%s\n", $i}'`; do echo
 -n `nslookup "$host" | grep -A1 Name | grep Address | awk
'{print $2}'`; echo " $host"; done


Okay, let’s go through it step by step:


for host in `grep “Server\(Alias\|Name\)” _config/apache/*

Here we extract all lines containing ServerAlias or ServerName in all config files.  The output will look like

_config/apache/foo.bar.conf: ServerName www.foo.bar
 _config/apache/foo.bar.conf: ServerAlias foo.bar fuh.bar www.foo.bar
_config/apache/example.com.conf: ServerName www.example.com
_config/apache/yah.conf: ServerAlias    example.com

As we can see, the results have a varying size. Therefore, we have to tell awk about it.


| awk ‘{ for(i=3 ; i<=NF ; i++)printf “%s\n”, $i}’`

Awk takes each line and starts to read it from the third column (each seperated with a space) until EOL. What we get is being printed with a CRLF so that we do not get example.com fuh.bar www.foo.bar but

example.com
fuh.bar
www.foo.bar

do echo -n

Now we print each of the results of the following command without a CRLF afterwards.


`nslookup “$host” | grep -A1 Name | grep Address | awk ‘{print $2}’`;

Here we perform a IP lookup of the current host, grep for a string called “Name” and let it print the following line as well as it contains the IP address we are looking for. As we just need the IP address, we do another grep on the result and pick the second line. But we don’t want the “Address” string here, so we awk it away. To make it a little bit easier to understand, I will quickly show you what these commands do:

nslookup example.com
Server:         213.133.100.100
Address:        213.133.100.100#53
Non-authoritative answer:
Name:   example.com
Address: 192.0.32.10
---
nslookup example.com | grep -A1 Name
Name:   example.com
Address: 192.0.32.10
---
nslookup example.com | grep -A1 Name | grep Address
Address: 192.0.32.10
---
nslookup example.com | grep -A1 Name | grep Address | awk '{print $2}'
192.0.32.10


echo ” $host”

Now we have an IP but we would like to know what the corresponding hostname is, so we quickly echo it.

Done. Afterwards we can see:

192.0.32.10 www.example.com
yetanotherhost.mil

No Comments
Categories: Linux, Webserver, howto
Tags: , , , , , , , , , , , , , , , ,

28 August 2008 - 14:46creating apache vhosts with style

We decided to have a reverse domain name structure that conatains (beside the htdocs) all vhost-related information like logs, stats and config.

The reverse structure is neccessary to get an quick overview not about the subdomains but the domains.

Additionally, we wanted to have a script which sets up the structure and creates the necessary files. If executed, the script fetches the domain name and if it starts with www a permanent redirect from example.com to www.example.com is being added as well. Moreover, we create a awstats config as well. In order to get this working properly, you will need a preconfigured awstats configuration (/etc/awstats.model.conf).

Okay. This would be the structure for the host www.example.com:

/WEBROOT/com.example.www:

conf
htdocs
logs
stats

/WEBROOT/com.example.www/conf:

awstats.www.example.com.conf
vhost.conf

/WEBROOT/com.example.www/htdocs:

/WEBROOT/com.example.www/logs:

access
error

/WEBROOT/com.example.www/stats:

As setting this up can be somewhat boring, we created a little script which can be downloaded here.

No Comments
Categories: Webserver, howto
Tags: , , , , ,

blogoscoop