Trung Nguyen
Trung Nguyen

Reputation: 868

How to config lighttpd and fcgi for c script?

I have implementing a program that using lighttp web server and fcgi(c script). And I have searched many times but don't find any page guide to do this. Just setting lighttpd and python fcgi or php fcgi... but C fcgi. Can anyone help me to configure lighttpd and c fcgi? Many thanks.

I have wrote a sample as below, and build it to executable file. But now I don't know how to run it with lighttpd web server.

#include "fcgi_stdio.h"
#include <stdlib.h>
#include <stdio.h>
int count;
using namespace std;
void initialize(void)
{
  count=0;
}

int main(void)
{
/* Initialization. */
  initialize();

/* Response loop. */
  while (FCGI_Accept() >= 0)   {
    printf("Content-type: text/html\r\n"
           "\r\n"
           "<title>FastCGI Hello! (C, fcgi_stdio library)</title>"
           "<h1>FastCGI Hello! (C, fcgi_stdio library)</h1>"
           "Request number %d running on host <i>%s</i>\n",
            ++count, getenv("SERVER_HOSTNAME"));
  }
  return 0;
}

Upvotes: 2

Views: 4958

Answers (1)

fycth
fycth

Reputation: 3489

By default, lighthttp allows execute cgi scripts in directory 'cgi-bin' only. So, just put your cgi program in '/cgi-bin' and it should work with default configuration.

This is example of cgi in C

#include <stdio.h>

int main(void)
{
   printf("Content-type: text/plain\n\n");
   puts("Hello from cgi-bin!...");
   return 0;
}

compiling

cc 1.c -o 1

Test

wget localhost:82/cgi-bin/1
--2014-03-20 16:27:36--  http://localhost:82/cgi-bin/1
Resolving localhost (localhost)... 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:82... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/plain]
Saving to: `1'

    [ <=>                                                                                                                              ] 21          --.-K/s   in 0s

2014-03-20 16:27:37 (1.08 MB/s) - `1' saved [21]

cat 1
hello from C cgi!...

UPDATED

By default, you have configuration file for cgi placed here:

/etc/lighttpd/conf-available/10-cgi.conf

You have to make a symlinc and restart lighthttpd

sudo ln -s /etc/lighttpd/conf-available/10-cgi.conf /etc/lighttpd/conf-enabled/10-cgi.conf

This is what have to be inside the cgi config file

cat /etc/lighttpd/conf-available/10-cgi.conf

server.modules += ( "mod_cgi" )

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

Upvotes: 1

Related Questions