Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm running Gentoo Linux, and I'm trying to get Lighttpd+PHP+FastCGI working. I discovered that Lighttpd doesn't have a FastCGI process manager. So, you're supposed to use spawn-fcgi. I have Lighttpd and PHP working. My problem is that I do not know how to setup spawn-fcgi and lighttpd to use my web-app.

This is what I've installed:

  • Lighttpd
  • PHP (with CGI USE flag)
  • spawn-fcgi

The application I am trying to communicate with is just the fcgi docs' example, 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"));
}

I have it compiled and working from the console:

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>

Bear with me here, I have no idea what I'm doing...

First I tried just placing the compiled tiny.fcgi in a web directory (cgi-bin, specifically, which didn't work, then just in the root web directory). The closest I got was a 403 forbidden error message. I made sure the permissions were liberal, no luck.

After reading some more, I realized that I needed to use spawn-fcgi. So I installed it and symlinked my application, and copied the default config, and edited for my application:

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

File /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"

Then I started the process, and it is running now:

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

and a socket has been created at: /var/run/lighttpd/tiny.socket-1

Now What?

How do I get lighttpd to connect to this? and how do I access that from a URL?

I know lighttpd needs to use that socket, but when I edit the mod_fastcgi.conf file:

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

I get this error:

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.

I don't know where to go from here. I couldn't find much on how to setup the lighttpd web server to spawn anything but PHP. Would this all just be a lot easier with Apache?

share|improve this question
    
You need to speak the FCGI protocol to lighttpd over a unix socket. I don't see where this is happening here. –  Keith Jun 14 '12 at 2:40
    
Well, there is a socket at /var/run/lighttpd/tiny.socket-1, my FCGI application has created that. I just don't know how to get Lighttpd to "open" that socket. I figured it was in the config file and had something to do with cgi-fcgi. In mod_fcgi.conf I specified that same socket: "socket" => "/var/run/lighttpd/fcgi.socket-1", but either cgi-fcgi is the wrong program to call, or I'm calling it wrong. –  Nick Jun 14 '12 at 13:17
    
I've removed the bin-path line in mod_fastcgi.conf, keeping the socket, and the server starts. The only indication I get that it has opened that socket in the logs, is: "all handlers for /tiny.fcgi? on tiny.fcgi are down." Now I am getting 500/503 errors in lighttpd. –  Nick Jun 14 '12 at 14:36
add comment

1 Answer

up vote 1 down vote accepted

Well, it was the bin-path setting that I was getting hung up on. My application resides now at:

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

The socket is created at:

/var/run/lighttpd/tiny.socket-1 (the '-1' is created by spawn-fcgi, and it's chmod'd so lighttpd can r/w to it)

The mod_fcgi.conf file:

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
    ))
)

And now it's working!

So, no bin-path if you are calling your own apps that already have a socket. The spawn-fcgi starts those processes, not Lighttpd.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.