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(运行前需要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的搭建的更多相关文章
- lighttpd+fastcgi模块分析
一开始不怎么明白fastcgi和cgi的区别,查了资料说,fastcgi多了一个进程池,不要每次都fork和退出 这个不是重点,还是对着代码看吧 怎样在lighttpd运行php呢,需要下面这样配置 ...
- Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器[摘抄]
[文章作者:张宴 本文版本:v6.3 最后修改:2010.07.26 转载请注明原文链接:http://blog.s135.com/nginx_php_v6/] 前言:本文是我撰写的关于搭建“Ngin ...
- Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)
前言:本文是我撰写的关于搭建“Nginx + PHP(FastCGI)”Web服务器的第6篇文章.本系列文章作为国内最早详细介绍 Nginx + PHP 安装.配置.使用的资料之一,为推动 Nginx ...
- lighttpd 与 gitweb 搭建服务器
搭建 Git 仓库服务器 下载 gitweb 如果是用 debian 系的 Linux 发行版,可以使用 apt 下载安装可执行的 gitweb sudo apt-get install gitweb ...
- Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)(转)
转自:http://blog.s135.com/nginx_php_v6/] 前言:本文是我撰写的关于搭建“Nginx + PHP(FastCGI)”Web服务器的第6篇文章.本系列文章作为国内最早详 ...
- Windows下Nginx+Web.py+FastCGI服务搭建
在搭建之前,有必要了解下什么是fastcgi,但鉴于我自己也不大了解,这里就不搬门弄斧了,请参考各种百科和官网资料. 1.资源下载 python下载地址:戳这里webpy下载地址:戳这里flup下载地 ...
- Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器
[文章作者:张宴 本文版本:v6.3 最后修改:2010.07.26 转载请注明原文链接:http://blog.zyan.cc/nginx_php_v6/] 前言:本文是我撰写的关于搭建“Nginx ...
- 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 ...
- 在树莓派里搭建 Lighttpd 服务器
Lighttpd 像 Ngnix 一样,是被设计运行在低内存,低 CPU 负载的设备上,它们都非常适合在树莓派上运行. 本文将介绍如何在树莓派上运行基本配置的 Lighttpd ,以及如何与 PHP- ...
随机推荐
- POJ 2369 Permutations
傻逼图论. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)
题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
- Mac下配置环境变量
1.创建并以 TextEdit 的方式打开 ~/.bash_profile 文件,如果没有则 touch ~/.bash_profile; 然后打开 vim ~/.bash_profile 2.新增环 ...
- MAC自动备份数据到服务器
需求:公司内部使用自己电脑,回家需要使用另一台电脑,所以想时时备份公司用的电脑中文件.代码到服务器上,回家就可以用啦 1 无密码使用scp (1)第一步:生成密匙对,我用的是rsa的密钥.使用命令 & ...
- 【英语】Bingo口语笔记(5) - 英式和美式英语的发音区别
- Filling a Path 模式
Filling a Path When you fill the current path, Quartz acts as if each subpath contained in the path ...
- 基于HTTP的直播点播HLS
HLS(HTTP Live Streaming) 是Apple在2009年发布的,可以通过普通的web服务器进行分发的新型流媒体协议.苹果官方对于视频直播服务提出了 HLS 解决方案 ...
- NPOI 2.0导出word(docx格式)
大名鼎鼎的NPOI用来导出EXCEL的文章园子里面有很多,可是用来导出WORD文档的文章大都含糊不清,最近刚好完成一个导出WORD文档的需求,在此分享下. NPOI里面认为word文档的最基本的结构是 ...
- (C#)RichTextBox控件
RichTextBox(有格式文本)控件可实现TextBox控件的所有功能. ❶在RichTextBox控件中显示滚动条 RichTextBox可设置Multiline属性来控制是否显示滚动套,tru ...
- 【转】linux_fdisk命令详解
转自:http://www.cnblogs.com/xiaofengkang/archive/2011/06/06/2073579.html fdisk -l 可以列出所有的分区,包括没有挂上的分区和 ...