Oct 28 2013

Howto: Dynamic Virtual Hosts in Synology

Dynamic Virtual Hosts

When dealing with several clients and software that requires to be run on a dedicated domain, running Apache virtual hosts is a must. If you need many vhosts you will end up copy-pasting basically the same Apache configuration all over again. But instead of duplicating the entries you can use Apache’s dynamic virtual hosts configuration where you create a pattern for all future virtual hosts and then just place new vhosts in a correct place in directory hierarchy and voilĂ , it works automatically! Here’s how to do it.

Apache Configuration

The Apache configuration part is well documented. In Synology, login as root and edit the conf file (for example with vi) /usr/syno/apache/conf/httpd.conf-user:

#<VirtualHost *:80>
#Include /usr/syno/etc/sites-enabled-user/*.conf
#</VirtualHost>

# Dynamic Virtual Hosts
# Get the server name from the Host: header
UseCanonicalName Off

# This log format can be split per-virtual-host based on the first field
#LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
#CustomLog logs/access_log vcommon

# Include the server name in the filenames used to satisfy requests
VirtualDocumentRoot /volume1/web/%0/html

Notice that I have commented out the normal virtual hosts definition. Virtual document root points to Synology’s default web root directory where ‘%0’ acts as a placeholder for different virtual host domains. That is all, just restart Apache (/usr/syno/etc.defaults/rc.d/S97apache-user.sh restart) and you are done.

Now if I wanted to create a new virtual host www.domain.com, all I need to do is to create a directory /volume1/web/www.domain.com/ and a directory html inside of it where I would place my code. The root directory www.domain.com could have for example domain specific configurations that must not be exposed to the Internet.

This configuration breaks some of the built-in services but there are workarounds which follow.

Landing Page

This configuration breaks the default Synology forwarding to port 5000 and the web-UI. Luckily this is easy to fix by creating a special landing page where you can for example list your services (web-UI, photo, phpMyAdmin, public web pages, etc) or do the forwarding to port 5000 yourself. Just create a directory /volume1/web/your.dynamic.hostname/html where your.dynamic.hostname is for example your host name from dynDNS or other dynamic name service. Inside the html directory you can place index.php where you can define services or to automatic redirecting.

Photo Station

Unfortunately the dynamic virtual hosts configuration breaks the built-in Photo Station because Synology can no longer respond to requests to URL /photo. You can fix this by enabling HTTPS and accessing the Photo Station with HTTPS protocol, e.g. https://your.dynamic.hostname/photo/. Login to Synology web-UI as admin and then go to Control Panel → Network Services → Web Services → HTTP Service tab → Tick “Enable HTTPS connection for web services”.

phpMyAdmin

The default installation via Package Center stops working after you have enabled dynamic virtual hosts. You need to copy phpMyAdmin directory from /volume1/web/phpMyAdmin to :/volume1/web/your.dynamic.hostname/html/phpMyAdmin. After the move phpMyAdmin will work as before and will respond at http://your.dynamic.hostname/phpMyAdmin/.

Conclusion

Setting up dynamic virtual hosts takes some time and requires you to do modifications for the Synology built-in services but you only need to do it once and it will save you tons of time if you need to set up new virtual hosts frequently.

Leave a Reply