Lighttpd Setup CGI-BIG CGI For Perl Programs

Lighttpd logo

Lighttpd has mod_cgi module that allows you running Perl and other server side programs via cgi-bin directory. The Common Gateway Interface (CGI) is a standard protocol for interfacing external application software with an information server.

Step #1: Create a CGI cgi-bin Directory

First you need to create a cgi-bin directory for your domain. Assuming that your domain hosted at /home/lighttpd/theos.in/http (DocumentRoot), create cgi-bin as follows:
# mkdir -p /home/lighttpd/theos.in/cgi-bin

Step # 2: Load mod_cgi Module

Open lighttpd configuration file using a text editor such as vi:
# vi /etc/lighttpd/ligttpd.conf

Now append or modify text as follows so that support for mod_cgi get loaded:

server.modules += ( "mod_cgi" )

Find out your virtual server configuration and append the following:

$HTTP["url"] =~ "/cgi-bin/" {
      cgi.assign = ( ".pl" => "/usr/bin/perl" )
}

Here is complete my config code:

$HTTP["host"]  =~ "theos.in" {
  server.document-root = "/home/lighttpd/theos.in/http"
  accesslog.filename         = "/var/log/lighttpd/theos.in/access.log"
  $HTTP["url"] =~ "/cgi-bin/" {
      cgi.assign = ( ".pl" => "/usr/bin/perl" )
  }
}

Step # 3:Restart the Lighttpd

Restart lighttpd webserver, enter:
# /etc/init.d/lighttpd restart

Step # 4: Test It

Create a file/perl program in /home/lighttpd/theos.in/cgi-bin/sample.pl:

#!/usr/bin/perl
print "Content-Type: text/plain", "\n\n";
print "Hi there! This is a sample perl program!!!", "\n";

Save and execute the program (http://yourdomain.com/cgi-bin/sample.pl).

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 10 comments… read them below or add one }

1 james 08.25.08 at 2:04 am

thanks mate, works sweet as

2 Leszek 09.25.08 at 10:43 pm

Can I enable CGI globally by entering:
lighty-enable-mod cgi
and restarting Lighttpd ?

3 Thomas Martin Klein 02.08.09 at 4:02 pm

Thanks for the info… it works like a charm,

4 Amir 03.16.09 at 3:34 pm

A life saver!
Thank you.

5 Dmitry 03.26.09 at 9:30 am

Thank you!

Trying this sample I got 403 Forbidden from lighttpd.

Would you please, explain why?

Thanks again.

6 Vivek Gite 03.26.09 at 1:20 pm

Check for directory permission and set +x permission on your script:
chmod +x script.pl

Also, look at your error log file.

7 Dmitry 03.26.09 at 3:48 pm

Thanks Vivek,

The problem has been solved by reading POST data from stdin by cgi script.

Thanks.

8 nobody 06.10.09 at 7:19 am

why using the so fast lighttpd when you are using the so slow perl… in 2009, it’s really pathetic still using perl…

9 Twitter Friendly 06.11.09 at 8:03 pm

thank you for the post. nginx has a limited embed implementation as well, alternatively you can wrap cgi for it and run Perl inside.
@nobody for many reasons too numerous to write down here.

10 Hawk 06.12.09 at 4:09 am

@nobody and @twit Friendly The “so slow perl” is probably due to your inability to program it properly. The only thing faster then perl is mod_perl! Why don’t you use PHP. It’s a dumbed down version of perl and might be more to your speed.

Can anyone explain why it gives a 500 Internal Server Error?

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , ,

Previous post: Shells scripting – How to create GUI dialogs for GNOME and KDE?

Next post: OpenSSH deny or restrict access to users and groups