CGI在Apache上的配置

想写个前台页面发送ajax请求到后台用c语言编写的cgi程序,并成功返回结果。源码都很简单,都有,结果在调试配置时遇到了一个又一个问题。特记录之。

过程参考:http://blog.sina.com.cn/s/blog_5dd73f550100sc6t.html

1、先贴代码:ajaxtest1.htm

<html>
<head>
<script type="text/javascript">

function FuncA(str)
{
var xmlhttp;
if(str.length==0)
 
  document.getElementById("txtIDA").innerHTML="";
  return;
  }
if(window.XMLHttpRequest)
  {
  //code for IE7+,Firefox,Chrome,Opera,Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  //code for IE6,IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if(xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtIDA").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","/ajaxtest1cgi/ajaxtest1.cgi?txtIDA="+str,true);
xmlhttp.send();
}

function FuncB(str)
{
var xmlhttp;
if(str.length==0)
 
  document.getElementById("txtIDB").innerHTML="";
  return;
  }
if(window.XMLHttpRequest)
  {
  //code for IE7+,Firefox,Chrome,Opera,Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  //code for IE6,IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if(xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtIDB").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","/ajaxtest1cgi/ajaxtest1.cgi?txtIDB="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<h3>send data to background CGI through ajax:</h3>
<h3>background send data to front:</h3>
<form action=""> 
name:<input type="text" id="txt1" onkeyup="FuncA(this.value)" />
age:<input type="text" id="txt2" onkeyup="FuncB(this.value)" />
</form>
<p>suggest:<span id="txtIDA"></span></p> 
<p>instruction:<span id="txtIDB"></span></p> 

</body>
</html>


cgi_main.c

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

int main(void) 
{
char *lenstr;

if(lenstr=getenv("QUERY_STRING"))
{
}
else
{
}

    printf("Content-type: text/html;Charset=utf-8\n\n");

if(strstr(lenstr,"txtIDA")!=NULL)
{
   printf("xianxiangdong\n\n");
}
if(strstr(lenstr,"txtIDB")!=NULL)
{
   printf("i am 27 years old\n\n");
}

return 0; 

2、用dev-c++编译生成cgi执行文件。

之前我用ubuntu上的gcc编译生成cgi,结果访问后显示乱码,或者显示下载该cgi文件。懵逼了,显示下载是因为我的apache服务器没有配置对cgi文件的执行的支持,需要在apache配置文件中进行配置,详见http://blog.****.net/naturebe/article/details/7443662。主要就三步:LoadModule cgi_module modules/mod_cgi.so,Options Indexes FollowSymLinks MultiViews ExecCGI,AddHandler cgi-script .cgi .pl .py .sh。网上有说我apache版本高了,要回到旧版本的,胡说八道。

显示乱码是因为我是在ubuntu中用gcc编译的,然后想放在windows10上跑,当然跑不过啦。在apache中的日志文件中看错误是这样的:gcc: Internal error: Aborted (program collect2)

[Wed May 24 14:51:10.505032 2017] [win32:error] [pid 1840:tid 1136] [client 127.0.0.1:63408] AH02102: E:/Program Files/Apache24/htdocs/ajaxtest1.cgi is not executable; ensure interpreted scripts have "#!" or "'!" first line
[Wed May 24 14:51:10.505032 2017] [cgi:error] [pid 1840:tid 1136] (9)Bad file descriptor: [client 127.0.0.1:63408] AH01222: don't know how to spawn child process: E:/Program Files/Apache24/htdocs/ajaxtest1.cgi。所以需要在windows10上用gcc编译生成cgi。然后我下载安装了dev-c++,一定要是Dev-Cpp 5.3.0.3 TDM-GCC x64版本的,4.9.9.2版本的不支持64位。链接:https://down.bccn.net/4195.html。配置环境变量,用gcc命令编译,参见:http://bbs.chinaunix.net/thread-768151-1-1.html。

3、文件目录:

CGI在Apache上的配置CGI在Apache上的配置

其中ajaxtest1.htm是访问的页面,其中的逻辑处理ajax请求就是调用的上面ajaxtest1cgi文件夹中的.cgi程序,从而实现页面的动态加载。

ajaxtest1cgi文件夹如下图:

CGI在Apache上的配置

其中的ajaxtest1.cgi就是用dev-c++编译cgi_main.c得到的.cgi文件。

在cmd控制台中cd到E:\Program Files\Apache24\htdocs\ajaxtest1cgi目录下,然后输入命令:gcc -o ajaxtest1.cgi cgi_main.c,就可以编译生成.cgi文件了。

在ajaxtest1.htm中的xmlhttp.open("GET","/ajaxtest1cgi/ajaxtest1.cgi?txtIDB="+str,true);就是在ajax请求,并且请求的cgi文件路径都已经写好,所以能够调用到编译好的cgi文件了。

4、调试结果:

CGI在Apache上的配置CGI在Apache上的配置

5、参考资料:

1.http://blog.sina.com.cn/s/blog_5dd73f550100sc6t.html

2.http://blog.****.net/naturebe/article/details/7443662 //apache对cgi的支持配置

3.https://down.bccn.net/4195.html //dev-c++下载链接

4.http://bbs.chinaunix.net/thread-768151-1-1.html //gcc编译生成cgi文件,gcc环境变量配置