I am currently enrolled in a web applications class at my college and we are learning about cgi scripts. I am having a hard time learning how to implement my CGI script. When I click on my link a window pops up asking me to download my helloworld.cgi file instead of just redirecting.
HTML:
<html>
<body>
<a href="/user/local/apache2/cgi-bin/helloworld.cgi">click me</a>
</body>
</html>
C++:
#include <iostream>
using namespace std;
int main(){
cout << "Content-type: text/html" << endl;
cout << "<html>" << endl;
cout << " <body>" << endl;
cout << " Hello World!" << endl;
cout << " </body>" << endl;
cout << "</html>" << endl;
return 0;
}
The CGI script is stored at /user/local/apache2/cgi-bin/helloworld.cgi
You need to compile the C++ file, and call the result helloworld.cgi. C++ is not a scripting language -- you can't just deploy it to your server.
On a typical *nix system, name the C++ file helloworld.cpp
gcc -o helloworld.cgi helloworld.cpp
Then put that file in your cgi-bin
Edit: you need two endl's after the last header item
cout << "Content-type: text/html" << endl << endl;
/user/local/apache2/cgi-bin/helloworld.cgi
is the physical path of the file on your hard disk. To run the script through Apache, you need to specify the path relative to your server's document root, for eg. http://localhost/cgi-bin/helloworld.cgi
.
I had this problem too, and this solution worked for me :
First run this commands on terminal:
sudo a2enmod cgi
sudo service apache2 restart
Then copy helloworld.cgi to /usr/lib/cgi-bin/
sudo cp helloworld.cgi /usr/lib/cgi-bin/
And finally change href link to:
<a href="/cgi-bin/helloworld.cgi">click me</a>
You just need to configure Apache to recognise a cgi-bin
properly...
Have a read of this: http://httpd.apache.org/docs/1.3/howto/cgi.html
In Apache config ScriptAlias
is probably what you want.
(I'm assuming you've compiled the binary to helloworld.cgi)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.