December 18, 2008...9:09 pm

Compojure on a Slicehost VPS

Jump to Comments

I recently decided to transfer my WordPress journal to a new Compojure application on EricLavigne.net. Compojure is a web development library for Clojure, a Lisp that was designed for tight integration with Java. This article describes how I configured a Slicehost virtual private server (VPS) to run a simple Compojure application. This article should be useful for me as a reference, and I hope that it will help others to get started with Compojure as well. The application will be improved in later articles until it is ready to host my journal.

Getting a slice

The first step is creating a slice. I entered my billing information (credit card required) followed by three other pieces of information: slice size, Linux distribution, and slice name. I chose the smallest slice size because it costs only $20 per month – I can increase this later if necessary. I accepted the default Linux distribution, Ubuntu 8.04.1 LTS (hardy), and you will find it easier to follow along if you make the same choice. The slice name is for your use only – it will identify your slice in SliceManager. My slice’s name is ericlavigne.

Connecting to the slice

When you have finished creating a slice, you will be given an IP address and root password for your slice. You will need an SSH connection to configure your slice. On Linux, I was able to create an SSH connection with the following command (173.45.234.188 is my slice’s IP address). If you are using Windows, you can create an SSH connection with Putty. You will be asked for your password – respond with your slice’s root password.

     ssh root@173.45.234.188

Configuring the slice

The next step is to change your root password.

     passwd

I prefer to use Sun’s Java development kit, so I needed to add “multiverse” to the /etc/apt/sources.list configuration file. The list “main restricted universe” appears several times in /etc/apt/sources.list, and we just need to add the word “multiverse” to the end of that list. Sed can perform that replacement for us.

     sed -ibackup 's/universe/universe multiverse /' /etc/apt/sources.list

Then it’s time to install a few tools and configure one of them.

     apt-get install git-core
     apt-get install sun-java6-bin
     apt-get install ant
     apt-get install screen
     echo "startup_message off" >> ~/.screenrc

Installing Compojure

Now for the main tool – we need to install Compojure. You can ignore the warning about tools.jar.

     mkdir install
     cd install
     git clone git://github.com/weavejester/compojure.git
     cd compojure
     ant

Creating a web application

The following commands create a new directory called “site” for the website and create a script called “run” that will run the web application.

     cd ..
     mkdir site
     cd site
     echo -n "java -cp " >> run
     echo -n "/root/install/compojure/compojure.jar:" >> run
     echo -n "/root/install/compojure/deps:" >> run
     echo -n "/root/install/compojure/deps/clojure.jar:" >> run
     echo -n "/root/install/compojure/deps/clojure-contrib.jar:" >> run
     echo -n "/root/install/compojure/deps/jetty-6.1.14.jar:" >> run
     echo -n "/root/install/compojure/deps/jetty-util-6.1.14.jar:" >> run
     echo -n "/root/install/compojure/deps/servlet-api-2.5-6.1.14.jar:" >> run
     echo ". clojure.lang.Repl  site.clj" >> run
     chmod +x run

Finally, it’s time to create and run a simple web application. It doesn’t need to be any more complicated than “hello world” for now – we can improve it later.

The following command creates a new file called “site.clj” for the web application’s source code.

     nano site.clj

After the above command, you should be running a simple text editor called “nano.” Type the following code (or copy and paste). Then hold ctrl while pressing x to exit the editor. Yes, you do want to save, and the default location (/root/site/site.clj) is the right place.

; Indicate which libraries we need, and the
; shorter names by which they will be called.
(ns site
     (:require [compojure.http.servlet :as servlet])
     (:require [compojure.http.routes :as routes])
     (:require [compojure.server.jetty :as jetty]))

; Respond to any request by saying "Hello."
(servlet/defservlet hello-servlet
     "A hello world web application"
     (routes/ANY "/*" "Hello."))

; This server will run on port 80 and use
; the hello-servlet that is defined above.
(jetty/defserver hello-server
     {:port 80}
     "/*" hello-servlet)

; Command to start the server
(jetty/start hello-server)

Now it’s time to test whether everything is working. Type the following command to start the application.

     ./run

You can view your website in a web browser. My slice’s IP address is 173.45.234.188, so I type the following into the address bar of my browser.

     http://173.45.234.188

If everything is working, you should now see the word “Hello.” Congratulations!

Keeping the application running

Just one final issue. The way we have it set up so far, the web application will shutdown when you close your terminal. We need this application to run for months at a time so that everyone can read your greeting. The solution is screen. I recommend reading more about screen in this screen introduction, but for now just follow along as I show how screen’s detach feature will allow your application to continue running when you log off, and how its reattach feature will let you continue using your application’s Clojure prompt when you return.

Hold ctrl and press d to shutdown your web application. Then start your screen session with the following command.

     screen

Now start the application again. It’s the same command, but now screen will keep the application running even if you detach.

     ./run

In order to detach from screen, you can hold down ctrl, press a, release ctrl, and press d. The d is short for detach, and ctrl-a is used for almost all screen commands. While you are detached, the application is still running (try reloading your browser to test this). If you close your terminal while you are using screen, it automatically detaches, so your program keeps running.

Try reattaching to screen with the following command.

     screen -r

Your prompt is back, just like you left it. You will need to do this every time you log into your virtual private server.

This simple web application is just the beginning. Expect more articles on Compojure as I build a web application to host my WordPress journal.

16 Comments


Leave a Reply