Wamp 下运行 CGI 笔记

时间:2022-07-23
本文章向大家介绍Wamp 下运行 CGI 笔记,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

httpd.conf 的配置

  我这里使用的是 Wamp 的 Apache 服务器,其配置在 C:wamp64binapacheapache2.4.33conf 目录下,然后修改配置如下:

LoadModule cgi_module modules/mod_cgi.so

ScriptAlias /cgi-bin/ "${INSTALL_DIR}/cgi-bin/"

#
# "c:/Apache24/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "${INSTALL_DIR}/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
    Options ExecCGI
    Order allow,deny
    Allow from all
</Directory>

AddHandler cgi-script .cgi

  上面的修改一共是四个部分,并没有在一起,查找一下即可。修改完配置以后记得重启一下服务器。

C 测试代码

  修改完 Apache 的配置以后来一段简单的 C 的代码,代码如下:

#include <stdio.h>
 
void main()
{
    printf("n");
    printf("<html><head><title>test</title></head><body>");
    printf("<a href="www.baidu.com">test</a>rn");
    printf("C CGI Test");
    printf("</body></html>");
}

  代码很简单,我使用的是 VS 的 IDE,因此使用 cl 在命令行下编译一下即可。将编译完的 exe 文件修改为 cgi 的扩展名,放入 CGI 的目录下(我这里是 C:wamp64cgi-bin),然后在浏览器中输入地址 http://localhost/cgi-bin/test.cgi 即可。

运行效果

  具体的运行效果就不看了,看一下浏览器中“查看源码”的功能吧,源码如下:

<html><head><title>test</title></head><body><a href="www.baidu.com">test</a>
C CGI Test</body></html>

效果就是这样了。

另外,做一个有节操的人,把参考的两个连接给出,并在此感谢。

Apache Web 服务器配置CGI程序,执行Python、Perl脚本 - 哎咿呀 - CSDN博客
https://blog.csdn.net/sotower/article/details/41014767

Windows下配置Apache运行C语言的CGI脚本 - u013005924的博客 - CSDN博客
https://blog.csdn.net/u013005924/article/details/52021152