首先是需要的工具。我是在linux平台下安装的apache服务器,并使用的gcc编译器。当然你也可以在windows下安装使用,用IDE(如VC2008等)编译器。
安装apache服务器。下载好安装程序,在linux下是解压(tar -zxvf 压缩文件)后,cd到解压后的文件夹中,然后执行以下命令
sudo ./configure -prefix=/var/httpd/*选择安装路径*/
sudo make
sudo make install
一定不要忘记sudo,因为可能权限不够,造成安装失败。然后执行
sudo /var/httpd/bin/httpd -k start
启动apache服务器,如果下边的提示
httpd: Could not reliably determine the server's fully qualified domain name, using ::1 for ServerName
不用着急。到安装目录下找 conf文件夹下的httpd.conf文件,添加如下一行
ServerName 127.0.0.1:80/*注意读写权限*/
配置apache服务器。
ScriptAlias /cgi-bin/ "/var/httpd/cgi-bin/" /*此处引号中的路径为你所要准备存放cgi程序的目录*/
<Directory "/var/httpd/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>
AddHandler cgi-script .exe .pl .cgi
然后需要关闭服务器并重新开启
sudo /var/httpd/bin/httpd -k stop
sudo /var/httpd/bin/httpd -k start
好了,配置好了,可以在你的浏览器中输入地址127.0.0.1试试,看有什么回应!
编写简单cgi程序并测试。
include <stdio.h>
int main(void)
{
printf("Content-Type: text/html \n\n");
printf("<html><p>HELLO WORLD!</p><a href='http://www.baidu.com'>BAIDU</a></html>");
return 0;
}
gcc编译成可执行文件后,将可执行文件重命名成index.cgi。然后将cgi程序移到我们刚配置好的准备存放我们cgi程序的目录(/var/httpd/cgi-bin/)下,最后在浏览器地址栏输入
http://127.0.0.1/cgi-bin/index.cgi
大功告成!随意发挥自己想象改造世界吧!