Perl was the language of choice in the early days of web programming, when Perl was a synonym for CGI. Many things are changed in the meanwhile, but we can still use Perl with a modern webserver to get things done!
Everybody should know that Perl is not the same of the early days and things has changed a lot. If you want to write a web application with Perl you should use one of the famous MVC frameworks available: Dancer, Mojolicious and Catalyst.
On the other hand if you want to write modern FastCGI applications you should use Plack because it will make your code adaptable and portable with a small effort.
But in this article I will talk about a much simpler solution based on Perl-FPM, a simple script I wrote and that addresses the problem from a different point of view. It does not create a whole FastCGI application, but it is a kind of specific FastCGI wrapper that will run your scripts using the Perl do-file mechanism to load your scripts. It’s an approach that resembles to how things are usually done with PHP and that is very straightforward for small project or for projects where frequent updated are needed by simply copying the script files, instead of restarting your fcgi app.
I also decided to write this script to have a simple way to run Perl with nginx.
Get Perl-FPM running in seconds
If you want to give a try to Perl-FPM you can clone its Docker example application. Things are very simple, but if are not used to Docker you can get started with my introduction to Docker.
To get things running just give a “docker-compose up”. Two services will be created: a nginx webserver listening on port 8080 and a Perl-FPM container.
You can find the nginx server configuration inside the “nginx” folder. It is a very basic configurations that and just binds all .pl extensions to the fcgi handler.
So just open your browser and go to localhost:8080. There should be a dump of the environment variables and a little form to try some data posting to the script.
The Perl scripts receive an instance of CGI to handle input parameters, and useful to set output headers. As usual the output payload is build with simple prints.
How to go further
The README.md included in Perl-FPM will give you some advice for using the script in your context. The most important option is probably the “processes” options that allows you to define how many workers you need. As a rule of thumb you can take the available CPU core number and double it, but it depends on you working context.
The other useful option is “startup”. You can set a path to a script that will be run before the fcgi loop starts. So you can do same pre-start work, but more probably you will load some common Perl modules that will be shared between all worker processes, receiving a little performance boost.
If you still using Docker you can pass this options by adding a command option to the perl-fpm service like this:
command: /root/perl-fpm.pl --processes 6 --startup /root/startup.pl
In this case you have to put someway the startup script into the container, using volumes, or by editing the Dockerfile where you can also add needed Perl related packages to Alpine.
I hope this script will be useful for someone. In case of need just write something here in the comments.
2 thoughts on “Perl-FPM, running Perl on nginx as FastCGI”
so if i have quad core i should set workers as 8?
Yes, it is usually the best setting.