公司很久以前有个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(运行前需要export LD_LIBRARY_PATH=/usr/local/libfcgi/lib)

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

就可返回结果:

对上面sample 稍作修改,以处理GET和POST请求。

#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <alloca.h>
#include <fcgiapp.h>
#include <stdio.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; }

    #if 1
    char* header = (char*) alloca(200);
    strcpy(header, "Status: 200 OK\r\nContent-Type: text/html\r\n\r\n");
    FCGX_PutStr(header, strlen(header), cgi.out);
    char response[1024];
    if(!strncmp("GET", FCGX_GetParam("REQUEST_METHOD", cgi.envp), strlen("GET")))
    {
      FCGX_PutStr("GET", strlen("GET"), cgi.err);
      if(!strncmp("action=getInfo", FCGX_GetParam("QUERY_STRING", cgi.envp), strlen("action=getInfo")))
      {
        snprintf(response, sizeof(response), "{\"action\": \"getInfo\"}\n");
      }
    }
    else if(!strncmp("POST", FCGX_GetParam("REQUEST_METHOD", cgi.envp), strlen("POST")))
    {
      char *post_data = (char*)malloc(1024);
      FCGX_GetStr(post_data, 1024, cgi.in);
      FCGX_PutStr(post_data, 1024, cgi.err);
      if(!strncmp("action=resetInfo", post_data, strlen("action=resetInfo")))
      {
        snprintf(response, sizeof(response), "{\"action\": \"resetInfo\"}\n");
      }
    }
    FCGX_PutStr(response, strlen(response), cgi.out);
    #endif
  }
  return 0;
}

curl -v http://localhost:9090/fellow?action=getInfo

结果如下:

curl -v -d "action=resetInfo" http://localhost:9090/fellow

结果如下:

lighttpd fastcgi的搭建的更多相关文章

  1. lighttpd+fastcgi模块分析

    一开始不怎么明白fastcgi和cgi的区别,查了资料说,fastcgi多了一个进程池,不要每次都fork和退出 这个不是重点,还是对着代码看吧 怎样在lighttpd运行php呢,需要下面这样配置 ...

  2. Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器[摘抄]

    [文章作者:张宴 本文版本:v6.3 最后修改:2010.07.26 转载请注明原文链接:http://blog.s135.com/nginx_php_v6/] 前言:本文是我撰写的关于搭建“Ngin ...

  3. Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)

    前言:本文是我撰写的关于搭建“Nginx + PHP(FastCGI)”Web服务器的第6篇文章.本系列文章作为国内最早详细介绍 Nginx + PHP 安装.配置.使用的资料之一,为推动 Nginx ...

  4. lighttpd 与 gitweb 搭建服务器

    搭建 Git 仓库服务器 下载 gitweb 如果是用 debian 系的 Linux 发行版,可以使用 apt 下载安装可执行的 gitweb sudo apt-get install gitweb ...

  5. Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)(转)

    转自:http://blog.s135.com/nginx_php_v6/] 前言:本文是我撰写的关于搭建“Nginx + PHP(FastCGI)”Web服务器的第6篇文章.本系列文章作为国内最早详 ...

  6. Windows下Nginx+Web.py+FastCGI服务搭建

    在搭建之前,有必要了解下什么是fastcgi,但鉴于我自己也不大了解,这里就不搬门弄斧了,请参考各种百科和官网资料. 1.资源下载 python下载地址:戳这里webpy下载地址:戳这里flup下载地 ...

  7. Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器

    [文章作者:张宴 本文版本:v6.3 最后修改:2010.07.26 转载请注明原文链接:http://blog.zyan.cc/nginx_php_v6/] 前言:本文是我撰写的关于搭建“Nginx ...

  8. Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)[原创]

    mkdir -p /data0/software cd /data0/software wget http://sysoev.ru/nginx/nginx-0.8.46.tar.gz wget htt ...

  9. 在树莓派里搭建 Lighttpd 服务器

    Lighttpd 像 Ngnix 一样,是被设计运行在低内存,低 CPU 负载的设备上,它们都非常适合在树莓派上运行. 本文将介绍如何在树莓派上运行基本配置的 Lighttpd ,以及如何与 PHP- ...

随机推荐

  1. ISO中运行时简单使用及KVC补充

    一.运行时简单使用 1.包含头文件<objc/message.h> 2.给对象发送消息的方法:objc_msgSend(id, SEL, ....) * 第1个参数是对象 * 第2个参数是 ...

  2. Myeclipse提示失效?

  3. ecshop 广告位固定

    不知道ECSHOP用户们发现没有,如果在一个广告位中添加多个广告图片, 在前台显示的时候,每刷新一次,图片的显示顺序就会随机变化一次. 注:如果给广告位只添加一个图片是没有这种问题的. 现在的问题是: ...

  4. SELinux Mysql的error-log文件位置的指定

    SELinux下,在配置my.cnf时,必须指定error-log的位置在/var/log/下, 否则error的默认位置为例如 /var/lib/mysql下的tyoyi.server.err文件, ...

  5. 最冤枉的关键字----sizeof

    <h4>一.常年被人误认为函数.</h4> sizeof 是关键字不是函数,其实就算不知道它是否为32 个关键字之一时,我们也可以借助编译器确定它的身份.看下面的例子: int ...

  6. python tile函数用法

    tile函数位于python模块 numpy.lib.shape_base中,他的功能是重复某个数组.比如tile(A,n),功能是将数组A重复n次,构成一个新的数组,我们还是使用具体的例子来说明问题 ...

  7. MySQL与Oracle 差异比较之四条件循环语句

    循环语句 编号 类别 ORACLE MYSQL 注释 1 IF语句使用不同 IF iv_weekly_day = 'MON' THEN       ii_weekly_day := 'MON';ELS ...

  8. UIScrollView 不能滚动的问题

    uiscrollview是开发sdk自带的控件, 在使用的时候,发现滚动不了, 最常山见的原因是 contentSize 这个属性,比uiscrollview的frame要小...所以无需滚动,自然就 ...

  9. Delphi 异或,英文为exclusive OR,或缩写成xor

    异或,英文为exclusive OR,或缩写成xor 异或(xor)是一个数学运算符.它应用于逻辑运算.异或的数学符号为“⊕”,计算机符号为“xor”.其运算法则为: a⊕b = (¬a ∧ b) ∨ ...

  10. Groovy获取json和xml数据

    如果是xml就用这个 // to read a node from your Response def grUtils = new com.eviware.soapui.support.GroovyU ...