Now, mod_fastcgi is configured and running. FastCGI supports connection via UNIX sockets or TCP/IP networking. This is useful to spread load among various backends. For example, php will be severed from and python / ruby on rails will be severed from This is only possible with mod_fastcgi.

Required utilities

You can spawn FastCGI processes using a dispatcher script or using spawn-fcgi utility, which is used to spawn remote FastCGI processes. spawn-fcgi included with lighttpd web server. You can grab source code from or simply install it using lighttpd as follows (you need EPEL repo enabled under RHEL / CentOS / Fedora Linux):
# yum install lighttpd-fastcgi
# cp /usr/bin/spawn-fcgi /tmp
# yum remove lighttpd-fastcgi
# mv /tmp/spawn-fcgi /usr/bin/spawn-fcgi

lighttpd-fastcgi is FastCGI module and spawning helper for lighttpd and PHP configuration.

How do spawning php as TCP/IP remote app?

Use /usr/bin/spawn-fcgi as follows, enter:
# /usr/bin/spawn-fcgi -f /usr/bin/php-cgi -a -p 9000 -P /var/run/ -u apache -g apache
You can also jail php, using following syntax (make sure /var/run/ and /usr/bin/php-cgi exists inside jail directory):
# /usr/bin/spawn-fcgi -c /httpdjail -a -p 9000 -P /var/run/ -u apache -g apache -- /usr/bin/php-cgi

  • -f /usr/bin/php-cgi: Filename of the fcgi-application
  • -a : Bind to ip address
  • -p 9000 : Bind to tcp-port
  • -P /var/run/ Name of PID-file for spawed process
  • -c /httpdjail : Chroot to directory (security feature)
  • -u apache : Change to user-id (security feature – drop root user privileges to apache user)
  • -g apache : Change to group-id (security feature – drop root group privileges to apache group)

Configure Apache 2 mod_fastcgi connect to external PHP fcgi application

Above command will run php fcgi on Here is our sample setup:

  1. port 9000 : PHP FastCGI server
  2. port 9000 : Python or Ruby on rails cgi process
  3. port 80 : Apache 2 running mod_fastcgi (DocumentRoot set to /webroot/http)

Open your httpd.conf on, enter:
# vi /etc/httpd/conf/httpd.conf
Locate your domain VirtualHost configuration and append following two directives:

AddHandler php5-fastcgi .php
FastCgiExternalServer /webroot/http -host

Here is complete snippet from one my box:

    DocumentRoot /webroot/http
    ErrorLog logs/
    CustomLog logs/ common
    AddHandler php5-fastcgi .php
    FastCgiExternalServer /webroot/http -host

Save and close the file. Restart httpd:
# service httpd restart
Make sure iptables is configured to allow communication between public and private fastcgi server.

How do I configure PHP FastCGI via UNIX sockets?

UNIX sockets are faster as compare to TCP/IP sockets. However, they do not support remote spawning. Create /tmp/php.socket as follows:
# /usr/bin/spawn-fcgi -f /usr/bin/php-cgi -s /tmp/php.socket -u apache -g apache
Add following configuration to your httpd.conf virtual host:

AddHandler php5-fastcgi .php
FastCgiExternalServer /webroot/http -socket /tmp/php.socket

Save and close the file. Restart httpd, type:
# service httpd restart

mod_fastcgi has lots of other options. Please refer to Apache and mod_fastcgi documentation for further information.

A note about mod_fastcgi limitation

You can not load balance between multiple php backend. You need to use lighttpd or nginx or other reverse proxy software.

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

🐧 8 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
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
8 comments… add one
  • php kursu Jan 27, 2009 @ 15:28

    thank you

  • Mike Miller Feb 8, 2009 @ 12:10

    I tried this, and it worked perfectly for pages that exist. However, DirectoryIndex no longer functions, so type a url like no longer serves the page at !

    Any idea why?

  • 🐧 nixCraft Feb 8, 2009 @ 12:43

    Do you have DirectoryIndex configured?

  • Mike Miller Feb 8, 2009 @ 19:04

    Yes; if I take out the FastCgiExternalServer line, I get my php (source) displayed automatically (i.e., I hit and I get's source)

  • Reino Feb 24, 2009 @ 21:32

    Thanks, needed this!

  • Rafael Tinoco Feb 18, 2010 @ 5:52

    This is possible also with mod_proxy ;D

  • Silvio Siefke Feb 4, 2011 @ 13:54


    when i use fastcgi i have problems with the DirectoryIndex. For example, when i has activated fastcgi and go on my website i become 404, only when i give direct the index,php. When i delete the fastcgi Support in Apache then all running well. Has someone the same problem and find a way to fix it?


  • alexus Feb 24, 2012 @ 23:05

    i do too, wanted to know the answer for that as i’m also experiencing same issue.

    also (if that helps) here is another weird part…
    i don tknow when I go to instead of index I get “No input file specified.” in my browser.

    curl’s headers:

    HTTP/1.1 404 Not Found
    Date: Fri, 24 Feb 2012 23:04:00 GMT
    Server: Apache/2.2.21 (FreeBSD) mod_ssl/2.2.21 OpenSSL/0.9.8q DAV/2 mod_python/3.3.1 Python/2.7.2 mod_fastcgi/2.4.6
    X-Powered-By: PHP/5.3.8
    Content-Type: text/html

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