【LINUX】教你如何构建支持lighttpd + cgi的环境

来源:互联网 发布:福州网络推广 编辑:程序博客网 时间:2024/05/19 12:39

一、 前期准备


1,、系统: Linux f8s 2.6.23.1-42.fc8 #1 SMP Tue Oct 30 13:55:12 EDT 2007 i686 i686 i386 GNU/Linux

2、web服务器: lighttpd/1.4.39 - a light and fast webserver


二、编译lighttpd


1、解压tar -xzvf lighttpd-1.4.39.tar.gz

2、进入lighttpd-1.4.39目录下,执行 ./configure --prefix=/home/lizj/0002_linux/lighttpd/lighttpd_project(这是你自己要编译安装的目录)

3、make

4、make install

5、执行以上步骤之后,lighttpd_project目录下就会生成lib sbin share三个目录


三、配置支持cgi


1、lighttpd_project目录下新建文件夹cgi,用于存放用C语言编写的cgi程序
2、lighttpd_project目录下新建文件夹config,将源码目录下doc/config下的 conf.d,vhosts.d,lighttpd.conf,modules.conf都拷贝过来
3、lighttpd_project目录下新建文件夹log,并创建文件access.log和error.log
4、lighttpd_project目录下新建文件夹state
5、lighttpd_project目录下新建文件夹www/htdocs/cgi-bin, 用于存放编译的cgi二进制文件
6、修改配置文件lighttpd.conf,具体修改如下所示

var.log_root    = "/home/lizj/0002_linux/lighttpd/lighttpd_project/log"var.server_root = "/home/lizj/0002_linux/lighttpd/lighttpd_project/www"var.state_dir   = "/home/lizj/0002_linux/lighttpd/lighttpd_project/state"var.home_dir    = "/home/lizj/0002_linux/lighttpd/lighttpd_project/lib"var.conf_dir    = "/home/lizj/0002_linux/lighttpd/lighttpd_project/config"server.document-root = server_root + "/htdocs" (注意,这个是提交表单的时候,以这个为基准的相对目录) include "../config/modules.conf"include "../config/conf.d/access_log.conf"include "../config/conf.d/debug.conf"include "../config/conf.d/mime.conf"
7、修改配置文件modules.conf,具体修改如下所示
server.modules = (  "mod_access",#  "mod_cgi",    "mod_alias",#  "mod_fastcgi",  "mod_compress"#  "mod_auth",#  "mod_evasive",#  "mod_redirect",#  "mod_rewrite",#  "mod_setenv",#  "mod_usertrack",) include "conf.d/cgi.conf"  // 注意一定要增加这个,否则提交表单之后,cgi没有解析
8、修改配置文件cgi.conf,具体修改如下所示
cgi.assign                 = ( ".pl"  => "/usr/bin/perl",   #".cgi" => "",                               ".cgi" => "/usr/bin/perl",                               ".rb"  => "/usr/bin/ruby",                               ".erb" => "/usr/bin/eruby",                               ".py"  => "/usr/bin/python" ) $HTTP["url"] =~ "^(/~[^/]+)?/cgi-bin/" {    cgi.assign = ("" => "")} 

四、测试验证


1、进入sbin目录下,执行./lighttpd -f ../config/lighttpd.conf
2、netstat -ltp命令查看是否已经启动


一) 直接显示


1、cgi目录下,新建文件hello.c输入如下内容

#include "stdio.h" int main(void) {  printf( "Content-type: text/plain\r\n\r\n ");  //printf("%s%c%c ","Content-Type:text/html;charset=gb2312",13,10);   printf("Hello world !\n");  return 0;}
2、编译gcc hello.c -o hello.git
3、将hello.git拷贝到www/htdocs/cgi-bin
4、输入elinks 127.0.0.1/cgi-bin/hello.cgi,如果显示:Hello world !, 说明成功 


二)交互显示


1、 cgi目录下,新建文件mult.c,输入如下内容

#include <stdio.h>#include <string.h> int main(void){    char *data;    long m,n;     //printf("%s\n\n","Content-Type:text/html;charset=gb2312");printf("%s\n\n","Content-Type:text/html");printf("<meta charset=\"utf-8\">");    printf(" ");    printf("[result] :\n");     data = getenv("QUERY_STRING"); //获得form传来的参数——两个乘数m和n    if(data == NULL)        printf("error, data is null\n");//printf("错误!数据没有被输入或者数据传输有问题");    else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2)        printf("error, invalid param.\n");//printf("错误!输入数据非法。表单中输入的必须是数字。");    else        printf("%ld * %ld = %ld \n",m,n,m*n);    return 0;}
2、编译gcc multi.c -o mult.cgi
3、将mult.git拷贝到www/htdocs/cgi-bin
4、www/htdocs目录下,新建index.html,输入如下内容
<html> <head><title>first cgi page</title></head> <body><form ACTION="/cgi-bin/mult.cgi" method = "get"> <P>please input number: <BR> <INPUT NAME="m" SIZE="5"><BR> <INPUT NAME="n" SIZE="5"><BR> <INPUT TYPE="SUBMIT" values="ok"> </form> </body></html>
5、elinks 127.0.0.1, 显示html的页面内容,第一个方框输入2, 第二方框输入3, 最终提交,显示如下[result] : 2 * 3 = 6

五、问题解决


1、configure: error: pcre-config not found, install the pcre-devel package or build with --without-pcre
解决办法:需要安装一个软件包pcre(Perl兼容的规则表达式库):yum install pcre-devel

2、can't find username lighttpd
解决办法:groupadd -g 208 lighttpd

useradd -u 208 -g lighttpd -d /home/lizj/0002_linux/lighttpd/lighttpd_project/www lighttpd

3、Duplicate array-key: .cgi
解决办法:cgi.cong中cgi.assign定义了两个".cgi" => "", ".cgi" => "/usr/bin/perl", 去掉其中一个即可


六、代码下载

代码包下载


0 0