Setting up Nginx + perl-fcgi
Everyone praises Nginx for its performance. But nobody accounts for the fact that it’s so fast because it’s missing all those modules that extend Apache’s functionality. You can’t install a module for handling perl or python files in Nginx through a regular package manager.
Nginx is really more like a universal front-end.
In this post I want to go over an example of how to set up Nginx to work with perl scripts.
All my notes are based on Linux Ubuntu. If you’re using CentOS, RedHat or OpenSuse, you won’t run into any fundamental differences, aside from package names and config file locations. If you run into trouble, reach out through the contact form.
So first you need to install the FCGI libraries for Perl.
apt-get install libfcgi-perl
Next download the wrapper and the script to run it:
wget http://nginxlibrary.com/downloads/perl-fcgi/fastcgi-wrapper -O /usr/bin/fastcgi-wrapper.pl
wget http://nginxlibrary.com/downloads/perl-fcgi/perl-fcgi -O /etc/init.d/perl-fcgi
If the links don’t work you can use these instead:
wget http://tech-notes.tk/wp-content/uploads/2014/02/fastcgi-wrapper -O /usr/bin/fastcgi-wrapper.pl
wget http://tech-notes.tk/wp-content/uploads/2014/02/perl-fcgi -O /etc/init.d/perl-fcgi
Set the correct permissions on the files (make them executable):
chmod +x /usr/bin/fastcgi-wrapper.pl
chmod +x /etc/init.d/perl-fcgi
The Init.d script needs a bit of editing:
- change su - to sudo -u
- remove -c from the run line
To do that run the following command in bash:
sed -i -e 's/su\ -/sudo\ -u/g' -e '/sudo/s/-c\ //g' /etc/init.d/perl-fcgi
Add it to startup and launch the perl server:
update-rc.d perl-fcgi defaults
/etc/init.d/perl-fcgi start
By default the perl-fcgi server starts on port 8999. It’s worth checking though:
netstat -anp |grep -i perl
Next you need to teach Nginx to proxy requests to perl. Add these lines to the Nginx virtual host config file.
location ~ \.pl$ {
try_files $uri =404;
gzip off;
fastcgi_pass 127.0.0.1:8999;
fastcgi_index index.pl;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Now our Nginx knows how to work with perl.
You can check the nginx config and restart it so the changes take effect:
nginx -t
/etc/init.d/nginx restart
To test it you can create an info.pl file in the site’s root directory and open that script in the browser.
#!/usr/bin/perl
# test.cgi by Bill Weinman [http://bw.org/]
# Copyright 1995-2008 The BearHeart Group, LLC
# Free Software: Use and distribution under the same terms as perl.
use strict;
use warnings;
use CGI;
print foreach (
"Content-Type: text/plain\n\n",
"BW Test version 5.0\n",
"Copyright 1995-2008 The BearHeart Group, LLC\n\n",
"Versions:\n=================\n",
"perl: $]\n",
"CGI: $CGI::VERSION\n"
);
my $q = CGI::Vars();
print "\nCGI Values:\n=================\n";
foreach my $k ( sort keys %$q ) {
print "$k [$q->{$k}]\n";
}
print "\nEnvironment Variables:\n=================\n";
foreach my $k ( sort keys %ENV ) {
print "$k [$ENV{$k}]\n";
}