Web Application on Erlang: Configure Nginx with Mochiweb

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

2 thoughts on “Web Application on Erlang: Configure Nginx with Mochiweb

  1. Pingback: How To Quickly Set Up Ubuntu 8.04 loaded with Erlang, Mochiweb and Nginx | BeeBuzz