apache + cgi 搭建系统

apache的安装就此略过,网上一堆资料。配置cgi花了一个小时才搞清楚怎么回事。。。

一、配置cgi

1、加载cgi模块

LoadModule cgid_module modules/mod_cgid.so
2、配置cgi运行目录 
2.1  ScriptAlias /cgi-bin/ /usr/local/apache/cgi-bin/
2.2 配置目录cgi执行权限
<Directory  /usr/local/apache/cgi-bin/>
  Require all granted
  AllowOverride None
  AddHandler cgi-script .pl .cgi  
  Options +ExecCGI   
  Order allow,deny
 Allow from all
</Directory>

二、测试
http://locahost/cgi-bin/test-cgi打开该地址,测试自己的cgi程序。首先要声明一个点就是 test-cgi必须是可执行程序,注意权限。否则会出现如下错误:

三、应用
      编写c程序,输出结果。结果可以是文本、图片、声音等。我在输出图片的时候遇到了错误,
printf(“<img src = ”image/***.jpg">);
这个地方的image目录位于默认根目录/usr/local/apache/htdocs/中,这样写路径会遇到错误。。。改成/ image/***.jpg即可。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string>
#include <vector>
#include <time.h>
#include <set>

int
main (int argc, const char* argv[])
{
    printf ("Content-type: text/html\n\n");


  char *query = getenv ("QUERY_STRING");
  if(query==NULL) {
	 fprintf(stdout, "no query submitted.");
	 return -1;
  }
  //添加自己的应用
....
  return 0;
}



猜你喜欢

转载自blog.csdn.net/cxf7394373/article/details/22858969
CGI