Lighttpd Setup CGI-BIG CGI For Perl Programs

by on July 29, 2006 · 23 comments· LAST UPDATED May 23, 2009

in , ,

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).

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 23 comments… read them below or add one }

1 james August 25, 2008 at 2:04 am

thanks mate, works sweet as

Reply

2 Leszek September 25, 2008 at 10:43 pm

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

Reply

3 Thomas Martin Klein February 8, 2009 at 4:02 pm

Thanks for the info… it works like a charm,

Reply

4 Amir March 16, 2009 at 3:34 pm

A life saver!
Thank you.

Reply

5 Dmitry March 26, 2009 at 9:30 am

Thank you!

Trying this sample I got 403 Forbidden from lighttpd.

Would you please, explain why?

Thanks again.

Reply

6 nixCraft March 26, 2009 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.

Reply

7 Dmitry March 26, 2009 at 3:48 pm

Thanks Vivek,

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

Thanks.

Reply

8 nobody June 10, 2009 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…

Reply

9 Twitter Friendly June 11, 2009 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.

Reply

10 Hawk June 12, 2009 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?

Reply

11 Kilbane July 13, 2009 at 12:02 pm

Really easy and fin tutorial :)
Works fine for me :)

Reply

12 Durand July 21, 2009 at 10:52 pm

Mostly worked well. In gentoo, I had to add this line to get it to work:
include “mod_cgi.conf”

Reply

13 NK August 2, 2009 at 9:02 pm

I just installed lightppd on Mandriva Linux 2008, and added following to /etc/lighttpd/lighttpd.conf, and restarted lighttpd:

server.modules += ( “mod_cgi” )
$HTTP["host"] =~ “127.0.0.1″ {
server.document-root = “/var/www/html”
accesslog.filename = “/var/log/lighttpd/access.log”
$HTTP["url"] =~ “/cgi-bin” {
cgi.assign = ( “.pl” => “/usr/bin/perl” )
}
}

/var/www/cgi-bin/test.pl is 755.

Clicking on http://127.0.0.1/cgi-bin/test.pl gives “404 – Not Found”. Would you happen to know why? /var/log/lighttpd/error.log does not show anything, but /var/log/lighttpd/access.log has an entry:
127.0.0.1 127.0.0.1 – [02/Aug/2009:13:53:29 -0700] “GET /cgi-bin/test.pl HTTP/1.1″ 404 345 “-” “Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10″

I have tried replacing 127.0.0.1 with localhost in the line ‘$HTTP["host"] =~ “127.0.0.1″ {‘, but that did not help either.

Reply

14 NK August 2, 2009 at 9:47 pm

I just turned ON the debug and noticed that it was trying to run /var/www/html/cgi-bin/test.pl, instead of /var/www/cgi-bin/test.pl. Changing server.document-root = “/var/www/html” to “/var/www” fixed it.

Reply

15 LCN September 4, 2010 at 4:37 am

I had this same problem, too. Indeed, the author’s configuration file should replace this entry:

server.document-root = “/home/lighttpd/theos.in/http”

with this one:

server.document-root = “/home/lighttpd/theos.in”

Reply

16 john plumridge March 27, 2010 at 3:51 pm

I have lighttpd running fine with cgi scripts form localhost/cgi-bin.
I would like to use however, cgi scripts from any directory (i.e. globally), a desire which echoes an earlier post. I can’t find informaton on this, and cannot figure it out.
Any ideas?

Reply

17 nishu April 9, 2010 at 8:35 am

Hi All,

Need ur help. I m new in this cgi stuff and faling again and again

Reply

18 john plumridge April 23, 2010 at 10:54 pm

@Nishu
CHeck wher eyour perl distribution is located from in your bash path, if on Unix or Linux:

In terminal: type

$ which perl

Your path might not be usr/bin/perl

Reply

19 Luke June 6, 2010 at 4:49 am

If you have cgi programs that are written in C how do you set it up? I have a bunch of C based code that is directly executable and I want to be able to execute from the web.

Reply

20 g.. August 25, 2011 at 7:40 pm

It works when I access the perl with a browser on the server with http://localhost/….
However when I try to access the perl with the external IP http://188.61.81.120 it get an 403 Forbidden message.
How could I make it to work also from the outside?

/g..

Reply

21 none-101 October 18, 2011 at 11:08 am

Does not show how to specify the cgi-bin.

e.g in Apache I have the cgi-bin located elsewhere (on a separate partition)
DocumentRoot /www/admin.example.com/
…….
ScriptAlias /cgi-bin/ /www/cgi-bin/admin.example.com/

Reply

22 adipa January 6, 2012 at 7:43 am

Sorry, I confused installing setup on cgi-bin folder for powersek program. please help me…
thanks

Reply

23 Razique May 24, 2013 at 8:21 am

Perfect tutorial!

Reply

Leave a Comment

Tagged as: , , , , , , , ,

Previous post:

Next post: