gpt4 book ai didi

c++ - 如何为 C 脚本配置 lighttpd 和 fcgi?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:54:16 24 4
gpt4 key购买 nike

我已经实现了一个使用 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;
}

最佳答案

默认情况下,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

你必须制作一个symlinc并重启lighthttpd

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 = ( "" => "" )
}

关于c++ - 如何为 C 脚本配置 lighttpd 和 fcgi?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22535837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com