简体   繁体   中英

How to config apache for C++ CGI scripts?

I've found a lot of stuff for Perl, but nothing for C++. I'm running Ubuntu right now but I'm fairly inexperienced with it, so simple instructions would be awesome. I've just written a small C++ program made to work as a CGI, and I need to test it out. Thanks!

You shouldn't need to do anything special; just compile it to an executable file named *.cgi (or whatnot), and make sure you have

AddHandler cgi-script .cgi

(or whatnot) in your server config or .htaccess or whatnot.

(Disclaimer: It's been many years since I've done that, so I may be forgetting something. But I think that should be all you need.)

There is nothing special for C++ programs. CGI programs are executed by the Web Server. So the prerequisites are the same as for the Perl Scripts/programs, too.

Here are some hints getting a C++ CGI program up and running.

  • Put a simple executable in the cgi-bin directory and make it executable by the web server. Prepare a simple program returning a valid CGI response, eg

     #include <iostream> using namespace std; int main() { cout << "Content-Type: text/html" << endl << endl; cout << "Hello to Apache and Firefox!" << endl; return 0; } 
  • Execute the program on the command line and check that the output starts with the following lines. Ensure the empty line after the Content-Type.

     $ ./hello_world Content-Type: text/html Hello to Apache and Firefox! 
  • Increase the LogLevel to debug and look into the web server error logs. Search the access log, the error log and the suexec.log. Look in the log from the virtual host you are using and in the main error.log, too. On my Ubuntu system the files are located in /var/log/apache2 and named access.log, error.log and suexec.log. One of my last problems was "directory is writable by others: ..."

  • When using DLLs ensure that the libraries are still available on the web server. Set the RPATH to point to a custom directory with the libraries. In this context ldd and objdump -x <executable> | grep RPATH objdump -x <executable> | grep RPATH are your friends.

  • (This tip from Apache documentation has not worked for me: Watch the input and output using the ScriptLog Directive from the mod_cgi module of Apache. ScriptLog should only be used on an Development Server. Further details are available from the mod_cgi page.)

  • More hints are shown in the article Debugging Apache Web Server Problems .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM