为我自己的 Web 应用程序设置 Lighttpd 和 FastCGI?

为我自己的 Web 应用程序设置 Lighttpd 和 FastCGI?

我正在运行 Gentoo Linux,并尝试让 Lighttpd+PHP+FastCGI 运行。我发现Lighttpd 没有 FastCGI 进程管理器。因此,您应该使用 spawn-fcgi。我已让 Lighttpd 和 PHP 运行。我的问题是我不知道如何设置 spawn-fcgi 和 lighttpd 来使用我的 Web 应用程序。

这是我已经安装的:

  • Lighttpd
  • PHP(带有CGIUSE 标志)
  • spawn-fcgi

我尝试与之通信的应用程序只是 fcgi 文档的示例tiny.c

#include <stdio.h>
#include <stdlib.h>
void main(void)
{
    int count = 0;
    printf("Content-type: text/html\r\n"
        "\r\n"
        "<title>CGI Hello!</title>"
        "<h1>CGI Hello!</h1>"
        "Request number %d running on host <i>%s</i>\n",
        ++count, getenv("SERVER_NAME"));
}

我已经将其编译并从控制台运行:

nick@blozup ~/zwave $ ./tiny.fcgi

Content-type: text/html <title>FastCGI Hello!</title>
<h1>FastCGI Hello!</h1>Request number 1 running on host <i>(null)</i>

请忍耐一下,我不知道自己在做什么……

首先,我尝试将编译好的文件tiny.fcgi放在 Web 目录中(cgi-bin具体来说,这不起作用,然后就放在根 Web 目录中)。我得到的最接近的结果是 403 禁止错误消息。我确保权限是宽松的,但没有成功。

在阅读更多内容后,我意识到我需要使用 spawn-fcgi。因此,我安装了它并符号链接了我的应用程序,复制了默认配置,并针对我的应用程序进行了编辑:

ln -s /etc/init.d/spawn-fcgi /etc/init.d/spawn-fcgi.fcgi
cp /etc/conf.d/spawn-fcgi /etc/conf.d/spawn-fcgi.fcgi

文件/etc/conf.d/spawn-fcgi.fcgi

# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/www-servers/spawn-fcgi/files/spawn-fcgi.confd,v 1.6 2009/09/28 08:38:02 bangert Exp $

# DO NOT MODIFY THIS FILE DIRECTLY! CREATE A COPY AND MODIFY THAT INSTEAD!

# The FCGI process can be made available through a filesystem socket or
# through a inet socket. One and only one of the two types must be choosen.
# Default is the inet socket.

# The filename specified by
# FCGI_SOCKET will be suffixed with a number for each child process, for
# example, fcgi.socket-1.
# Leave empty to use an IP socket (default). See below. Enabling this,
# disables the IP socket.
#
FCGI_SOCKET=/var/run/fcgi.socket

# When using FCGI_PORT, connections will only be accepted from the following
# address. The default is 127.0.0.1. Use 0.0.0.0 to bind to all addresses.
#
FCGI_ADDRESS=127.0.0.1

# The port specified by FCGI_PORT is the port used
# by the first child process. If this is set to 1234 then subsequent child
# processes will use 1235, 1236, etc.
#
#FCGI_PORT=1234

# The path to your FastCGI application. These sometimes carry the .fcgi
# extension but not always. For PHP, you should usually point this to
# /usr/bin/php-cgi.
#
#FCGI_PROGRAM=/usr/bin/php-cgi
FCGI_PROGRAM=/home/nick/zwave/tiny.fcgi

# The number of child processes to spawn. The default is 1.
#
FCGI_CHILDREN=1

# If you want to run your application inside a chroot then specify the
# directory here. Leave this blank otherwise.
#
FCGI_CHROOT=

# If you want to run your application from a specific directiory specify
# it here. Leave this blank otherwise.
#
FCGI_CHDIR=

# The user and group to run your application as. If you do not specify these,
# the application will be run as root:root.
#
FCGI_USER=
FCGI_GROUP=

# Additional options you might want to pass to spawn-fcgi
#
#FCGI_EXTRA_OPTIONS=

# If your application requires additional environment variables, you may
# specify them here. See PHP example below.
#
ALLOWED_ENV="PATH"

然后我启动了该进程,现在它正在运行:

blozup zwave # /etc/init.d/spawn-fcgi.fcgi start
Starting....                                                     [OK]
blozup zwave # ps aux | grep tiny root
5809  0.0  0.0   2236   504 ?        Ss   17:47   0:00 /home/nick/zwave/tiny.fcgi

并且已在以下位置创建套接字:/var/run/lighttpd/tiny.socket-1

怎么办?

我如何让 lighttpd 连接到这个?以及如何从 URL 访问它?

我知道 lighttpd 需要使用那个套接字,但是当我编辑文件时mod_fastcgi.conf

fastcgi.server += (".fcgi" =>
        ( "localhost" =>
            (
                "socket" => "/var/run/lighttpd/fcgi.socket-1",
                "bin-path" => "/usr/bin/cgi-fcgi",
                "max-procs" => 1
        ))
)

我收到此错误:

2012-06-13 18:50:18: (mod_fastcgi.c.1389) --- fastcgi spawning
port: 0
socket /var/run/lighttpd/fcgi.socket-1
current: 0 / 1
2012-06-13 18:50:18: (mod_fastcgi.c.1103) the fastcgi-backend /usr/bin/cgi-fcgi failed to start:
2012-06-13 18:50:18: (mod_fastcgi.c.1107) child exited with status 1 /usr/bin/cgi-fcgi
2012-06-13 18:50:18: (mod_fastcgi.c.1110) If you're trying to run your app as a FastCGI backend, make sure you're using the FastCGI-enabled version.
If this is PHP on Gentoo, add 'fastcgi' to the USE flags.
2012-06-13 18:50:18: (mod_fastcgi.c.1397) [ERROR]: spawning fcgi failed.

我不知道接下来该怎么做。我找不到太多关于如何设置 lighttpd web 服务器以生成除 PHP 之外的任何内容的信息。使用 Apache 这一切会不会容易得多?

答案1

好吧,bin-path我被这个设置难住了。我的应用程序现在位于:

/var/www/localhost/cgi-bin/tiny.fcgi

套接字创建于:

/var/run/lighttpd/tiny.socket-1('-1' 由 spawn-fcgi 创建,并且已对它进行 chmod 处理,因此 lighttpd 可以对它进行读写)

文件mod_fcgi.conf

server.modules += ("mod_fastcgi")

fastcgi.debug = 1

fastcgi.server = ( ".php" =>
    ((
        "socket" => "/var/run/lighttpd/lighttpd-fastcgi-php-" + PID + ".socket",
        "bin-path" => "/usr/bin/php-cgi",
    )), 
    "/cgi-bin/" =>
    ((
        "socket" => "/var/run/lighttpd/tiny.socket-1",
#       "bin-path" => "/home/nick/zwave/tiny.fcgi",
        "check-local" => "disable",
        "max-procs" => 1
    ))
)

现在它开始工作了!

因此,如果您调用自己的已有套接字的应用程序,则无需 bin-path。spawn-fcgi 会启动这些进程,而不是 Lighttpd。

相关内容