December 6, 2012

Install PHP-FPM on OSX 10.8 Mountain Lion

php-logo

php-logoOSX 10.8 comes pre-installed with PHP and PHP-FPM. PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. PHP-FPM is not a web server, but rather is used in combination with a web server such as Apache and Ngnix to handle PHP requests.

Since PHP-FPM is pre-installed on OSX 10.8, you only need to configuration it. This example will install PHP-FPM using the default setup so that the service is listening on port 9000.

Step 1: Create the working directories

mkdir -p /usr/share/php/var/run
mkdir -p /usr/share/php/var/log
mkdir -p /usr/share/php/var/session

Step 2: Copy and edit the conf file

cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf

Once copied, edit /private/etc/php-fpm.conf. PHP-FPM will initially be run manually from the command line. Once everything is confirmed to be running you can optionally configure it to run as a daemon at startup.

; process and log file paths
pid = /usr/share/php/var/run/php-fpm.pid
error_log = /usr/share/php/var/log/php-fpm.log

; this example will require running php-fpm manually from the command line
daemonize = no

; optionally change listener from port to socket (will require tweaking nginx.conf as well)
listen = 127.0.0.1:9000
; listen = /tmp/php-fpm.sock

; configure the path to save session files
php_admin_value[session.save_path] = /usr/share/php/var/session

Step 3: Confirm Installation

At this point PHP-FPM is installed and should run.  Try to start the service with the following:

/usr/sbin/php-fpm

You should see the output “ready to handle connections.”  If there are any permission errors then you may need to change file permissions on the working folders created in step 1.  You can verify that the service is running with the following line:

ps -e | grep php-fpm

Running this line should display 3 running instances (ignore the grep command itself which will also appear in the list)

 (Optional) Step 4: Install as a Daemon

If you want PHP-FPM to start automatically when the computer starts, you can create a launch file.  If you started PHP-FPM manually and it is still running, be sure to CTRL+c to stop the service first.

Change the daemonize setting in /private/etc/php-fpm.conf:

daemonize = yes

Create a new daemon plist file for launching php-fpm at startup (thanks to group7even):

sudo touch /Library/LaunchDaemons/net.php.php-fpm.plist

Using the text editor of your preference, paste the following into net.php.php-fpm.plist:

<!--?xml version="1.0" encoding="UTF-8"?-->
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
 <key>KeepAlive</key>
 <true/>
 <key>Label</key>
 <string>net.php.php-fpm</string>
 <key>LaunchOnlyOnce</key>
 <true/>
 <key>NetworkState</key>
 <true/>
 <key>ProgramArguments</key>
 <array>
  <string>/usr/sbin/php-fpm</string>
 </array>
 <key>RunAtLoad</key>
 <true/>
 <key>ServiceDescription</key>
 <string>PHP FastCGI Process Manager</string>
 <key>StandardErrorPath</key>
 <string>/var/log/system.log</string>
</dict>
</plist>

Once the file has been saved, the service can be started with the command:

sudo launchctl load -F /Library/LaunchDaemons/net.php.php-fpm.plist

If all goes well you should see no output from the command.  You can confirm that the service is running using the grep command from step 3.

Troubleshooting Note: Once you configure your web server to use PHP-FPM you may see warnings in the browser about permission errors with the session path.  If this happens, you may need to adjust the permissions on all files and folders inside /usr/share/php/var so that the daemon can read/write to that path.

8 Comments on “Install PHP-FPM on OSX 10.8 Mountain Lion

Kakawait
January 31, 2013 at 7:17 am

When I’m using sudo launchctl unload -w/-F /Library/LaunchDaemons/net.php.php-fpm.plist

I have this error:
launchctl: Error unloading: net.php.php-fpm

But the load works … ? Any advice ?

Reply
Jason
March 7, 2013 at 12:47 am

sounds like possibly you launched it manually first, then tried to launch it as a daemon while it was still running..? if that’s the case just killing the process then launching it again should work just fine.

Reply
Newton Calegari
October 31, 2013 at 9:49 am

Hi Jason, great post, very useful!

Reply
tom
November 1, 2013 at 8:29 pm

Thanks for the tutorial. Should php-fpm be running along with apache or on its own?

Reply
Jason
November 1, 2013 at 9:34 pm

php-fpm is like an internal service pooling PHP resources, so you still need a web server like apache or ngnix running along with it to serve web pages.

Reply
Sven
February 21, 2014 at 9:20 pm

Hi Jason,

Can I ask you for some guidance on how to enable the php-fpm in apache?

Thanks in advance.
SD

Reply
FBe
September 18, 2015 at 4:04 am

Hi Jason,

Your /Library/LaunchDaemons/net.php.php-fpm.plist seems not to have a right xml body! Right?

When I launched, it returned:
/Library/LaunchDaemons/net.php.php-fpm.plist: Invalid property list

Some advices or corrections?

Reply
Jason
October 5, 2015 at 12:36 pm

ah thanks, i updated the post – wordpress editor seems to have stripped out all of the xml code

Reply

Leave a Reply

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