修改httpd.conf
找到第一个<Directory />的地方,如下:
需要注意的是,一定要修改第一次出现的地方,修改后面的位置,一直报403错误。
<Directory />
AllowOverride none
Require all denied
</Directory>
修改这一段成下:
<Directory "C:/xampp/cgi-bin"> AllowOverride None Order allow,deny Allow from all Options ExecCGI </Directory> AddHandler cgi-script .exe .pl .cgi .py
cgi-bin目录一般是在所安装的Apache目录下,但是本人安装的是XAMPP,所以是这个目录。
当然,实际上可以由自己指定的。
此外,还要修改ScriptAlias
貌似在370多行,修改的路径和上面一致即可。
看起来是必须的,但是实际上,这个可能需要读者自行测试了。
以下为第一个程序:test.cpp
#include <iostream> using namespace std; int main () { cout << "Content-type:text/html "; cout << "<html> "; cout << "<head> "; cout << "<meta charset="utf-8">"; cout << "<title>Hello World </title> "; cout << "</head> "; cout << "<body> "; cout << "<h2>Hello World! </h2> "; cout << "</body> "; cout << "</html> "; return 0; }
直接 g++ test.cpp -o test.exe,生成可执行文件。
然后启动apache,输入路径,访问即可。
或者g++ test.cpp -o test.cgi
至于python:test.py
#!"C:Python39python.exe" print("Content-type:text/html ") print("<html> ") print("<head> ") print("<meta charset="utf-8">") print("<title>Hello World </title> ") print("</head> ") print("<body> ") print("<h2>Hello World! </h2> ") print("</body> ") print("</html> ")
和cgi-bin目录中已有的cgi.cgi类似。第一行需要填写解释器的路径。