Monday, April 27, 2009

Controlling Your Home Web Server Content Remotely Using Dropbox


I have a home server (using Tomcat) that I use for some stuff like sharing files with family and friends and getting feedback on some web-based stuff. Recently, I was creating the website for Folder2Feed (a Java app that generates feeds from folders) and I was tweaking some CSS bits after hours at office. I wanted to check with some friends about the look and these were my options:

  1. Send the files to each person using an IM client
  2. Cons:
    • The receiver would need to be competent enough to understand what to do with the files I was sending
    • I'd have to send the files to each person
    • The other person needs to be online at the time
    • Not every workplace allows IMs
    • Resuming broken downloads isn't usually possible
  3. Use VNC to connect to my home machine and then put the updated content in the web server folders
  4. Cons:
    • It's a slow and tedious process over VNC
    • Doing this over and over for small changes is a major pain
    • Not every workplace would be setup to use VNC
  5. Wait until I got home
  6. Cons:
    • I'd have to wait
    • I'd have to remember to take the files home with me (I always forget!)

All of these are pretty viable options but where's the geek cred? Alright, so VNC does have some but still, it's painful for regular use over a slow connection. I have been using Dropbox, which is an awesome service. It's free, painless to setup and so simple, even your most technophobic friends will be able to use it. So I figured that if I could setup a way to update my web server using Dropbox, I'd be in business. The easiest way to do that would be to move my web server installation into the Dropbox folder but that would be overkill. I didn't need the complete web server setup to be shared and synced, only one particular webapp I was running on it.

Now there's this concept of vHosts, wherein a single physical server can host multiple websites. For example nogoodatcoding.com and nogoodatcoding.org could both be on the same server but that would not be readily apparent to a visitor. Most modern web servers should support vHosts - Tomcat and Apache certainly do. The one that would probably be more commonly used is name-based and that is what is used in the instructions below.

So coming to the crux of this post - you can simply add a vHost to your web server and point it to a folder inside your Dropbox home and voila! You have complete control over the content you're hosting!

 I use DynDNS and NO-IP for my dynamic DNS needs and all I had to do there was add another hostname and point it to the same machine.
 

Here's how you can do it for Tomcat:
  1. Browse to $CATALINA_HOME/conf and edit server.xml.
  2. You'll find a block of code that defines the default vHost - localhost:
    <Host name="localhost"  appBase="webapps"
    unpackWARs="true" autoDeploy="true"
    xmlValidation="false" xmlNamespaceAware="false">
    You can leave this untouched.
  3. Add another host, something like
    <Host name="myDynDNSHostNameForDropbox.dyndns.biz"  appBase="c:\\My Dropbox\\My vHost Content"
    unpackWARs="true" autoDeploy="true"
    xmlValidation="false" xmlNamespaceAware="false">
  4. Restart the server and you should be good to go.

You can find more about configuring Tomcat for vHosts at the official website: Virtual Hosting and Tomcat

For doing the same with the Apache Web Server (version 2.2; for older ones you should locate the appropriate documentation and tutorials):
  1. Edit your httpd.conf and uncomment the line for virtual hosts:
    #Include conf/extra/httpd-vhosts.conf
  2. Edit conf/extra/httpd-vhosts.conf and set it to something like:
    NameVirtualHost *:80
    
    #This will maintain your current configuration for all names except the second vHost
    <VirtualHost *:80>
    ServerName localhost
    
    #This is the relative path to the htdocs directory
    DocumentRoot htdocs
    </VirtualHost>
    
    #This vHost will answer for requests to myDynDNSHostNameForDropbox.dyndns.biz
    <VirtualHost *:80>
    ServerName myDynDNSHostNameForDropbox.dyndns.biz
    
    #The absolute path to the directory for this hosts content
    DocumentRoot "c:\\My Dropbox\\My vHost Content"
    </VirtualHost>
    
    #This apparently is required for overriding the default security; otherwise you'll be denied access
    <Directory "c:\\My Dropbox\\My vHost Content">
    Order Deny,Allow
    Allow from all
    </Directory>
  3. Restart the server and you should be good to go.

I'm not really an Apache user, I tend to stick with Tomcat. I got this information from the offical Apache docs: Virtual Hosting with Apache, Name based virtual hosting with Apache and this great tutorial on Apache Virtual Hosting.

 It's pretty much the same technique for any webserver that supports vHosts and you should find the documentation for yours and check what needs to be done.
 

Once you've set this system up, you'll be able to simply modify files in your locally synced folders under Dropbox and the changes will be reflected in a few moments on your home webserver! You could use this to put up pretty much anything on your webserver - a photo you just clicked at work, a document you want to put up to share with a friend or even some collaborative work on one single webpage or anything else!