How To Quickly Set Up Ubuntu 8.04 loaded with Erlang, Mochiweb and Nginx

Let’s say you want to give a try to Erlang (Discover our post about Why Erlang?) for your next web development project and you want to be up and running as quickly as possible… you just landed smoothly in the right place.

This post is the starting point of a series of posts in which I’m going to provide you with all the commands you’ll need to set up an Ubuntu 8.04 server loaded with Erlang, Mochiweb proxied by Nginx.

In the same series, I’ll also cover:

  • The basic configuration of Postfix (mail)
  • The use of Imagemagick to create dynamically a captcha for your application
  • The configuration of Bind9 in order to play with the url CNAME

The goal here is not to set up an hardened production server with all the optimizations and security niceties in head (I definitively want to avoid a ‘Why not Gentoo, or Slackware, or Debian, or FreeBSD, …?’, I love them all).


No, it’s all about being able to quickly set up a test server and/or your dev machine in order to dive directly in the development typhoon. And, I must say, Ubuntu(*) is a king at this task.

(*) from a dev point of view, having your dev machine installed with the same OS that your test server is running is definitively a big plus.

You’ve just rent a cheap box from your web host provider, done a fresh Ubuntu 8.04 server installation and you are currently ssh logged in as root … yes, yes, you have done all those things. (In the case you are setting up your desktop machine you can go directly to point 3)

So let’s start!

Step 0 – Upgrade

We ensure with this step that our package sources and our installation is up to date.

root@bar:~$ apt-get update
root@bar:~$ apt-get clean
root@bar:~$ apt-get upgrade

Step 1 – Denyhosts

We install Denyhosts, a Python script used to prevent brute force hacking of our SSH server.

Also, we set RESET_ON_SUCCESS = yes in order to avoid being blocked because we reached the max attempt value.

root@bar:~$ apt-get install denyhosts
root@bar:~$ vi /etc/denyhosts.conf

/RESET_ON_SUCCESS [enter]
i

RESET_ON_SUCCESS = yes

[esc]
:wq

Step 2 – A new sudoer + reboot

We add a new user and grant him the ability to use the sudo command.

A first and unique reboot (we don’t need to do that … but I’m used to reboot my system at least once in order to check that everything is properly set up after a clean install).

root@bar:~$ adduser foo

root@bar:~$ apt-get install sudo
root@bar:~$ visudo

foo ALL=(ALL) ALL

root@bar:~$ reboot

We log in with the user we have just created:

ssh foo@bar

Step 3 – Some useful packages

Installation of the GNU C and C++ compiler, the ‘make’ utility, svn and Git.

foo@bar:~$ sudo apt-get install build-essential subversion git-core

Step 4 – Erlang

Building the last Erlang release from source.

foo@bar:~$ sudo apt-get build-dep erlang
foo@bar:~$ sudo apt-get install java-gcj-compat java-gcj-compat-dev

foo@bar:~$ wget http://www.erlang.org/download/otp_src_R12B-4.tar.gz

foo@bar:~$ tar zxvf otp_src_R12B-4.tar.gz
foo@bar:~$ cd otp_src_R12B-4
foo@bar:~/otp_src_R12B-4$ ./configure
foo@bar:~/otp_src_R12B-4$ make && sudo make install

foo@bar:~/otp_src_R12B-4$ erl

Eshell V5.6.4 (abort with ^G)
1>
init:stop().

Step 5 – Mochiweb

UPDATE, Octobre 20th 2008: Read and view more about installing Mochiweb and creating a first Erlang Web application on our video

Here, we install Mochiweb and we create our first app skeleton … Tataaaan

foo@bar:~$ svn checkout http://mochiweb.googlecode.com/svn/trunk/ mochiweb

foo@bar:~$ cd mochiweb
foo@bar:~/mochiweb$ make
foo@bar:~/mochiweb$ chmod +x scripts/new_mochiweb.erl
foo@bar:~/mochiweb$ ./scripts/new_mochiweb.erl myapp ../.
foo@bar:~/mochiweb$ cd ../myapp
foo@bar:~/myapp$ make
foo@bar:~/myapp$ ./start-dev.sh

Launch your favorite browser and check this address : http://bar:8000, where bar is the ip adress or the name of your server/desktop.

You should see:

MochiWeb running.

Well Done, you have an ultra fast ligthweight webapp server up and running !

Step 6 – Some getting started hints

Where do I start coding ?

foo@bar:~/myapp/src$ vi myapp_web.erl

In the main loop you will find a case testing the GET and the POST method.

Just add the ‘congrat’ part.

loop(Req, DocRoot) ->
 "/" ++ Path = Req:get(path),
 case Req:get(method) of
  Method when Method =:= 'GET'; Method =:= 'HEAD' ->
   case Path of

    "congrat" ->
     Req:ok({"text/html", [],["<h1>Congratulation</h1>"]});

    _ ->
     Req:serve_file(Path, DocRoot)
    end;
  'POST' ->
   case Path of
    _ ->
     Req:not_found()
   end;
  _ ->
    Req:respond({501, [], []})
 end.

foo@bar:~/myapp/src$ cd ..
foo@bar:~/myapp$ make
foo@bar:~/myapp$ ./start-dev.sh

Launch the browser you want to live with and travel to this url: http://bar:8000/congrat, where bar is the ip adress or the name of your server/desktop.

In this module, I’m used to limit the code to the functions related to Req, the mochiweb_request module;

Here are a few more:

Req:get_cookie_value(“myCookie”)
Req:parse_qs() (parse the query string)
Req:parse_post() (parse the posted data)

How do I start Mnesia with my app?

foo@bar:~/myapp/src$ vi myapp.erl

%% @spec start() -> ok
%% @doc Start the myapp server.
start() ->
 myapp_deps:ensure(),

 ensure_started(crypto),
 ensure_started(mnesia),
 application:start(myapp).

%% @spec stop() -> ok
%% @doc Stop the myapp server.
stop() ->
 Res = application:stop(myapp),
 application:stop(mnesia),
 application:stop(crypto),
 Res.

Edit your start script in order to specify the db location

foo@bar:~/myapp$ vi start-dev.sh

exec erl ... -mnesia dir '"/path_to_your_db/myapp_dev"' ...

How do I start my app silently in background ?

Launch Erlang with the -detached flag (start erl without a controlling terminal)

foo@bar:~/myapp$ vi start-dev.sh

exec erl ... $1

You can now pass the -detached argument to your start script like this:

foo@bar:~/myapp$ ./start-dev.sh -detached

Mochiweb is coming with a handy set of modules

Have a look at the source of mochiweb_request, mochiweb_cookies, mochinum, mochijson2, mochihex, …

In the next post, I’ll finish this basic install with the configuration of Nginx and I’ll provide you with a small JSON app example.

NOTE : The post has been updated on the 08/10/2008 to match the new Erlang release number

Comments are closed.