How to use CGI CGI Script?

I am currently participating in a web application class in my college and we are studying cgi scripts. I find it difficult to learn how to implement my CGI script. When I click on the link, a window appears asking me to download the helloworld.cgi file, and not just redirect it.

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;
    }

CGI script is stored in /user/local/apache2/cgi-bin/helloworld.cgi

+3
source share
4 answers

/user/local/apache2/cgi-bin/helloworld.cgi- The physical path of the file on your hard drive. To run the script through Apache, you need to specify the path relative to the root of the server document, for example. http://localhost/cgi-bin/helloworld.cgi.

+4
source

You need to compile the C ++ file and call the result of helloworld.cgi. C ++ is not a scripting language - you cannot just deploy it on your server.

* nix ++ helloworld.cpp

 gcc -o helloworld.cgi helloworld.cpp

cgi-bin

:

  cout << "Content-type: text/html" << endl << endl;
+9

You just need to configure Apache to recognize cgi-bincorrectly ...

Read the following: http://httpd.apache.org/docs/1.3/howto/cgi.html

In Apache config ScriptAlias, probably you want.

(I assume you compiled the helloworld.cgi binary)

+1
source

I also had this problem, and this solution worked for me:
First, run these commands on the 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 the href link to:

  <a href="/cgi-bin/helloworld.cgi">click me</a>
+1
source

Source: https://habr.com/ru/post/1788378/


All Articles