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.
15 Comments
December 21, 2008 at 1:36 am
I’ve been meaning to start learning Compojure. This was the perfect kick-start. Thanks!
December 21, 2008 at 11:11 am
Thanks for the feedback, rzezeski. Building a Compojure application has been a lot of fun so far. If this article inspires someone else to start building, then it was definitely worth writing. Hopefully I can finish a second article in the next few days.
December 28, 2008 at 3:01 am
[...] application that will host my journal on EricLavigne.net. In a previous article, I showed how to setup Compojure to run on a Slicehost VPS. The next step is to setup a database to store the journal articles. In this article I will show [...]
December 30, 2008 at 6:52 pm
I can only agree with rzezeski, this was a great kickstart! I have developed a couple of small web apps using webjure, another web framework for clojure which runs under Tomcat (or similar, I assume), but development of it seems to have stopped. Also, compojure looked more powerful the last time I checked.
Thanks!
January 7, 2009 at 10:30 am
[...] Of course, it would be fun to put this on the web, behind a form for uploading texts or linking to online texts. A Clojure program should be able to create and wrap a servlet, and there’s some buzz about a web library for Clojure called compojure. [...]
January 10, 2009 at 4:14 pm
[...] fashion that looks odd but probably saves the experienced user lots of keystrokes. Eric Levigne has written some posts about using Compojure to run a [...]
January 24, 2009 at 8:10 pm
hey, thanks for the post, eric. just trying out compojure myself. all your steps seem to work great, until I try to execute the simple web app:
root@arjun:~/site# ./run
Caused by: java.io.FileNotFoundException: Could not locate compojure/http__init.class or compojure/http.clj on classpath:
any thoughts/ideas? sorry for being such a n00b
January 24, 2009 at 8:59 pm
Arjun, this is definitely NOT your fault. There have been some recent changes to Compojure, and I haven’t yet found time to update these articles.
At the beginning of the program, there are two lines of code for importing libraries:
(use ‘compojure.http)
(use ‘compojure.jetty)
Try changing them as follows:
(use ‘compojure.http.servlet)
(use ‘compojure.http.routes)
(use ‘compojure.server.jetty)
The other articles have similar issues. I hope to fix those issues in the next few days. Until then, please feel free to ask for help with any problems you encounter.
January 25, 2009 at 4:09 pm
I just finished updating the code in this article for the latest version of Compojure.
If you are still using an old version of Compojure, use the following commands to update to the latest version:
cd /root/install/compojure
git pull
ant
The other two articles also need to be updated. I will place a comment like this one at the bottom of each article after I finish updating them. I hope to finish by the end of the day.
I am preparing to deliver a Clojure presentation to a Java user group in mid-February. I will definitely post my slides later.
January 25, 2009 at 5:09 pm
awesome, i’m up & running now. thanks, eric.
January 27, 2009 at 12:22 am
Thanks Eric for sharing this information.
I’d like to mention there’s more slice set up and lock down tips in the slice setup article for Ubuntu Hardy: http://articles.slicehost.com/2008/4/25/ubuntu-hardy-setup-page-1
It’s important to disallow root login via ssh, and there are a few more good tips in there.
Welcome to Slicehost.
February 15, 2009 at 8:01 am
Thanks, this was really helpful as I set up my blog in Clojure.
April 10, 2009 at 1:17 pm
Thanks for the good post – you might want to add ant deps before ant (I got
“BUILD FAILED
/home/compojure/install/compojure/build.xml:34: /home/compojure/install/compojure/deps not found.”
until I went to http://en.wikibooks.org/wiki/Compojure/Getting_Started and saw that addition)
May 12, 2009 at 5:08 am
You can abbreviate the classpath with “java -Djava.ext.dirs=/root/install/compojure/deps” — there is no need to list every jar individually on the classpath.
September 3, 2009 at 2:57 am
[...] off github, or that I hadn’t included all jars needed. I found a tip on one of the comments here for specifying a folder where all jars are included which takes the pain out of adding each jar [...]