Installing PHP FPM for NginX with MacPorts on OSX

Install PHP with fastCGI

sudo port install php55 php55-cgi php55-fpm
sudo port install  php55-curl php55-gd php55-http php55-iconv php55-imagick php55-mcrypt php55-memcache php55-mysql php55-xdebug php55-openssl php5-zlib

note: we are installing php55-memcache not php55-memcached beacuse Wordpress W3-cache module doesn't support latter.

Create config files

cd /opt/local/etc/php55
sudo cp php.ini-development php.ini
sudo cp php-fpm.conf.default php-fpm.conf

Select php55 as default

sudo port select php php55

Loading and unloading php-fpm

sudo launchctl load -w /Library/LaunchDaemons/org.macports.php55-fpm.plist
sudo launchctl unload /Library/LaunchDaemons/org.macports.php55-fpm.plist


Run any of below commands to make sure PHP-FPM is listening on port 9000:

sudo lsof -Pni4 | grep LISTEN | grep php
sudo lsof -nP -iTCP:9000 -sTCP:LISTEN

Using memchased with w3cache info:

Installing memcached with mac-ports:

Configuring NginX with default settings (with TCP/IP on port 9000)

By default PHP-FPM listens on port 9000 for TCP/IP connections.

Example configuration

Update server_name and root directives.

server {
  listen 80;
  root /path/to/site/directory/;
  index               index.php index.html index.htm;

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

  location / {
      autoindex on;

Update /etc/hosts

Reload NginX

sudo port unload nginx && sudo port load nginx

Configuring NginX to run via socket


Open config file with: sudo vim /opt/local/etc/php55/php-fpm.conf
Uncomment following directives setting them to:

; Unix user/group of processes
user = www
group = www

; The address on which to accept FastCGI requests
listen = /tmp/php-fpm.sock

; Set permissions for unix socket
listen.owner = www = www
listen.mode = 0666


Open config file with: sudo vim /opt/local/etc/nginx/nginx.conf and add below line at the very top of the config.



Open your site nginx configuration file and replace line fastcgi_pass with location to php-fpm socket, as shown below.

  location ~ \.php$ {
    include fastcgi_params;    
    # fastcgi_pass;
    fastcgi_pass unix:/tmp/php-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

Reload NginX and PHP-FPM services

sudo port unload nginx && sudo port load nginx
sudo launchctl unload /Library/LaunchDaemons/org.macports.php55-fpm.plist
sudo launchctl load -w /Library/LaunchDaemons/org.macports.php55-fpm.plist

Now running below command should NOT return anything.

sudo lsof -nP -iTCP:9000 -sTCP:LISTEN

Add yourself to www group

sudo dseditgroup -o edit -a `whoami` -t user _www
sudo dseditgroup -o edit -a www -t user staff

Tweaking .bash_profile for handy aliases


It was already shown in previous post [todo: link] but in case you haven't seen it here it goes again.

# nginx start, stop, restart aliases
alias nginx-start="sudo launchctl load -w /Library/LaunchDaemons/org.macports.nginx.plist"
alias nginx-stop="sudo launchctl unload -w /Library/LaunchDaemons/org.macports.nginx.plist"
alias nginx-restart="nginx-stop; nginx-start;"


#php-fpm start, stop, restart aliases
alias php-fpm-start="sudo launchctl load -w /Library/LaunchDaemons/org.macports.php55-fpm.plist"
alias php-fpm-stop="sudo launchctl unload /Library/LaunchDaemons/org.macports.php55-fpm.plist"
alias php-fpm-restart="php-fpm-stop; php-fpm-start;"

Published by IndieForger