*** 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: http://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 http://itworked.deqb.com and still see your main page.

Technorati Tags: , wildcard dns, , , ,