CentOS Clojure
CentOS Clojure installation and sample tutorial.
In the following post I’ll show how to install and configure Clojure on you CentOS box with sample web server example.
Also,, weI’ll use leiningen for manage the Clojure app and http-kit for deployment.
CentOS Clojure Installation
First, Clojure requires only Java 1.5 or greater, if you don’t have the JDK installed yet on your CentOS box please follow this instructions by ItekBlog writer – Adam to install Oracle Java on your CentOS box.
Download Clojure
Before you continue, check the Downloads page to install the latest version.
To download Clojure use:
# if you don't have wget installed - use: yum install wget # if you don't have unzip installed - use: yum install unzip cd /opt wget http://repo1.maven.org/maven2/org/clojure/clojure/1.5.1/clojure-1.5.1.zip unzip clojure-1.5.1.zip rm clojure-1.5.1.zip
to bring up a simple read-eval-print loop (REPL) use:
cd clojure-1.5.1 java -cp clojure-1.5.1.jar clojure.main
try:
user=> (+ 1 2 3) 6 user=> (javax.swing.JOptionPane/showMessageDialog nil "Hello World")
Try Clojure Online
- TryClojure provides a brower-based Clojure REPL
- Himera provides a browser-baed ClojureScript REPL
Community Resources
Community volunteers maintain Getting Started documentation for a number of different tools and approaches.
Leiningen
Leiningen is the easiest way to use Clojure. With a focus on project automation and declarative configuration, it gets out of your way and lets you focus on your code.
Installing Lieningen
to install Lieningen you need to download the lein script place it on your $PATH (eg. ~/bin) and set it to be executable:
cd ~/bin wget https://raw.github.com/technomancy/leiningen/stable/bin/lein chmod a+x ~/bin/lein
you can read the tutorial by running lein help tutorial.
The tutorial is the best place to start. It does not cover learning the language itself; good Clojure documentation can be found.
Running lein help faq will get you the FAQ. Documentation for each individual task is available via lein help $TASK. You can also see the sample project.clj file containing a reference of most project settings by running lein help sample.
Creating a Project
Creating a new project is easy:
cd ~/ lein new app my_first_app cd my_first_app find .
In this example we’re using the
1 | app |
template, which is intended for an application project rather than a library. Omitting the app argument will use the default template, which is suitable for libraries.
Directory Layout
The output of the find command is:
Here you’ve got your project’s README.md file, .gitignore file for you all GIT users, a src/ directory containing the code, a test/ directory, and a project.clj file which describes your project to Leiningen. The src/my_stuff/core.clj file corresponds to the my-stuff.core namespace.
Running Code
let’s start our code running:
lein run
For extended information and documentation about Leiningen and the REPL Read here.
FAQ: Set LEIN_ROOT to disable this warning.
If you want to run LEIN using root, you should add to your ~/.bash_profile :
export LEIN_ROOT="Something"
rem
ember to touch the file:
. ~/.bash_profile
Frameworks
Luminus
Luminus is a micro-framework based on a set of lightweight libraries. It aims to provide a robust, scalable, and easy to use platform. With Luminus you can focus on developing your app the way you want without any distractions.
create a new project:
lein new luminus myapp cd myapp lein ring server
the app is now available at
1 | localhost:3000 |
Clojure Deployment
When you’re ready to deploy your app, you have many web servers such as: nginx, http-kit, immutant, tomcat, JBoss, Pallet, GlassFish, Jetty, Netty, Grizzly, etc.
using nginx
You can use nginx-clojure to run your clojure project inside of nginx server.
In Clojure web server benchmarks, this server achieved the highest performance.
read this tutorial for more information about nginx/clojure implementation.
using Tomcat
To install Tomcat server follow those steps.
You need to package the application as a WAR archive, to do that run:
lein ring uberwar
then simply copy the resulting myapp-0.1.0-SNAPSHOT-standalone.war to the webapps folder on Tomcat, eg:
cp target/myapp-0.1.0-SNAPSHOT-standalone.war ~/tomcat/webapps/myapp.war
Your app will now be avaliable at the context /myapp when Tomcat starts. To deploy the app at root context, simply copy it to webapp as ROOT.war.
HTTP Kit – HTTP client/server for Clojure
HTTP Kit is a minimalist, efficient, Ring-compatible HTTP client/server for Clojure. It uses a event-driven architecture to support highly concurrent a/synchronous web applications. Feature a unified API for WebSocket and HTTP long polling/streaming
<div class="codecolorer-container text railscasts" style="overflow:auto;white-space:nowrap;width:555px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br /></div></td><td><div class="text codecolorer"> [http-kit "2.1.16"] ; Add to your project.clj.</div></td></tr></tbody></table></div>
Check this great example of a websockets app with Clojure and http-kit.
That’s It!
Cheers!,