apache2+cgi

来源:互联网 发布:百分百软件 破解版 编辑:程序博客网 时间:2024/05/16 16:08

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即可。
[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <sys/types.h>  
  4. #include <sys/ipc.h>  
  5. #include <sys/shm.h>  
  6. #include <string>  
  7. #include <vector>  
  8. #include <time.h>  
  9. #include <set>  
  10.   
  11. int  
  12. main (int argc, const char* argv[])  
  13. {  
  14.     printf ("Content-type: text/html\n\n");  
  15.   
  16.   
  17.   char *query = getenv ("QUERY_STRING");  
  18.   if(query==NULL) {  
  19.      fprintf(stdout, "no query submitted.");  
  20.      return -1;  
  21.   }  
  22.   //添加自己的应用  
  23. ....  
  24.   return 0;  
  25. }  
0 0
原创粉丝点击