Linode Library Home
Linode Library RSS Feed
Home :: Web Servers :: Nginx :: Perl-FastCGI
Print View View Source

Nginx and Perl-FastCGI on Fedora 13

Published: by

Warning

This document is an older, unmaintained guide. There may be a new guide available for this software.

The nginx web server is a fast, lightweight server designed to efficiently handle the needs of both low and high traffic websites. Although commonly used to serve static content, it's quite capable of handling dynamic pages as well. This guide will help you get nginx up and running with Perl and FastCGI on your Fedora 13 Linux VPS.

It is assumed that you've already followed the steps outlined in our getting started guide. These steps should be performed via a root login to your Linode VPS over SSH.

Contents

Basic System Configuration

Issue the following commands to set your system hostname, substituting a unique value for "plato."

echo "HOSTNAME=plato" >> /etc/sysconfig/network
hostname "plato"

Edit your /etc/hosts file to resemble the following, substituting your Linode's public IP address for 12.34.56.78, your hostname for "plato," and your primary domain name for "example.com." :

File:/etc/hosts

127.0.0.1   localhost.localdomain   localhost
12.34.56.78 plato.example.com   plato

Install Required Packages

Issue the following commands to update your system and install the nginx web server and compiler tools (Perl should already be installed):

yum update
yum install nginx make automake gcc gcc-c++ wget fcgi-perl
chkconfig --add nginx
chkconfig --level 35 nginx on
service nginx start

Configure Your Site

In this guide, we'll be using the domain "example.com" as our example site. You should substitute your own domain name in the configuration steps that follow. First, we'll need to create directories to hold our content and log files:

mkdir -p /srv/www/www.example.com/public_html
mkdir /srv/www/www.example.com/logs
chown -R nginx:nginx /srv/www/www.example.com

Issue the following commands to create virtual hosting directories:

mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled

Add the following lines to your /etc/nginx/nginx.conf file, immediately after the line for "include /etc/nginx/conf.d/*.conf":

File excerpt:/etc/nginx/nginx.conf

# Load virtual host configuration files.
include /etc/nginx/sites-enabled/*;

Next, we'll need to define our site's virtual host file:

File:/etc/nginx/sites-available/www.example.com

server {
    listen   80;
    server_name www.example.com example.com;
    access_log /srv/www/www.example.com/logs/access.log;
    error_log /srv/www/www.example.com/logs/error.log;
    root /srv/www/www.example.com/public_html;

    location / {
        index  index.html index.htm;
    }

    location ~ \.pl$ {
        gzip off;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:8999;
        fastcgi_index index.pl;
        fastcgi_param  SCRIPT_FILENAME  /srv/www/www.example.com/public_html$fastcgi_script_name;
    }
}

Issue the following commands to enable the site:

cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/www.example.com
service nginx restart

You may wish to create a test HTML page under /srv/www/www.example.com/public_html/ and view it in your browser to verify that nginx is properly serving your site (Perl will not work yet). Please note that this will require an entry in DNS pointing your domain name to your Linode's IP address (found on the "Remote Access" tab in the Linode Manager).

Configure FastCGI Wrapper

Issue the following command sequence to download the FastCGI wrapper script (credit: Denis S. Filimonov) and an init script to control the FastCGI process, set the permissions, launch the wrapper for the first time, and ensure that FastCGI launches at startup:

cd /opt/
wget -O fastcgi-wrapper http://library.linode.com/assets/640-fastcgi-wrapper.sh
wget -O init-rpm.sh http://library.linode.com/assets/639-init-rpm.sh
mv /opt/fastcgi-wrapper /usr/bin/fastcgi-wrapper.pl
mv /opt/init-rpm.sh /etc/rc.d/init.d/perl-fastcgi
chmod +x /usr/bin/fastcgi-wrapper.pl
chmod +x /etc/rc.d/init.d/perl-fastcgi
/etc/rc.d/init.d/perl-fastcgi start
chkconfig --add perl-fastcgi
chkconfig perl-fastcgi on

Test Perl with FastCGI

Create a file called "test.pl" in your site's "public_html" directory with the following contents:

File:/srv/www/www.example.com/public_html/test.pl

#!/usr/bin/perl

print "Content-type:text/html\n\n";
print <<EndOfHTML;
<html><head><title>Perl Environment Variables</title></head>
<body>
<h1>Perl Environment Variables</h1>
EndOfHTML

foreach $key (sort(keys %ENV)) {
    print "$key = $ENV{$key}<br>\n";
}

print "</body></html>";

Make the script executable by issuing the following command:

chmod a+x /srv/www/www.example.com/public_html/test.pl

When you visit http://www.example.com/test.pl in your browser, your Perl environment variables should be shown. Congratulations, you've configured the nginx web server to use Perl with FastCGI for dynamic content!

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

Creative Commons License

This guide is licensed under a Creative Commons Attribution-NoDerivs 3.0 United States License.

Last edited by Phil Paradis on Tuesday, May 17th, 2011 (r1942).