← blog

Tutorial: Use G-WAN as a static content server (poors' CDN)

G-WAN is an ultra-fast, ultra-light and ultra-reliable web application server. We can use it as a static content server instead of nginx, Lighttpd or Apache (as it outperforms them all). It is kind of like a poor VPS guy’s CDN but is very effective when you have a site with medium traffic. Oh! And a very important note, G-WAN is not “Free Software” but a freeware. You will be charged for support.

Installing G-WAN

G-WAN is available for Debian based distributions (not sure whether will work on others) on 32-bit and 64-bit architectures. Download the appropriate one that suits you. Install the libc6-dev package. (Note: 64-bit users should fetch the libc6-dev-i386 package instead.) Extract the archive you have downloaded. You should get a folder like gwan_linux32-bit or gwan_linux64-bit according to your selected architecture. Move, copy or rename the folder to a location of your comfort and we are done!

apt-get install libc6-dev
wget "http://gwan.com/archives/gwan_linux32-bit.tar.bz2"
tar xvf gwan_linux32-bit.tar.bz2

Configuring G-WAN

Here comes the fun part. Configuring G-WAN is 320,000x times simpler than configuring nginx, Apache or lighttpd! If you browse to the directory of gwan, you will find a directory named “0.0.0.0_8080. 0.0.0.0 is the IP Address and 8080 is the port the server is going to bind to. You can create your own directory or just rename it two suit your needs. Just remember one thing, the IP address and port number is separated by an underscore.

G-WAN directory structure

Inside the IP Directory, we need to create directories for our domains. All you need to do is create a directory in the name of your domain followed by a hash or pound sign. For example, if your domain name is “static.example.com”, your directory name would be “#static.example.com” (without the quote marks). But, if you want to access it as “localhost”, it is better to keep it “#0.0.0.0 Inside the domain directory, we need to create a directory named “www”. This is basically the Document Root of G-WAN and “www” doesn’t have to be a directory itself, it can also be a softlink to a directory.

Configuration boils down to these three things:

  • You must have a directory in {ipaddress}_{port}* format in Server Root.

  • Under the IPPort directory, you should have a directory in #{domain} format.

  • Inside the Domain directory, there should be a “www” directory, all your files that need to be served go under that directory.

cd gwan_linux32-bit
mkdir -p 111.222.333.444_80/\#static.example.com/www
cd 111.222.333.444_80/\#static.example.com/www

There is one thing I need to explain however. Have you noticed that I have put a backslash before the pound (#) sign? Well, that is because the bash shell treats # as the comment sign. If you use the pound sign directly, everything after the pound sign will be treated as a comment and will not be parsed. The backslash tells bash that “I don’t mean a comment, I really need a directory that has a pound in it’s name!

Hope you got how G-WAN structures stuff! The configuration bit is upon you. You can use C, D or Java Scripts with your server (that is what G-WAN is originally designed for) but it is out of the scope of this post.

Testing if it works

Testing is always good before going into production. Jump to the directory where the extracted files of G-WAN live. If you are using a graphical shell, it is better to use a Terminal Emulator. So, in a terminal type in,

./gwan

You will immediately see (that is, after pressing enter) G-WAN running and the terminal will show you something like this:

G-WAN 3.3.28 (pid:3676)

That means G-WAN is running. Open up your browser and type in the address. And you should see a page like this one.

Servicifying (!) G-WAN

I think you already noticed that G-WAN doesn’t really run as a daemon but runs kind of like a development server (like Django, webapp). We can start it in daemon mode with the -d argument. But, I want to make it a service so that I can simply say “service gwan start” and “service gwan stop” like I do with nginx, MySQL, PHP-FastCGI.

First, browse to /etc/init.d/. Create a file named gwan with the following content:

#! /bin/sh -e
#
# Author: Aniruddha Adhikary
# Description: This file starts/stops/restarts the G-WAN Static Server.

### BEGIN INIT INFO
# Provides: gwan
# Required-Start: $null
# Required-Stop: $null
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: gwan server for Static CDN
# Description: gwan server for Static CDN
### END INIT INFO

case "$1" in
start)
  echo "Starting G-WAN Server"
  /usr/bin/gwan/gwan -d:www-data:www-data
  ;;
stop)
  echo "Stopping G-WAN Server"
  /usr/bin/gwan/gwan -k || echo "G-WAN Server is not running!"
  ;;
restart)
  $0 stop
  sleep 2
  $0 start
  ;;
*)
  echo "Usage: /etc/init.d/gwan {start|stop|restart}"
  exit 1
  ;;
esac

Save the file. Now, give it permission to execute and register it as a service:

chmod u+x gwan
update-rc.d gwan defaults

Now, you are ready to go. Keep the following commands handy:

service gwan start
service gwan stop
service gwan restart

For any help, visit their official website or have a look at the user manual.