How do I create subdomain names dynamically?

10,368

Solution 1

You can make a CNAME entry/ A Record in your DNS settings, for each subdomain

A CNAME record is a record in your Domain Management Settings that allows you to control a subdomain of your domain.

To automate it along with registration, you can write a script which is executed for each user, when s/he registers.

You can refer to this link, as well, for a step-by-step process for Apache:

How to setup subdomains in apache

Alternate Solution

You can also refer to an easier wildcard solution, given by Alnitak.

You can either use a specific DNS (CNAME or A/AAAA) entry for each known subdomain, or a wild-card DNS entry that'll accept *.example.com:

$ORIGIN example.com
foo     IN A 12.34.6.78
bar     IN A 12.34.6.78

or

$ORIGIN example.com
*       IN A 12.34.6.78

The advantage of this latter is that no changes are required to either DNS or Apache configuration once the service is running. The disadvantage is that all such wildcard lookups must (by definition) end up returning the same IP address.

The Apache configuration will depend on your requirements, both for end-user control and security. Note that if the users have permission to run CGI scripts on the server then additional setup will be needed to ensure that that's done securely.

Depending on whether content is static or dynamic this will also affect your configuration:

  1. Use mod_vhost_alias to map individual virtual hosts into their individual directories on the server.

  2. If you really want, create a separate <VirtualHost> section for each known site, but then you'll have to restart Apache each time a new user signs up

  3. Use a single <VirtualHost> and then look at the hostname part of the requested URL (from the $SERVER_NAME environment variable) in the scripts that render the output to figure out which user's content to display.

Solution 2

The creation of subdomains is built into Apache, look into mass vhosting. Specifically: http://httpd.apache.org/docs/2.0/vhosts/mass.html

All you'll need to do then is come up with a convention, and then have your script create directories when needed. But, technically you want to start with the apache mass vhost configuration setup.

Solution 3

Don't actually create the subdomains, but instead just use a wildcard subdomain so you can have http://anything.example.com.

Solution 4

This is actually a DNS question, not an Apache one unless you have a *.example.com record pointing to said webserver. If the client can't resolve subdomain??.example.com, the webserver is irrelevant.

Share:
10,368
Admin
Author by

Admin

Updated on September 17, 2022

Comments

  • Admin
    Admin almost 2 years

    I would like to be able to create and remove subdomain name dynamically using scripting languages such as PHP, Python, Perl or others. I am using Apache as a web server

    Example:

    http://subdomain.example.com
    http://subdomain2.example.com
    

    thanks

  • Cian
    Cian almost 15 years
    The need for an A record for *.example.com is also worth pointing out.
  • Eddie C.
    Eddie C. over 6 years
    First link dead of this date — which is exactly why this should be an answers site, not a link to answers site.