Create Subdomain (on the fly) by using PHP

Posted July 8, 2015 23.5k views
PHPDigitalOceanPerlAPIDNSLinux Commands

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

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.

Submit an Answer
1 answer

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

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

Example vhost config (written for CentOS 6.6)

<VirtualHost *:80>
        ServerAlias *
        #### This is where you put your files for that domain: /var/www/vhosts/
        DocumentRoot /var/www/vhosts/
        <Directory /var/www/vhosts/>
                # 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:
                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
        ## Everything to see here. Just the log files.  Good to use for troubleshooting errors.
        CustomLog /var/log/httpd/ combined
        ErrorLog /var/log/httpd/

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
  • In your PHP, have it get the host name:
$user= str_replace("","",$URL);

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

  • Any suggestions on how we can create dynamic database for each user using PHP (as an when they register)? I am using the below code - it works on the local server, but not working in the live server on digitalocean. Is this a permission issue to create DB?

    public function setupNewDatabaseWithSubdomainName($domainname = “) {
    $db2 = array(
    ‘dsn’ => ”, 'hostname’ => $this->hostname, 'username’ => $this->username, 'password’ => $this->password, 'database’ => $domain
    'dbdriver’ => 'mysqli’, 'dbprefix’ => “, 'pconnect’ => FALSE, 'dbdebug’ => (ENVIRONMENT !== 'production’),
    on’ => FALSE, 'cachedir’ => ”, 'charset’ => 'utf8’, 'dbcollat’ => 'utf8generalci’, 'swappre’ => “,
    'encrypt’ => FALSE, 'compress’ => FALSE, 'stricton’ => FALSE, 'failover’ => array(), 'save_queries’ => TRUE

        $sql_db = "CREATE DATABASE IF NOT EXISTS " . $domain_name;
        $this->dbNew = $this->load->database($db2, TRUE);
        $arr_table_lists = $this->retun_all_tables_from_database($this->db->database);
        foreach ($arr_table_lists as $tbl_name) {
            $sql_new = "";
            $sql_new = "SHOW CREATE TABLE " . $this->db->database . "." . $tbl_name->table_name;
            $query = $this->db->query($sql_new);
            $result = $query->result_array();
            $sql_new = strtolower($result[0]['Create Table']);
            $sql_new = str_replace("create table", "create table if not exists ", $sql_new);