如何配置Apache以运行C++ CGI脚本?

4

我找到了很多关于Perl的东西,但是没有关于C++的。我现在正在使用Ubuntu,但是对它不太熟悉,所以简单的指示会很棒。我刚刚编写了一个小的C++程序,并将其制作成CGI,我需要测试一下。谢谢!

2个回答

5
你不需要做任何特殊的事情,只需将它编译为一个名为*.cgi的可执行文件(或其他文件名),并确保你拥有。
AddHandler cgi-script .cgi

在您的服务器配置或者.htaccess等文件中,加入或修改类似的内容。(免责声明:我已经很多年没做过这个了,所以可能会遗漏一些东西。但我认为这就是您需要的全部内容。)

但请注意:每个平台都有自己的依赖策略。例如,在Windows中,您必须将所依赖的DLL文件放置在系统目录或CGI的相同文件夹中(请使用http://www.dependencywalker.com进行检查),而对于其他平台,我会进行解释。 - softghost
谢谢,我搞定了!原来我需要在根目录下创建一个cgi-bin文件夹,并将其放在那里。这对于那些刚发现这个问题的人很有用。 - theeggman85

2

对于C++程序来说,没有什么特别之处。CGI程序是由Web服务器执行的。因此,它们的前提条件与Perl脚本/程序相同。

以下是一些提示,帮助您启动和运行C++ CGI程序。

  • 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, e.g.

    #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 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.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接