ARM Lighttpd Fastcgi C

CGI能够使用C语言编写,以通过共享内存等方式与其他C语言进程通讯。对CGI程序的每个HTTP请求,web服务器都会创建一个新的CGI进程,web服务器通过环境变量传递http请求,CGI程序通过标准输出返回处理结果,然后退出。

fastcgi使用循环响应HTTP请求,服务器负载比cgi小,能与其他程序(如数据库)保持持续链接。fastcgi可以使用tcp或UNIX domain与web服务器通讯,并将HTTP请求转换为环境变量传入,TCP方式中使用标准输出返回处理结果,UNIX domain方式则使用fastcgi封装的函数。cgi-fcgi程序能够将cgi程序作为fastcgi程序运行。

lighttpd支持fastcgi,能够启动fastcgi程序,并进行负载均衡。

【1】交叉编译。源文件都从官网下载。

http://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.33.tar.bz2 

http://www.fastcgi.com/dist/fcgi.tar.gz

http://zlib.net/zlib-1.2.8.tar.gz

tar xf zlib-1.2.8.tar.gz                                
cd zlib-1.2.8                                            
CC=arm-linux-gcc ./configure --prefix=/home/armroot
make                                                    
make install
tar xf fcgi.tar.gz
cd fcgi-2.4.1-SNAP-0311112127/
./configure --host=arm-linux --prefix=/home/armroot
make
make install
tar xf lighttpd-1.4.33.tar.bz2 
cd lighttpd-1.4.33
 ./configure --host=arm-linux --prefix=/home/armroot --without-bzip2 --without-pcre
make
make install

拷贝可执行文件(这里是/home/armroot/sbin/lighttpd) 到设备中(我放在了/usr/local/bin/下)。

拷贝lighttpd的mod(这里是/home/armroot/lib/mod*.so)到设备中(我放在了/opt/lighttpd下)

【2】配置服务器

/etc/lighttpd.conf

server.document-root="/www"
server.port=80
mimetype.assign=(
    ".html" => "text/html",
    ".json" => "text/plain",
    ".css" => "text/css"
)
server.modules = ("mod_fastcgi",                                                  "mod_compress",                                                 
                )                                                                                
server.errorlog            = "/tmp/lighttpd/error.log"                          
server.indexfiles           = ("index.html")                                                                      
                                                                                
compress.cache-dir = "/tmp/lighttpd/cache/"                                     
compress.filetype = ("text/plain","text/html","text/css","text/javascript")                                                                                                                    
                                                                                
fastcgi.debug=0                                                                 
fastcgi.server = (                                                              
            ".fcgi" =>                                                          
            (                                                                   
                "main" =>                                                       
                (                                                               
                    "host" => "127.0.0.1",                                      
                    "port" => 2048,                                             
                    "bin-path" => "/www/fastcgi.fcgi",                          
                    "max-procs" => 1,                                           
                )                                                               
            )                                                                   
        )

【3】fastcgi程序

test.c:

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcgi_stdio.h>
int count = 0;
int main()
{
    while(FCGI_Accept() >= 0)
    {
        int width = 0;
        int height = 0;
        char *query_string = getenv("QUERY_STRING");
        if(query_string != NULL)
            sscanf(query_string, "width=%d&height=%d", &width, &height);
     printf(
"Content type: text/html\n\n"); printf("width=%d,height=%d\n",width,height); printf("pid=%d\n",getpid()); printf("count=%d\n",count); count++; } return 0; }

Makefile:

CROSS_PREFIX=arm-linux-

CC=$(CROSS_PREFIX)gcc
STRIP=$(CROSS_PREFIX)strip

ARMROOT=/home/armroot
LIB=$(ARMROOT)/lib
INC=$(ARMROOT)/include

CFLAGS= -g -I$(INC) --std=gnu99 -W -Wall -O0

SRC=$(shell find ./ -name '*.c')
OBJ=$(SRC:%.c=%.o)
DEP=$(SRC:%.c=%.dep)

#LIBS:动态库
LIBS=-lpthread -lfcgi -lz

#SLIBS:静态库
SLIBS=
TARGET=fastcgi

$(TARGET): $(DEP) $(OBJ)
    @echo -n linking...
    @$(CC) $(SLIBS) $(OBJ) -o $(TARGET) $(CFLAGS) -L$(LIB) $(LIBS)
    @echo done: $(TARGET)

.PHONY:strip
strip:$(TARGET)
    @if [ -e $(TARGET)_g ];then rm $(TARGET)_g;fi
    @cp $(TARGET) $(TARGET)_g
    @$(STRIP) $(TARGET)
%.dep:%.c
    @$(CC) -MM $< $(CFLAGS) > $@__
    @if [ -e $@__ ];then echo -n $(dir $@) > $@;cat $@__ >> $@;rm $@__;fi
sinclude $(DEP)
%.o:%.c
    @echo building $<
    @$(CC) -c $< -o $@ $(CFLAGS)
.PHONY:clean
clean:
    @for i in $(DEP);do if [ -e $${i} ];then rm $${i};fi;done
    @for i in $(OBJ);do if [ -e $${i} ];then rm $${i};fi;done
    @if [ -e $(TARGET) ];then rm $(TARGET);fi

将生成的文件拷贝到设备/www目录中

【4】启动服务器

lighttpd -f /etc/lighttpd.conf -m /opt/lighttpd 

-f 选项指定配置文件

-m 选项指定mod所在目录,默认为编译lighttpd时的prefix/lib,我这里就是/home/armroot/lib。

lighttpd会在初始化时启动配置文件中指定的fastcgi程序,这里是/www/fastcgi

【5】测试

ps

523 root       0:00 lighttpd -f /etc/lighttpd.conf  -m /opt/lighttpd          
542 root       0:00 /www/fastcgi

浏览器访问

http://192.168.1.127/fastcgi?width=800&height=480

结果:

width=800,height=480 
pid=542 
count=0
width=800,height=480 
pid=542 
count=1

 

posted on 2013-11-12 10:16  jacob1934  阅读(697)  评论(0)    收藏  举报

导航