Libraries and samples to deploy an OCaml application to an Nginx server Libraries and samples to deploy an OCaml application to an Nginx server nginx nginx

Libraries and samples to deploy an OCaml application to an Nginx server


I don't think the solution I will propose is the less heavy, but it has several advantages :

  • It allows you to generate the website in Ocaml, so that the interface with your code won't be to hard to do
  • If needed you will be able to export your whole application directly in Javascript : you won't let your serveur do useless computes that the user could do, and moreover you don't need to rewrite your code in Javascript, Ocsigen can convert your code for you
  • If some operations need to be performed by the serveur, you can really easily call server side functions from the client side code, and all your code will be written in Ocaml.
  • It's pretty easy

What is this amazing tool ? Ocsigen ! You can find a complete tutorial here.

Now let's see how you can use it

Install Ocsigen

First if you don't have it, install opam (it will allow you to install ocaml packages). Just follow the instructions on the website (I cannot paste link since I don't have enough reputation points), but basically for ubuntu run :

sudo add-apt-repository ppa:avsm/ppasudo apt-get updatesudo apt-get install ocaml ocaml-native-compilers camlp4-extra m4 opam

Then, you need to install Ocsigen. All instructions are here : https://ocsigen.org/install but basically just do :

sudo aptitude install libev-dev libgdbm-dev libncurses5-dev libpcre3-dev libssl-dev libsqlite3-dev libcairo-ocaml-dev m4 opam camlp4-extraopam install eliom

(Note : you can also install it with apt-get if you don't want to install/use opam, but I prefer using opam to deal with ocaml depends, you can choose a precise version...)

Well it's done, you now have installed ocsigen !

Create the web page

Then to create a basic scaffold site just run

eliom-distillery -name mysite -template basic -target-directory mysite

and to run it :

cd mysite/make test.byte

You should see a basic page at localhost:8080/.

Now, let's insert your code. Let's imagine it is named myscript and return a string :

let myscript () = "Here is my amazing result"

Add this code before the let () = in the file mysite.eliom, and add just after h2 [pcdata "Welcome from Eliom's distillery!"]; the line :

p [pcdata (Printf.sprintf "My script gives the return function : \"%s\"" (myscript ()))]

This will create a paragraphe (p) whose content (pcdata) contains the result of myscritpt.

For me the whole mysite.eliom gives :

{shared{  open Eliom_lib  open Eliom_content  open Html5.D}}module Mysite_app =  Eliom_registration.App (    struct      let application_name = "mysite"    end)let main_service =  Eliom_service.App.service ~path:[] ~get_params:Eliom_parameter.unit ()let myscript () = "Here is my amazing result"let () =  Mysite_app.register    ~service:main_service    (fun () () ->      Lwt.return        (Eliom_tools.F.html           ~title:"mysite"           ~css:[["css";"mysite.css"]]           Html5.F.(body [               h2 [pcdata "Welcome from Eliom's distillery!"];               p [pcdata (Printf.sprintf "My script gives the return function : \"%s\"" (myscript ()))]           ])))

(Please note that let application_name = "mysite" must follow the name that you gave to eliom-distillery. If it's not the case your javascript won't be linked)

Let's compile again :

make test.byte

Now at localhost:8080 you can read :

My script gives the return function : "Here is my amazing result"

The result of the script has been included !

Going further

You can also define myscript to be run in client side, take some Post/Get param, or communicate with the page in real time in only a few lines, but if you want to learn more about that just read the ocsigen tutorial !

Interface with Nginx

I'm not sure you really need to interface it with Nginx, since ocsigenserver is supposed to run as an (http) serveur, but if needed you can always put ocsigenserver behing a Nginx serveur by using reverse-proxy (or the reverse thing, you can serve Nginx from ocsigenserver, read the ocsigenserver manual for more details).