OSX 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/sessio
Step 3: Confirm Installation
At this point PHP-FPM is installed and should run. Try to start the service with the following:
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.