【问题标题】:How to config lighttpd and fcgi for c script?如何为 c 脚本配置 lighttpd 和 fcgi?
【发布时间】:2014-04-27 11:12:57
【问题描述】:

我已经实现了一个使用 lighttp 网络服务器和 fcgi(c 脚本) 的程序。 我已经搜索了很多次,但没有找到任何页面指南来做到这一点。只需设置 lighttpd 和 python fcgi 或 php fcgi... 但 C fcgi。 谁能帮我配置lighttpd和c fcgi? 非常感谢。

我已经编写了如下示例,并将其构建为可执行文件。但现在我不知道如何使用 lighttpd 网络服务器运行它。

#include "fcgi_stdio.h"
#include <stdlib.h>
#include <stdio.h>
int count;
using namespace std;
void initialize(void)
{
  count=0;
}

int main(void)
{
/* Initialization. */
  initialize();

/* Response loop. */
  while (FCGI_Accept() >= 0)   {
    printf("Content-type: text/html\r\n"
           "\r\n"
           "<title>FastCGI Hello! (C, fcgi_stdio library)</title>"
           "<h1>FastCGI Hello! (C, fcgi_stdio library)</h1>"
           "Request number %d running on host <i>%s</i>\n",
            ++count, getenv("SERVER_HOSTNAME"));
  }
  return 0;
}

【问题讨论】:

    标签: c++ c lighttpd fastcgi mod-fcgid


    【解决方案1】:

    默认情况下,lighthttp 只允许在 'cgi-bin' 目录中执行 cgi 脚本。 因此,只需将您的 cgi 程序放在“/cgi-bin”中,它就可以使用默认配置。

    这是 C 语言中的 cgi 示例

    #include <stdio.h>
    
    int main(void)
    {
       printf("Content-type: text/plain\n\n");
       puts("Hello from cgi-bin!...");
       return 0;
    }
    

    编译

    cc 1.c -o 1
    

    测试

    wget localhost:82/cgi-bin/1
    --2014-03-20 16:27:36--  http://localhost:82/cgi-bin/1
    Resolving localhost (localhost)... 127.0.0.1
    Connecting to localhost (localhost)|127.0.0.1|:82... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: unspecified [text/plain]
    Saving to: `1'
    
        [ <=>                                                                                                                              ] 21          --.-K/s   in 0s
    
    2014-03-20 16:27:37 (1.08 MB/s) - `1' saved [21]
    
    cat 1
    hello from C cgi!...
    

    更新

    默认情况下,这里有 cgi 的配置文件:

    /etc/lighttpd/conf-available/10-cgi.conf
    

    你必须创建一个符号并重新启动 lig​​hthttpd

    sudo ln -s /etc/lighttpd/conf-available/10-cgi.conf /etc/lighttpd/conf-enabled/10-cgi.conf
    

    这是 cgi 配置文件中必须包含的内容

    cat /etc/lighttpd/conf-available/10-cgi.conf
    
    server.modules += ( "mod_cgi" )
    
    $HTTP["url"] =~ "^/cgi-bin/" {
        cgi.assign = ( "" => "" )
    }
    

    【讨论】:

    • 我在 /var/www/lighttpd 中创建了一个 cgi-bin 目录。然后我编写了一个示例,它使用 fcgi 库来显示“hello world”字符串。我构建为可执行文件,然后将此文件复制到 /var/www/lighttpd/cgi/bin。然后我在浏览器上输入了“localhost/cgi-bin/test”。刚刚下载的二进制文件,但没有显示“hello world”。
    • 这里我只能使用 fcgi 而不是 cgi。在fastcgi.com页面下载。
    • 我更新了答案。检查您是否有 '/etc/lighttpd/conf-available/10-cgi.conf' 配置文件,并且包含 mod_cgi 服务器模块。
    • 我已经使用以下命令安装了 lighttpd 和 lighttpd-fcgi:sudo yum install lighttpd 和 sudo yum install lighttpd-fcgi。但我没有看到任何 /etc/lighttpd/conf-available/10-cgi.conf 文件。只有 /etc/lighttpd/lighttpd.conf 文件。
    • 好的。然后尝试将最后一个配置块添加到 lighttpd.conf 并重新启动它
    猜你喜欢
    • 2010-11-24
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 2015-06-22
    • 2014-09-30
    • 1970-01-01
    相关资源
    最近更新 更多