Articles

Call FastCGI applications from Erlang


What is FastCGI

FastCGI is like CGI with a (not) minor difference: it allows you to avoid fork()ing new processes for each request, and you can also put all your (Fast)CGI related computation behind a load balancer and away from affecting the computing power of your web servers (i.e: decouple!).

Also, it can be useful to call FastCGI applications directly from your Erlang code, so in this article we're going to see how to use erl_fastcgi to do such thing in a quick and easy way!

What is erl_fastcgi

erl_fastcgi is an Erlang/OTP library packaged as a rebar3 project and you can find it at the GitHub repo at: https://github.com/marcelog/erl_fastcgi

Installing erl_fastcgi

To use it in your Rebar application just add this to your rebar.config file:

Using erl_fastcgi

Let's see a quick example:

In this example we're calling php-fpm to process a simple request. Php-fpm will need some of the FastCGI parameters that you see in the example in order to find and run the right PHP script (some of these were taken from this nginx example for php-fpm).

After calling erl_fastcgi:run/4 your process will start receiving messages like:

FastCGI + Erlang + erl_fastcgi: A real winner!

Now you are able to easily call a lot of different applications in different languages directly from your Erlang code, for example to extend your cowboy application. How cool is that?