First you need to setup a wildcard for the domain in the DNS records e.g.
Code:
*.example.com. IN A 1.2.3.4
Second you need to edit httpd.conf to set the ServerName to the same wildcard, e.g.
Code:
<VirtualHost 1.2.3.4>
ServerName www.example.com
ServerAlias *.example.com
....
</VirtualHost>