The voice of BeeBole

Web Application on Erlang: Configure Nginx with Mochiweb

September 30, 2008 by Hughes (Erlang)

My previous post, the first of our series dedicated to the Erlang world, was definitively getting too long, so here is the second chapter of this marvelous article.

I’ll assume that you have successfully installed Erlang and Mochiweb and deployed your first small tiny app.

When it comes to deliver static content at lightning speed and do reverse proxy job, there is one name : Nginx, the Igor Sysoev high-performance HTTP server.

It plays well with ssl and in the last dev version you can even find an interesting module for uploading files, nothing less … we will probably tell you more about that in the series.

Nginx is really turning itself into a real handy swiss knife.

Step 0 - SSL packages check

So let’s start by ensuring that the ssl packages are already installed on your box :

foo@bar:~$ sudo apt-get install openssl libssl-dev

Step 1 - Install Nginx

Now, we are ready to download, compile and install Nginx.

Here, we want to install the last stable version (0.6.32).

foo@bar:~$ sudo apt-get build-dep nginx
foo@bar:~$ wget http://sysoev.ru/nginx/nginx-0.6.32.tar.gz
foo@bar:~$ tar -zxvf nginx-0.6.32.tar.gz
foo@bar:~$ cd nginx-0.6.32
foo@bar:~/nginx-0.6.32$ ./configure –with-http_ssl_module
foo@bar:~/nginx-0.6.32$ make && sudo make install

Done.

Step 2 - Nginx configuration

We do have now to tell Nginx what to proxy, in our case http://my_server_name:8000 (in the previous post, remember?)

You can find the Nginx configuration file, nginx.conf, under /usr/local/nginx/conf/.

Here is a working example one :

Discover the code in Friendpaste

user www-data;

worker_processes  4;
...

http {
    include       mime.types;
    ...

    upstream mochiweb {
        server my_server_name:8000;
    }

    server {
        listen       80;
        server_name  my_server_name;

        ...

        location / {
            root   path_to_myapp/myapp/priv/www;
            index  index.html index.htm;
            ...

            if (!-f $request_filename) {
                proxy_pass http://mochiweb;
                break;
            }

        }

    }

}

Step 3 - Start, Stop Nginx

Finally, we are going to tell Ubuntu how to start and stop the server.

Just create a new file named ‘nginx’ in /etc/init.d and paste the content of this link:

Discover the code in Friendpaste

foo@bar:~/nginx-0.6.32$ cd /etc/init.d
foo@bar:~/etc/init.d$ sudo vi nginx

Above, for those of you who are not in love with vi, you can use nano instead: sudo nano nginx

foo@bar:~/etc/init.d$ sudo chmod +x nginx
foo@bar:~/etc/init.d$ sudo update-rc.d nginx defaults
foo@bar:~/etc/init.d$ sudo ./nginx start

Cool, you have now a working reverse proxy.

foo@bar:~/etc/init.d$ cd ~/myapp
foo@bar:~/myapp$ ./start-dev.sh

Browse this link http://my_server_name

Peace!

As a reminder, the next points we will discuss are:

  • 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

So, stay tuned

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

September 25, 2008 by Hughes (Erlang)

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

(Read the rest of the article…)

Major Release for our JavaScript Template Engine – PURE

September 18, 2008 by Yves (JavaScript, PURE)

Thanks to your comments and the requests made on our discussion group we have been able to bring PURE to its next step.

This release contains a few corrections but also a lot of new features:

  • Auto-rendering: a new PURE method takes your HTML and your JSON data and merges them automatically. The class attribute is used to map the HTML and the data. (Read more about autoRender and its jQuery version on our Wiki).
  • Functions as directive are called by reference and not serialized
  • Change the id of the template root node (as any other attribute)
  • Better string value handling
  • Use of named properties in iteration “obj.prop” as well as “obj['name']“

We also made some changes to the PURE environment:

  • The source code moved from Google Code to GitHub

We are also actively looking for help in order to port PURE to other JS libraries such as DomAssistant, Dojo, Mootools, Prototype, YUI, …

If you are ready to get your hands dirty, just pass by the discussion group and leave us a message.

Stay tuned

PURE is running on the iPhone

September 11, 2008 by Mic (PURE)

As you may know, Belgium has some oddities.

One of them is a poor phone law that makes consumers pay the full price for a phone, probably one of the highest in the world. This doesn’t bring any cheaper usage cost than our neighbours either.

So it came as a kind of consolation when I saw PURE running smoothly on my brand new iPhone 3G.

PURE on iPhone

Ok, it’s an easy shot as Safari is running there. But that’s good news for web applications using PURE.

Even if we’ll have to build specific screens for the smaller devices, we’ll be able to use the same technology, back-end services and share some templates between the desktop and the smart phone versions.

Moreover, the rendering speed was really good. We will show some benchmarks soon.

I can’t wait on what Google/HTC will ship in a couple of months.

Web Application on Erlang: So Far, So Good, …

September 2, 2008 by Hughes (Erlang)

As a quick recap, I’m building a lightweight server side custom framework which focuses mainly on handling JSON service requests.  This, combined with Mnesia, is our MC stack (Model - Controller).  The client browser will be fully in charge of the ‘V’iew layer of the app.

(See our post Why Erlang ? for more information).

A stable version of the core has been finished a week ago.  In brief, a request hits the server, Nginx proxies it to Mochiweb which starts a new process. 

At this point the framework converts the posted data (JSON) to a native Erlang structure. We check if the request contains one or more services and, based on their name (=:= module), we spawn processes accordingly.

As you may have noticed, in order to save some http requests and to use the full power of our server, the client can ask for an array of services.

i.e. : Let’s imagine you are building a screen which is divided into multiple small independent functional parts : your preferences, your status, your personal info, whatever …, you have the choice to wrap all those service calls into one JSON and the framework will happily process those in parallel … fine isn’t?

At this stage of development, and provided that the DB is not fully populated, if I make a comparison with other developments I’ve done using other languages/architectures, my global feeling is : SPEED.

Whenever I find myself in the situation of using a ‘map’ function and depending on the computation weight on each element, I’m parallelizing the operation (pmap).

Coupled with PURE for the rendering at the client side, we aim to end up with a highly responsive application.

The job now is to continue on that track and make the whole thing even more flexible.

One of my concern when choosing Erlang was the lack of libraries compared to other languages such as Ruby. However, I found relatively easy to develop, from scratch, a mail module responsible to dynamically build messages to be send to one or more addresses (via Postfix) and a ‘captcha’ system.

Soon, I will say a little bit more about those two points and will also describe how to quickly set up an Ubuntu server running Erlang and friends.

Stay tuned

© 2008 BeeBole | powered by WordPress with Barecity