← All posts

Nginx. Using PHP in FastCGI Mode with spawn-fcgi

php-fastcgi

In my previous article on FastCGI, Nginx. Using PHP in FastCGI Mode with php-fpm, I explained how to use the php-fpm spawner (I should mention that a lot has changed since then: for example, PHP 5.3.3 was released, php-fpm became part of the PHP distribution, and the php-fpm configuration format changed). Today I’ll explain how to use spawn-fcgi, which was once part of the lighttpd distribution but is now a standalone project.

Installation

Before installing the spawner, let’s make sure PHP was built with CGI support:


[X] CGI        Build CGI version

Next, go to the ports tree and install spawn-fcgi:


cd /usr/ports/www/spawn-fcgi
make install clean

Configuration

Edit “nginx.conf” and add an upstream:


upstream spawn-fcgi {
    server unix:/tmp/spawn-fcgi.sock;
}

Open “/etc/rc.conf” for editing and specify the necessary settings:


# При запуске системы загружать spawn-fcgi
spawn_fcgi_enable="YES"

# Пользователь от которой работать будет php-cgi
spawn_fcgi_username="www"

# Группа
spawn_fcgi_groupname="www"

# Путь до unix-сокета, именно его мы используем
spawn_fcgi_bindsocket="/tmp/spawn-fcgi.sock"

# Количество запущенных php-cgi процессов
spawn_fcgi_children="5"

# Кол-во запросов, после которых перезагружается процесс (для предотвращения утечек памяти)
spawn_fcgi_max_requests="1000"

You can find the full list of supported settings in “/usr/local/etc/rc.d/spawn-fcgi”. Here are some that I haven’t mentioned above:


# Путь до php-cgi приложения
spawn_fcgi_app="/usr/local/bin/php-cgi"

# Дополнительные аргументы для php-cgi (их можно узнать командой "php-cgi -h")
spawn_fcgi_app_args=""

# Вы можете указать путь до песочницы
spawn_fcgi_chroot_dir=""

# IP адрес, который будет слушать демон
spawn_fcgi_bindaddr="127.0.0.1"

# Соответственно порт
spawn_fcgi_bindport="9000"

# Путь до PID файла
spawn_fcgi_pidfile="/var/run/spawn-fcgi.pid"

# Права на файл unix-сокета
spawn_fcgi_bindsocket_mode="0777"

Starting Up

Start spawn-fcgi and restart nginx:


/usr/local/etc/rc.d/spawn-fcgi start
nginx -s reload

The version of spawn-fcgi used is 1.6.3

Comments are closed. Read the original discussion in Russian →