FreeBSD: Nginx FastCGI PHP Configuration

Q. How do I configure PHP as FastCGI under FreeBSD Nginx webserver?

A. You can easily configure php as FastCGI application under Nginx for performance. You need following components:

[a] spawn-fcgi binary – For spawning a FastCGI process. This binary can be installed from Lighttpd webserver project. Nginx does not automatically spawn FCGI processes. You must start them separately using spawn-fcgi.

[b] : A shell script to stop / start / restart php fastcgi process. This script swap php process on IP and 9000 port. Once backend is started Nginx can be configured to connect to PHP.

FreeBSD Install PHP5

If php5 is not installed type the following commands (make sure you select FastCGI option):
# cd /usr/ports/lang/php5
# make config
# make install clean

Fig.01 Enable FastCGI support by selecting FastCGI option

Fig.01 Enable FastCGI support by selecting FastCGI option

Also installed required php extensions such as php-msyql, php-gd and so on:
# cp /usr/local/etc/php.ini-recommended /usr/local/etc/php.ini
# cd /usr/ports/lang/php5-extensions/
# make install clean

Install spawn-fcgi

Grab latest spawn-fcgi from another Lighttpd installation or just install it as follows:
# pkg_add -r -v lighttpd
Sample output:

scheme:   [ftp]
user:     []
password: []
host:     []
port:     [0]
document: [/pub/FreeBSD/ports/amd64/packages-7.0-release/Latest/lighttpd.tbz]
looking up
connecting to
<<< 220 Welcome to
>>> USER anonymous
<<< 331 Please specify the password.
x lib/lighttpd/
x lib/lighttpd/mod_accesslog.a
x lib/lighttpd/
x lib/lighttpd/
x lib/lighttpd/mod_alias.a
x lib/lighttpd/
x lib/lighttpd/
x lib/lighttpd/mod_auth.a
x lib/lighttpd/

Now you can use /usr/local/bin/spawn-fcgi to swap process. If you want you can copy /usr/local/bin/spawn-fcgi to /root or other directory and just delete lighttpd package by typing the following commands:
# cp /usr/local/bin/spawn-fcgi /root/
# pkg_delete -v lighttpd-1.4.18_1
# cp /root/spawn-fcgi /usr/local/bin/spawn-fcgi

Now you can start PHP Fastcgi from command line as follows:
# /usr/local/bin/spawn-fcgi -a -p 9000 -u www -g www -f /usr/local/bin/php-cgi

  • -a : PHP FastCGI bind IP address, where Nginx will connect.
  • -p 9000: PHP FastCGI port number, where Nginx will connect.
  • -u www : PHP FastCGI username.
  • -g www : PHP FastCGI groupname.
  • -f /usr/local/bin/php-cgi : Path to PHP5 fastcgi binary.

Verify that php running as FastCGI, enter:
# sockstat -4 | grep 9000
Sample Output:

www      php-cgi    8998  0  tcp4    *:*
www      php-cgi    8997  0  tcp4    *:*
www      php-cgi    8996  0  tcp4    *:*
www      php-cgi    8995  0  tcp4    *:*
www      php-cgi    8994  0  tcp4    *:*
www      php-cgi    8993  0  tcp4    *:*

Sample Shell Script To Start / Stop PHP FastCGI process

# NGINX FastCGI php5 startup shell script
# Feedback <>
# Set ME #
### No editing below ####
  	echo "Starting $PROVIDES..."
	echo "Killing $PROVIDES..."
        $PGREP $PROVIDES > /dev/null
	[ $? -eq 0  ] && echo "$PROVIDES running" || echo "$PROVIDES NOT running" 
  	echo "Usage: $0 {start|stop|restart|status}"
case ${cmd} in
[Ss][Tt][Aa][Rr][Tt]) pcgi_start;;
[Ss][Tt][Oo][Pp]) pcgi_stop;;
[Rr][Ee][Ss][Tt][Aa][Rr][Tt]) pcgi_restart;;
[Ss][Tt][Aa][Tt][Uu][Ss]) pcgi_status ;;
*)      pcgi_help ;;

Install above shell script:
# cd /tmp
# fetch
# unzip
# mv /usr/local/etc/rc.d/
# chmod +x /usr/local/etc/rc.d/
# rm

To start php FastCGI, enter:
# /usr/local/etc/rc.d/ start
# sockstat -4 | less

To stop php FastCGI, enter:
# /usr/local/etc/rc.d/ stop

Connect Nginx Webserver To PHP FastCGI Process

Open your ngixn.conf file, enter:
# vi /usr/local/etc/nginx/nginx.conf
Append following config directives so that Nginx can pass the PHP scripts to FastCGI server listening on

location ~ \.php$ {
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /websites/$fastcgi_script_name;
            include        fastcgi_params;

If your website is hosted at /home/www/, your config should look like as follows:

server {
	listen  80;
        access_log  /var/log/nginx/  main;
        location / {
            root   /home/www/;
            index  index.php index.html index.htm;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/www/nginx-dist;
       # pass the PHP scripts to FastCGI server listening on
        location ~ \.php$ {
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/www/$fastcgi_script_name;
            include        fastcgi_params;
        location ~ /\.ht {
            deny  all;

Restart Nginx web server

# nginx -c /usr/local/etc/nginx/nginx.conf -t
# /usr/local/etc/rc.d/nginx restart

Test php with following sample script:


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 10 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
10 comments… add one
  • George Bonev Feb 24, 2009 @ 15:29

    Worked Like a charm / thx :)

  • Ed Mar 18, 2009 @ 16:50

    Thanks for the great tutorial. Long life for freebsd and nginx!

  • Leo Vandewoestijne May 8, 2009 @ 0:12

    Maybe the port wasn’t available by writing your article, or maybe I’m confusing things… …but the step “Install spawn-fcgi”, can that be done without any lighttpd? Like:
    pkg_add spawn-fcgi

  • symbol Jun 8, 2009 @ 3:52

    yea. and spawn_fcgi_enable=”YES” >> /etc/rc.conf seem to do the trick

    or am i missing something that would come back to haunt me sometime later

  • Bash Jun 11, 2009 @ 20:44
  • ksil Jun 27, 2009 @ 9:23

    Hi, thank you for useful post. Let me cite this article in my Japanese blog:

  • Dustin Sweigart Jul 3, 2009 @ 14:36

    I set this up and it works great, but under high loads (testing with apache’s ab) the spawn-fcgi/php-cgi process dies and so my server stops being able to load PHP. I know I’m testing with an unrealistic load on my server, but I also don’t want others to be able to so easily break my server.
    ab -n1000 -c200 http://websiteurl/

    Does anyone else have this problem, and if so, does anyone know how to fix it?

    • 🐧 nixCraft Jul 3, 2009 @ 15:12

      Do you get any specific error in a log file? Also, specify your server config

  • Dustin Sweigart Jul 3, 2009 @ 19:08

    my config:

    Normally it runs fine, you can check out if you’d like to see PHP running normally. But push the server really hard and whatever process exists after you call spawn-fcgi dies (as in it is no longer running)

    [error] 24813#0: *1032 recv() failed (104: Connection reset by peer) while reading response header from upstream, client:, server: localhost, request: "GET / HTTP/1.0", upstream: "fastcgi://", host: ""

    [error] 24813#0: *3964 connect() failed (111: Connection refused) while connecting to upstream, client:, server: localhost, request: "GET / HTTP/1.0", upstream: "fastcgi://", host: ""

  • Shatil Dec 26, 2010 @ 3:28

    Hi guys,

    FreeBSD’s ports selection (at least 8.1’s—I’ve been using it for four hours now) has PHP 5.3.4+ now, which means PHP-FPM comes standard as long as you compile it through ports. With that, you can do away with needing spawn-fcgi, lighttpd, and the other nonsense that used to be necessary to get PHP + Nginx working.

    Maybe you could post another post with that?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum