1. 先确认apache配置文件/etc/httpd/conf/httpd.conf中有加载cgi库
LoadModule cgi_module modules/mod_cgi.so
没有的话,用以下命令加载
/apache/bin/apxs -i -a -c /Apache-2.2.25/httpd-2.2.25/modules/generators/mod_cgi.c
2. 在httpd.conf加入下面内容
<VirtualHost *:80>
ServerName www.victortest.comDocumentRoot "/var/www"
<Directory /var/www>
Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
HostnameLookups off
<Files ~ "\.(html|cgi)$">
HostnameLookups on
</Files>
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory /var/www/cgi-bin/>
Options +ExecCGI
AddHandler cgi-script .cgi
AllowOverride None
Order Deny,Allow
Allow from all
</Directory>
</VirtualHost>
3. 重启apache server
/etc/init.d/httpd restart
4. 测试
#include <iostream>
using namespace std;
int main ()
{
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>\n";
cout << "<head>\n";
cout << "<title>Hello World - First CGI Program</title>\n";
cout << "</head>\n";
cout << "<body>\n";
cout << "<h2>Hello World! This is my first CGI program</h2>\n";
cout << "</body>\n";
cout << "</html>\n";
return 0;
}
将以上代码编译生成的hello.cgi放到/var/www/cgi-bin/ 目录下
在浏览器输入http://192.168.7.126/cgi-bin/hello.cgi