Automatically manage subdomains (without editing vhosts) apache

November 23, 2016 2k views
DNS Apache Ubuntu 16.04


I've decided to give digitalocean a try. I wanted to have more control of my remote dev enviroment but I guess it was more of the need to get my hands dirty with server management.

I've bought domain with local hosting and would like to point subdomains with projects on my Digital Ocean VPS.

Is there a way to setup vhosts in a way that they would simply point to a specific folder that when I create a new folder it would be automaticall "mapped"? Also do I have to create the specific subdomain with my local hosting service every time i need a new one? Or if i point my domain to the droplet all new subdomains will automatically work?

What are my options here? Thanks!

1 Answer
xMudrii November 23, 2016
Accepted Answer

You can try mod-vhost-alias.

This module creates dynamically configured virtual hosts, by allowing the IP address and/or the Host: header of the HTTP request to be used as part of the pathname to determine what files to serve. This allows for easy use of a huge number of virtual hosts with similar configurations.

You can use VH like following:

GNU nano 2.5.3 File: /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        VirtualDocumentRoot /var/www/%0/html

        <Directory /var/www/%0/html/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

I tested it and it worked for me.
%0 means it will substitute it with full link. So if someone goes, it'll use it.
You can also use %1, to use only first part. For more look at above link provided.

Edit, Just to note, you need to add wildcard CNAME record @ to make desired effect.

  • Hi! thanks for your answer.

    So just so i understand it correctly. When i create a new folder inside /var/www/ let's call it mysubdomain and create html folder and put website files in there it should work, given that my DNS records are properly set up, right?

    • Yes.

      I forgot to note in initial post, you need to enable adequate module on Apache:

      • sudo a2enmod vhost-alias

      Don't forget to restart Apache after it:

      • sudo systemctl restart apache2

      With that, your Web site will work.

      Now you can do additional tweaking if your site requires it.
      For example if you need to write in that folder or change files from Web (browser) you need to adjust permissions and ownership:

      • sudo chown -R sammy:www-data /var/www/subdomain-folder/html
      • I already had this whole reply prepared with the contents of my configuration files etc.

        Then I've decided to double check your first post and realized that i was creating only the first part of the URL i.e test when i should have been creating

        In case somebody needs this please note that the correct command to enable mod-vhost-alias is:

        sudo a2enmod vhost_alias

        @xMudrii Thank you very much for your help!

Have another answer? Share your knowledge.