首页 > 代码库 > lighttpd fastcgi的搭建

lighttpd fastcgi的搭建

公司很久以前有个task需要在板子上搭建个webserver以响应局域网内手机的请求。

以前是用lighttpd plugin实现的,后来仔细想想用fast cgi来弄也可以。

在install lighttpd之前,先要install pcre和fcgi。我习惯install到/usr/local/pcre和/usr/local/libfcgi.

写个简单的配置文件,如下:

server.document-root = "/mnt/hgfs/share/test/fcgi/"
server.port = 9090
socket_dir = "/tmp/"
server.username = "weifeilong"
server.groupname = "weifeilong"
server.errorlog = "/mnt/hgfs/share/test/fcgi/err.log"
mimetype.assign = (
".html" => "text/html",
".txt" => "text/plain",
".jpg" => "image/jpeg",
".png" => "image/png"
)

static-file.exclude-extensions = ( ".fcgi", ".php", ".rb", "~", ".inc" )
index-file.names = ( "index.html" )
server.modules += ("mod_fastcgi")
fastcgi.server = (
   "/fellow" => (
    "test.fastcgi.handler" => (
    "socket" => socket_dir + "test.fastcgi.socket",
    "check-local" => "disable",
    "bin-path" => "/mnt/hgfs/share/test/fcgi/test.fastcgi",
    "max-procs" => 10,
  )
  )
)

这里fastcgi程序就用官网的sample,

#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <alloca.h>
#include <fcgiapp.h>
#define LISTENSOCK_FILENO 0
#define LISTENSOCK_FLAGS 0
int main(int argc, char** argv) {
  openlog("testfastcgi", LOG_CONS|LOG_NDELAY, LOG_USER);
  int err = FCGX_Init(); /* call before Accept in multithreaded apps */
  if (err) { syslog (LOG_INFO, "FCGX_Init failed: %d", err); return 1; }
  FCGX_Request cgi;
  err = FCGX_InitRequest(&cgi, LISTENSOCK_FILENO, LISTENSOCK_FLAGS);
  if (err) { syslog(LOG_INFO, "FCGX_InitRequest failed: %d", err); return 2; }

  while (1) {
    err = FCGX_Accept_r(&cgi);
    if (err) { syslog(LOG_INFO, "FCGX_Accept_r stopped: %d", err); break; }
    char** envp;
    int size = 200;
    for (envp = cgi.envp; *envp; ++envp) size += strlen(*envp) + 11;
    char* result = (char*) alloca(size);
    strcpy(result, "Status: 200 OK\r\nContent-Type: text/html\r\n\r\n");
    strcat(result, "<html><head><title>testcgi</title></head><body><ul>\r\n");

    for (envp = cgi.envp; *envp; ++envp) {
    strcat(result, "<li>");
    strcat(result, *envp);
    strcat(result, "</li>\r\n");
    }

  strcat(result, "</ul></body></html>\r\n");
  FCGX_PutStr(result, strlen(result), cgi.out);
  }

  return 0;
}

gcc -I/usr/local/libfcgi testfastcgi.c -L/usr/local/libfcgi/lib -lfcgi -o test.fastcgi

启动lighttpd:/usr/local/lighttpd/sbin/lighttpd -f /mnt/hgfs/share/test/fcgi/lighttpd.conf

通过curl -v http://localhost:9090/fellow

就可返回结果:

技术分享

 

lighttpd fastcgi的搭建