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/ (DocumentRoot), create cgi-bin as follows:
# mkdir -p /home/lighttpd/

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"]  =~ "" {
  server.document-root = "/home/lighttpd/"
  accesslog.filename         = "/var/log/lighttpd/"
  $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/

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

Save and execute the program (

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 23 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
23 comments… add one
  • james Aug 25, 2008 @ 2:04

    thanks mate, works sweet as

  • Leszek Sep 25, 2008 @ 22:43

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

  • Thomas Martin Klein Feb 8, 2009 @ 16:02

    Thanks for the info… it works like a charm,

  • Amir Mar 16, 2009 @ 15:34

    A life saver!
    Thank you.

  • Dmitry Mar 26, 2009 @ 9:30

    Thank you!

    Trying this sample I got 403 Forbidden from lighttpd.

    Would you please, explain why?

    Thanks again.

    • 🐧 nixCraft Mar 26, 2009 @ 13:20

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

      Also, look at your error log file.

  • Dmitry Mar 26, 2009 @ 15:48

    Thanks Vivek,

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


  • nobody Jun 10, 2009 @ 7:19

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

  • Twitter Friendly Jun 11, 2009 @ 20:03

    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.

  • Hawk Jun 12, 2009 @ 4:09

    @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?

  • Kilbane Jul 13, 2009 @ 12:02

    Really easy and fin tutorial 🙂
    Works fine for me 🙂

  • Durand Jul 21, 2009 @ 22:52

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

  • NK Aug 2, 2009 @ 21:02

    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”] =~ “” {
    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/ is 755.

    Clicking on 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: – [02/Aug/2009:13:53:29 -0700] “GET /cgi-bin/ HTTP/1.1” 404 345 “-” “Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/2009042315 Firefox/3.0.10”

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

  • NK Aug 2, 2009 @ 21:47

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

    • LCN Sep 4, 2010 @ 4:37

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

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

      with this one:

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

  • john plumridge Mar 27, 2010 @ 15:51

    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?

  • nishu Apr 9, 2010 @ 8:35

    Hi All,

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

  • john plumridge Apr 23, 2010 @ 22:54

    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

  • Luke Jun 6, 2010 @ 4:49

    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.

  • g.. Aug 25, 2011 @ 19:40

    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 it get an 403 Forbidden message.
    How could I make it to work also from the outside?


  • none-101 Oct 18, 2011 @ 11:08

    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/
    ScriptAlias /cgi-bin/ /www/cgi-bin/

  • adipa Jan 6, 2012 @ 7:43

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

  • Razique May 24, 2013 @ 8:21

    Perfect tutorial!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum