How To Make php-fpm Listen On Both Tcp And Unix Socket?

I need to make php-fpm listeing on both tcp/unix socket, and this is how it done.
(this was not pretty workarround i guess, but it work 😀 )
first  download php rpm source

$ wget http://centos.alt.ru/pub/php-fpm/5.3.3-2/SRPMS/php-5.3.3-2.el5.src.rpm

Compile and install

$ rpmbuild --rebuild php-5.3.3-2.el5.src.rpm
$ sudo rpm -Uvh /path/to/RPMS/php-*

Configuring the default php-fpm for using tcp socket
Edit www.conf

$ sudo vi /etc/php-fpm.d/www.conf

Find line containing

listen = 127.0.0.1:9000

We can make it listening to port what ever we desire, ie 9001 etc
Start php-fpm first instance

$ sudo service php-fpm start

Configuring php-fpm for using unix socket

$ sudo cp /etc/php-fpm.conf /etc/php-fpm2.conf
$ sudo cp -rp /etc/php-fpm.d /etc/php-fpm2.d

Edit /etc/php-fpm2.conf

include=/etc/php-fpm2.d/*.conf
pid = /var/run/php-fpm/php-fpm2.pid
error_log = /var/log/php-fpm/error2.log

Edit /etc/php-fpm2.d/www.conf

listen = /tmp/php-fpm.sock
php_admin_value[error_log] = /var/log/php-fpm/www-error2.log


Copy The start up script in /etc/init.d/

$ sudo cp /etc/init.d/php-fpm /etc/php-fpm2

Make a view changes on php-fpm2 init script

#! /bin/sh
#
# chkconfig: - 84 16
# description:  PHP FastCGI Process Manager
# processname: php-fpm
# config: /etc/php-fpm.conf
# pidfile: /var/run/php-fpm/php-fpm.pid

# Standard LSB functions
#. /lib/lsb/init-functions

# Source function library.
. /etc/init.d/functions

# Check that networking is up.
. /etc/sysconfig/network

if [ "$NETWORKING" = "no" ]
then
        exit 0
fi

RETVAL=0
prog="php-fpm2"
pidfile=${PIDFILE-/var/run/php-fpm/php-fpm2.pid}
lockfile=${LOCKFILE-/var/lock/subsys/php-fpm2}

start () {
        echo -n $"Starting $prog: "
        daemon --pidfile ${pidfile} php-fpm2 --fpm-config /etc/php-fpm2.conf
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch ${lockfile}
}
stop () {
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} php-fpm2
        RETVAL=$?
        echo
        if [ $RETVAL -eq 0 ] ; then
                rm -f ${lockfile} ${pidfile}
        fi
}

restart () {
        stop
        start
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status -p ${pidfile} php-fpm2
        RETVAL=$?
        ;;
  restart|reload|force-reload)
        restart
        ;;
  condrestart|try-restart)
        [ -f ${lockfile} ] && restart || :
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart}"
        RETVAL=2
        ;;
esac

exit $RETVAL

Don’t forget to copy php-fpm binary in /usr/sbin/ driectory

$ sudo cp /usr/sbin/php-fpm /usr/sbin/php-fpm2

Start php-fpm2 unix socket instance

$ sudo service php-fpm2 start

Check it out if it’s successfuly listening on both tcp/unix socket

$ sudo netstat -pln | grep php-fpm

It’ll look like this

tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      1265/php-fpm
unix  2      [ ACC ]     STREAM     LISTENING     888664697 27700/php-fpm2      /tmp/php-fpm.sock

Finnaly, make all the work we’ve done get fixed

$ sudo chkconfig php-fpm on
$ sudo chkconfig php-fpm2 on

We can use it as backends for nginx upstream module

http {
    ......
    ......
    upstream backend  {
        fair;  # we can use upstream fair module here
        server   127.0.0.1:9000;
        server   unix:/tmp/php-fpm.sock;
    }
    ......
    ......
    server {
    location ~ .php$ {
        fastcgi_pass   backend;
        fastcgi_index  index.php;
        .......
        .......
        include fastcgi_params;
        }
    ......
    ......
    }
}

that’s it, i hope it work on you too 😀

3 Comments

  1. Commenter No.1

    Isn’t that effectively the same as just making a separate php-fpm pool?

    [www]
    listen = 127.0.0.1:9001

    [www2]
    listen = /tmp/php-fpm.sock

    • yes, you’re right, but i forgot why am i doing this, creating 2 instance instead of creating two pool in /etc/php-fpm.d/ 😀

    • well, i remember now, i was on the middle of experiment with php-fpm,run each instance on different CPU cores,change secheduler priority.
      it was easier for me to running php-fpm from separate instance(executable), rather than running one executable with many pool.

Leave a Reply

Your email address will not be published. Required fields are marked *