1.lighttpd 服务器
lighttpd是一个比较轻量的服务器,在运行fastcgi上效率较高。lighttpd只负责投递请求到fastcgi。
centos输入yum install lighttpd安装
2.fastcgi
fastcgi解决了cgi程序处理请求每次都要初始化和结束造成的性能问题。fastcgi并且是独立于webserver的,fastcgi的crash并不影响webserver,然后他们之间通过soket通信。与fastcgi不同的另一种解决cgi程序反复创建,销毁的方法是让webserver开放api,然后编写cgi的时候,把cgi嵌入到webserver中,这样有个不好的地方就是cgi的crash会影响到webserver。
支持fastcgi的服务器有很多比如,nginx IIS什么的。他们都是把http请求转换为stdin和一些环境变量传递给fastcgi程序,然后返回stdout。编写fastcgi程序最终要的一个api就是int FCGI_Accept(void);当一个请求被送达的时候返回。一个基本的fastcgi结构如下。
echo.c
#include "fcgi_stdio.h" #include <stdlib.h> void main(void) { //初始化一些全局变量,这里只在fastcgi启动时候运行一次 while(FCGI_Accept() >= 0) { //fastcgi的处理逻辑 //根据http协议返回处理结果 printf("Content-type: text/html "); printf(" "); printf("hello world"); } }
3.编译echo.c
编译时候首先要安装fastcti
wget http://www.fastcgi.com/dist/fcgi.tar.gz
tar -zxvf fcgi.tar.gz
cd fcgi-2.4.1-SNAP-0311112127/
./configure --prefix=/etc/fcgi
make && make install
出现fcgio.cpp:50: error: 'EOF' was not declared in this scope的话 在/include/fcgio.h文件中加上 #include <cstdio>
到此就安装完了。然后我们来编译echo.c
gcc echo.c -o echo.cgi -I/etc/fcgi/include -L/etc/fcgi/lib/ -lfcgi
注意-I -L -l都不能少。
4.lighttpd+fastcgi配置
lighttpd支持fastcgi需要安装fastcgi模块
yum install lighttpd-fastcgi
创建配置文件 vim /etc/fcgi/fcgi.conf
server.document-root = "/var/www/cgi-bin/"
server.port = 8080
server.username = "www-data"
server.groupname = "www-data"
server.modules = ("mod_access", "mod_accesslog", "mod_fastcgi")
server.errorlog = "/var/www/cgi-bin/error.log"
accesslog.filename = "/var/www/cgi-bin/access.log"
static-file.exclude-extensions = (".cgi" )
fastcgi.debug = 1
fastcgi.server = (
"echo.cgi" => ((
"host" => "127.0.0.1",
"port" => "9000",
"bin-path" => "/var/www/cgi-bin/echo.cgi",
))
)
lighttpd -D -f fcgi.conf 时候会出现error while loading shared libraries: libfcgi.so.0: cannot open shared object file: No such file or directory
反正就是找不到动态链接库,比较无脑的方式就是把fastcgi安装目录下的include和lib都拷贝到/usr/include 和 /usr/lib中。注意,不是/usr/local
设置过之后可能还是找不到,参考网上另一种方法是。
首先vim /etc/ld.so.conf 添加libfcgi.so.0的路径,然后运行/sbin/ldconfig,重新编译trie.cgi,ldd trie.cgi一下,看到libfcgi.so.0找到就OKAY了。
在浏览器输入http://127.0.0.1:8080/echo.cgi 就可以看到hello world了
5.实例 输入提示
输入提示的具体实现方法已经在这篇博客里说过 http://www.cnblogs.com/23lalala/p/3513492.html
这次把他通过fastcgi的api改写成fastcgi程序。
trie.c
#include "stdio.h" #include "stdlib.h" #include "string.h" #include "malloc.h" #include "fcgi_stdio.h" char* strcatch(char *str, char ch) { char *p = str; while (*p!='