Report this

What is the reason for this report?

Create Subdomain (on the fly) by using PHP

Posted on July 8, 2015

Hi i try to find way for user to register in my web. they just need to fill in their name,phone,email address and subdomain that need to create. this information will store in database and PHP will execute the command to create subdomain. is it possible to do that? my webhost control panel are not using cpanel , my domain i already point using DO DNS. thanks



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Pretty simple, but you’ll need to handle the coding part.

  • Create an A record at DO for a wild-card sub-domain (*.users.your-domain.com, for example)
  • In apache, create a wild-card vhost:

Example vhost config (written for CentOS 6.6)

<VirtualHost *:80>
        ServerName users.your-domain.com
        ServerAlias *.users.your-domain.com
        #### This is where you put your files for that domain: /var/www/vhosts/c.jonsjava.com
        DocumentRoot /var/www/vhosts/users.your-domain.com/
        <Directory /var/www/vhosts/users.your-domain.com>
                # This section sets directives for the directory.
                # -Indexes <-- That blocks index listings on folders that don't have a default file (index.php/index.html/default.html/etc)
                # FollowSymLinks <-- This will treat symlinks like they should be treated in linux: as folders/files in the folder the symlink resides
                # MultiViews  <--It's easier for you to read this: http://httpd.apache.org/docs/2.0/content-negotiation.html#multiviews
                Options -Indexes FollowSymLinks MultiViews
                # AllowOverride All <-- This is required for Apache HTTPD server to read .htaccess files.  It says that you can have a per-folder override for apache directives
                AllowOverride All
        </Directory>
        ## Everything to see here. Just the log files.  Good to use for troubleshooting errors.
        CustomLog /var/log/httpd/users.your-domain.com-access.log combined
        ErrorLog /var/log/httpd/users.your-domain.com-error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
</VirtualHost>
  • In your PHP, have it get the host name:
$URL = $_SERVER[SERVER_NAME];
$user= str_replace(".users.your-domain.com","",$URL);

Now, you have the user. Just have your code configured to show that users page, or a 404 if it doesn’t exist.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.