--- /opt/local/var/macports/sources/rsync.macports.org/release/ports/lang/php5/Portfile.orig	2011-10-18 00:04:19.000000000 +0400
+++ /opt/local/var/macports/sources/rsync.macports.org/release/ports/lang/php5/Portfile	2011-10-18 01:15:59.000000000 +0400
@@ -89,7 +89,8 @@
                         --with-libxml-dir=${prefix} \
                         --with-zlib=${prefix} \
                         --without-pear \
-                        --disable-cgi
+                        --disable-cgi \
+                        --disable-fpm
 
 # php5-mysql +mysqlnd needs mysqlnd support compiled into php
 configure.env-append    PHP_MYSQLND_ENABLED=yes
@@ -100,7 +101,7 @@
     configure.env-append LIBS=-lresolv
 }
 
-variant no_web conflicts apache apache2 fastcgi description {Don't include any web server support} {}
+variant no_web conflicts apache apache2 fastcgi fpm description {Don't include any web server support} {}
 
 # Build an Apache 1 module. On Mac OS X, it uses Apple's provided Apache 1 server.
 # On other platforms, the MacPorts apache port is used. Keep the options here
@@ -147,6 +148,29 @@
     }
 }
 
+# Build the FPM binary. Keep the options here in sync with those in the
+# relevant part of the post-destroot phase.
+variant fpm conflicts no_web description {Add FPM (Fast Process Management) web server binary} {
+    depends_lib-append      port:libevent
+    
+    set fpm_pidfile         ${prefix}/var/run/php-fpm.pid
+    
+    patchfiles-append       patch-fpm-conf.diff
+    
+    startupitem.create      yes
+    startupitem.name        php-fpm
+    startupitem.executable  ${prefix}/sbin/php-fpm --fpm-config ${phpinidir}/php-fpm.conf
+    startupitem.pidfile     auto ${fpm_pidfile}
+    
+    if { ![variant_isset apache] && ![variant_isset apache2] } {
+        configure.args-delete \
+            --disable-fpm
+        configure.args-append \
+            --enable-fpm
+    }
+}
+
+
 variant ipc description {Add semaphore, shared memory and IPC functions} {
     configure.args-append \
         --enable-shmop \
@@ -155,7 +179,7 @@
         --enable-sysvmsg
 }
 
-if {![variant_isset apache] && ![variant_isset apache2] && ![variant_isset fastcgi] && ![variant_isset no_web]} {
+if {![variant_isset apache] && ![variant_isset apache2] && ![variant_isset fastcgi] && ![variant_isset fpm] && ![variant_isset no_web]} {
     default_variants +apache2
 }
 
@@ -264,6 +288,44 @@
         # conflict with the cli version.
         xinstall -m 755 ${worksrcpath}/sapi/cgi/php-cgi ${destroot}${prefix}/bin
     }
+
+    if { [variant_isset fpm] } {
+        # If we've built an Apache module (any version) then the FPM binary
+        # will not have been built, so we need to run through the whole process
+        # again and build just the FPM binary. Keep the options here in sync
+        # with the options specified in the apache and apache2 variants.
+        # This shouldn't be necessary, but is done for http://bugs.php.net/bug.php?id=52419
+        if { [variant_isset apache] } {
+            if { ![variant_isset macosx] && (![info exists os.subplatform] || ${os.subplatform} != "macosx") } {
+                configure.args-delete \
+                    --with-apxs=${prefix}/sbin/apxs
+            } else {
+                configure.args-delete \
+                    --with-apxs=/usr/sbin/apxs
+            }
+        }
+        if { [variant_isset apache2] } {
+            configure.args-delete \
+                --with-apxs2=${prefix}/apache2/bin/apxs
+        }
+        
+        # Run the build again to get the FPM binary. Keep the options here
+        # in sync with those in the fpm variant.
+        if { [variant_isset apache] || [variant_isset apache2] } {
+            configure.args-delete \
+                --disable-fpm
+            configure.args-append \
+                --enable-fpm
+            ui_msg "$UI_PREFIX Configuring ${name} again for FPM"
+            command_exec configure
+            ui_msg "$UI_PREFIX Building ${name} again for FPM"
+            command_exec build
+            ui_msg "$UI_PREFIX Staging ${name} FPM into destroot"
+        }
+        
+        # Copy the FPM binary to the sbin dir.
+        xinstall -m 755 ${worksrcpath}/sapi/fpm/php-fpm ${destroot}${prefix}/sbin
+    }
     
     #file rename ${destroot}${prefix}/etc/pear.conf ${destroot}${prefix}/etc/pear.conf.sample
     
@@ -273,6 +335,11 @@
         php.ini-development \
         php.ini-production \
         ${destroot}${phpinidir}
+
+    # Copy FPM configuration file.
+    if {[variant_isset fpm]} {
+        xinstall -m 644 ${worksrcpath}/sapi/fpm/php-fpm.conf ${destroot}${phpinidir}/php-fpm.conf.default
+    }
     
     # Copy mysqlnd headers.
     xinstall -d ${destroot}${prefix}/include/php/ext/mysqlnd
@@ -303,6 +370,19 @@
         ui_msg "${phpinidir}/php.ini-development (if this is a development server) or"
         ui_msg "${phpinidir}/php.ini-production (if this is a production server)."
     }
+
+    if {[variant_isset fpm]} {
+        ui_msg ""
+        if {![file exists ${phpinidir}/php-fpm.conf]} {
+            ui_msg "To set up PHP FPM, copy"
+            ui_msg "${phpinidir}/php-fpm.conf.default to"
+            ui_msg "${phpinidir}/php-fpm.conf and then make changes."
+        } else {
+            ui_msg "You may need to update your php-fpm.conf for any changes that have been made"
+            ui_msg "in this version of php. Compare ${phpinidir}/php-fpm.conf.default with"
+            ui_msg "${phpinidir}/php-fpm.conf."
+        }
+    }
     
     if {![variant_isset no_web]} {
         ui_msg ""
