Setting up Wildcard DNS & Subdomains on cPanel
*** UPDATE: DON’T READ THIS POST… JUST READ COMMENT #2 FROM CPANEL ***
I’ve posted instructions in the past on how to set up wildcard DNS and subdomains on DirectAdmin. DirectAdmin makes it rather trivial, which is quite nice. There is a little bit more effort involved in cPanel – manly because you actually have to make some modifications via SSH. So I would say this tutorial involves and intermediate skill level to accomplish (it can’t be more because this is about the extent of my skill level). You will need access to WHM and root access to your server.
Background: I’m currently learning Django and trying to create a solution for sub-domaining accounts. I’ve been told by a few people that this will be the hardest part to set up. At my currently Django level, this part is trivial. I hope to make more posts when I get the Django parts figured out as well. Stay tuned.
Step 1: DNS: A Record
First we’ll need to log in to WHM. Under DNS Functions, select Edit DNS Zone. Choose the domain for the zone in which you wish to edit. Add an A record mapped to asterisk (wildcard) for the subdomain and the IP Address the site is hosted on. You likely already have A records for ftp, webmail, etc. just model this new one after those.

You should now be able to enter any subdomain on your domain, but it will not likely find your main site. So now we need to set that up.
Step 2: ServerAlias
Log in to your server via SSH and go open /etc/httpd/conf/httpd.conf (I’ll assume you know your way around linux via command line as well as vi or some other editor)
*Note: wherever you see deqb.com you should expect to see your domain name that you are setting up
Now, we could edit this file and make everything work, but there is a problem with that. Look at the top of the document and you should see something like this:
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#  Direct modifications to the Apache configuration file may be lost upon subsequent regeneration of the      #
#  configuration file. To have modifications retained, all modifications must be checked into the             #
#Â Â configuration system by running:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â #
#      /usr/local/cpanel/bin/apache_conf_distiller –update                                                   #
#Â Â To see if your changes will be conserved, regenerate the Apache configuration file by running:Â Â Â Â Â Â Â Â Â Â Â Â Â #
#      /usr/local/cpanel/bin/build_apache_conf                                                                #
#  and check the configuration file for your alterations. If your changes have been ignored, then they will   #
#  need to be added directly to their respective template files.                                              #
#Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â #
#  It is also possible to add custom directives to the various “Include” files loaded by this httpd.conf      #
#  For detailed instructions on using Include files and the apache_conf_distiller with the new configuration  #
#  system refer to the documentation at: www.cpanel.net/support/docs/ea/ea3/customdirectives.html      #
#Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â #
#Â Â This configuration file was built from the following templates:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â #
#    /var/cpanel/templates/apache2/main.default                                                               #
#    /var/cpanel/templates/apache2/main.local                                                                 #
#    /var/cpanel/templates/apache2/vhost.default                                                              #
#    /var/cpanel/templates/apache2/vhost.local                                                                #
#    /var/cpanel/templates/apache2/ssl_vhost.default                                                          #
#    /var/cpanel/templates/apache2/ssl_vhost.local                                                            #
#Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â #
# Templates with the ‘.local’ extension will be preferred over templates with the ‘.default’ extension.       #
# The only template updated by the apache_conf_distiller is main.default.                                     #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Basically what it’s saying is don’t edit this file directly because you’re changes may be lost.
Do a search for the domain you are adding the wildcard to. You should find the VirtualHost set up for your domain. Here is mine:
# DO NOT EDIT. AUTOMATICALLY GENERATED. IF YOU NEED TO MAKE A CHANGE PLEASE USE THE INCLUDE FILES.
<VirtualHost 67.225.128.241:80>
   ServerName deqb.com
   ServerAlias www.deqb.com
   DocumentRoot /home/deqb/public_html
   ServerAdmin webmaster@deqb.com
   UseCanonicalName Off
       Options -ExecCGI -Includes
       RemoveHandler cgi-script .cgi .pl .plx .ppl .perl
   CustomLog /usr/local/apache/domlogs/deqb.com combined
   CustomLog /usr/local/apache/domlogs/deqb.com-bytes_log “%{%s}t %I .\n%{%s}t %O .”
   ## User deqb # Needed for Cpanel::ApacheConf
   <IfModule !mod_disable_suexec.c>
       SuexecUserGroup deqb deqb
   </IfModule>
   # To customize this VirtualHost use an include file at the following location
   # Include “/usr/local/apache/conf/userdata/std/2/deqb/deqb.com/*.conf”
Â
</VirtualHost>
Notice the highlighted lines. This is where we are going to create a file. We are going to create a file at:
/usr/local/apache/conf/userdata/std/2/deqb/deqb.com/ServerAlias_wildcard.conf
(you can give it any name you want really)
*Hint: You may need to create the directories first, run this command:
mkdir -p /usr/local/apache/conf/userdata/std/2/deqb/deqb.com/
So really, you don’t need to edit anything it httpd.conf, we just needed some info.
Inside of the ServerAlias_wildcard.conf file, enter this line:
ServerAlias *.deqb.com
Now, you’ll need to rebuild the httpd.conf file per the instructions at the top of that file by running the following command:
/usr/local/cpanel/bin/build_apache_conf
If you open your /etc/httpd/conf/httpd.conf file again you should see that your VirtualHost for your domain as changed slightly:
<VirtualHost 67.225.128.241:80>
   ServerName deqb.com
   ServerAlias www.deqb.com
   DocumentRoot /home/deqb/public_html
   ServerAdmin webmaster@deqb.com
   UseCanonicalName Off
       Options -ExecCGI -Includes
       RemoveHandler cgi-script .cgi .pl .plx .ppl .perl
   CustomLog /usr/local/apache/domlogs/deqb.com combined
   CustomLog /usr/local/apache/domlogs/deqb.com-bytes_log “%{%s}t %I .\n%{%s}t %O .”
   ## User deqb # Needed for Cpanel::ApacheConf
   <IfModule !mod_disable_suexec.c>
       SuexecUserGroup deqb deqb
   </IfModule>
   Include “/usr/local/apache/conf/userdata/std/2/deqb/deqb.com/*.conf”
</VirtualHost>
Â
Step 3: Restart Apache
The last step required is to restart apache. I suppose there are a number of ways to do this. As long as we are logged in to SSH, we might as well just run the command:
/usr/sbin/apachectl restart
Now, give it a try. you should be aby to access something like itworked. style=”color: #006600;”>deqb.com and still see your main page.
Technorati Tags: dns, wildcard dns, subdomain, django, cpanel, whm
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
September 11, 2008
Posted in: Programming & Internet




9 Responses
Django Accounts on Subdomains | Nerdy Dork - September 11, 2008
[...] that I want to test. When I go live, I will likely remove these from the hosts files and set up wildcard dns & subdomains on the [...]
Actually, there’s a much easier way. Simply add a subdomain like you would any other subdomain in the cPanel interface, except make your subdomain * (as if to show on the screen: *.example.com). If this is not working for you in the latest builds of cPanel/WHM, please let us know.
@David,
Wow, you’re right! That is WAY easier! This post was certainly a waste of time!
@Dustin,
This is not a waste of time! Intermediate skill level? Pah. Don’t underrate yourself. I wish I had time to blog, it would do wonders for my SEO. Too busy working on other people’s sites though. Anyhow, found your site because of Google and trying to solve a cPanel/Bind issue unrelated to your post. Very nice layout. Maybe I will set up wordpress for myself one day. Good job man.
Thanks a lot Dustin! It’s working very well!
I am curious, once you have a wildcard setup and then do url aliasing so you can grab variables from your clean url domain/city/state/etc and pass them to orig.php?city=xyz&state=123 and so on, is there a way to grab the wildcard subdomain as variable #1 and pass it to the orig.php as one of the variables for the query string?
On a side note, great article, wish I had read your comments area first though, I spent a whole day yesterday on that in putty and then I just went into another account t used cpanel to create * as a subdomain and amazingly it worked! thanks for any feedback on my question.
There is now a basic email level plugin with DirectAdmin. The plugin allows you to install some php scripts into dedicated accounts which then allows any POP account to login to those scripts to access their POP account setup. This is an ideal solution if you have an account that has 20 email accounts because the interface permits each email user to log into their individual email control panel to change things like password, autoresponder, etc. It shows them their current disk usage, ability to change their password, set vacation messages (without bugging the webmaster), and provides them with the three links to the webmail programs.
Now I’m a bit puzzled, please look below
Does the cpanel method work for a plain new installation or not?
“It only works because that webhost has done the Apache edits already, or has them added to the accounts on signup.
That’s the ONLY reason.”
http://mu.wordpress.org/forums/topic/12595?replies=2#post-75173
It should work on a new installation of cPanel. I’ve had it work on a couple of different installations. The person that left the tip above works for cPanel I believe.
Comments are closed for this entry.