您的位置:首页 > 理论基础 > 计算机网络

fedora 20 安装lighttpd 并配置使用其CGI

2014-04-10 17:58 381 查看
由于最近工作需要搭建http服务器,在fedora 20 ,我们可选的http服务器一般有两个:Apache 和 lighttpd 。Apache是fedora 默认使用的http服务器,使用yum install httpd 即可安装。
这里我选用的是lighttpd,安装过程如下:

su -

进入管理员身份,

yum -y install lighttpd

等待安装完成,

systemctl restart lighttpd.service //重启服务

如果有需要也可以设为开机启动该服务器

systemctl enable lighttpd.service  //设置开机启动服务

先测试下web服务器是否正常,在浏览器输入本机IP或localhost,然后按回车,如果出现下面这一图片,则说明已经
正确安装啦。



安装完成lighttpd后,还要配置一下我们才能使用它的cgi(关于cgi的介绍可以参考百度百科或者维基百科)。

接下来就是要修改配置文件了,

kwrite /etc/lighttpd/modules.conf&

将137行的 include "conf.d/cgi.conf"前面的#号去掉,目的是添加cgi 模式(mod_cgi)保存退出,

kwrite /etc/lighttpd/lighttpd.conf&

将338行 改成static-file.exclude-extensions = ( ".php", ".pl", ".fcgi", ".scgi",".cgi"
)即在括弧内最后加上".cgi" 

kwrite
/etc/lighttpd/conf.d/cgi.conf&

将16行改成
".cgi" => "",表示以.cgi后缀结尾的CGI程序不需要指定另外的程序来解析便可直接执行,保存退出,

 service lighttpd restart //重启一下服务

cd /var/www/lighttpd 将index.html 内容改成如下

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Powered by lighttpd</title>
<link rel="shortcut icon" href="favicon.ico" />
<link rel="icon" href="favicon.ico" />
<style type="text/css">
<!--
body { background: white; color: #666f85; text-align: center }
img  { border: none }
-->
</style>
</head>
<body>

<form action="/cgi-bin/login.cgi" method="POST">
<p>
用户名:<input name="name"><br>
</p>
<p>
密  码:<input name="password" type="password"><br>
<input value="确定" type="SUBMIT">
</p>
</form>

</body>
</html>

pwd

/var/www/lighttpd

mkdir cgi-bin

cd cgi-bin

vi login.c //login.c代码如下

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *str_len=NULL;
int len=0;
char buf[100]="";
char user[20]="";
char passwd[20]="";

printf("%s\r\n\r\n","Content-Type:text/html");
printf("<html>\n<head>\n");
printf("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">");
printf("<title>CGI3:登录结果</title></head><br>\n");
str_len = getenv("CONTENT_LENGTH");
if( (str_len==NULL)  || (sscanf(str_len, "%d", &len)!=1) || (len>80) )
printf("sorry!error!");
fgets(buf, len+1, stdin);
sscanf(buf, "name=%[^&]&password=%s", user,passwd);
if( (strncmp(user,"root",4)==0) && (strncmp(passwd, "111111", 6)==0) )
{
printf("<h1>Welcome! 登录成功!<h1>");
}

else
printf("<h1>Sorry! 用户名或密码错误!");
return 0;
}

gcc login.c -o login.cgi

ok! 打开浏览器输入本机IP试试,如果弹出一个简单的登录页面,那么恭喜你!

以后要写更复杂的cgi程序,只需修改cgi-bin 内的文件,然后再重新编译生成.cgi后缀的文件即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息