by Scott Schecter

Serve PHP Applications Using PHP-FPM


In this post I will demonstrate how to serve PHP applications using PHP-FPM running behind a Nginx web server. PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. If you are looking for the Ruby equivalent of this post check out my previous post where I demonstrated hot to serve Rack based Ruby applications behind Nginx using Phusion Passenger.

Unless we have application requirement that dictates otherwise our default stack for serving PHP applications currently at Schecter & Co. is using PHP-FPM behind Nginx. Since PHP-FPM is a stand alone process manager and not a Nginx module there is no need to compile Nginx with PHP-FPM support like there is with Phusion Passenger. If you are using FreeBSD as your web server operating system you will need to install the PHP-FPM port. If you are using another operating system for your web server you will need to check with your operating system’s package management system for the required PHP-FPM package to install. On most Linux distributions I have used PHP-FPM is a separate package that needs to be installed in addition to the base PHP package. After you have installed the PHP-FPM port you can start PHP-FPM by issuing the following command as a privileged user.

If you want to enable PHP-FPM at startup issue the following command as a privileged user.:

Below is a basic Nginx configuration file that demonstrates using PHP-FPM as an application server behind Nginx as a web server and reverse proxy on FreeBSD.

PHP-FPM itself requires no changes to the base Nginx configuration file when including site configurations from their own configuration files.

Nginx will serve as a web server and reverse proxy in this setup. Nginx will handle all requests and delegate PHP application processing to PHP-FPM as needed. We will use Nginx to serve and cache all static files for maximum efficiency. This setup has proven to be a very capable and stable platform for delivering production PHP web applications. Nginx itself is also very flexible and works with many different application servers. For example, you can also use Phusion Passenger behind Nginx to serve Rack based Ruby applications as needed. This includes sites using popular Ruby web frameworks such as Ruby on Rails and Sinatra.

You will notice that instead of embedding the configuration for each site in the main Nginx configuration file itself I include a separate folder which contains symbolic links to the actual per site configuration files. This keeps each sites configuration in it’s own file and makes administration of multiple sites much more manageable. For testing purposes you could include all configuration information within the main Nginx configuration file.

Next, Let’s examine what one of these site configuration files looks like for a PHP application.

In the site configuration within the server section you will set the server_name directive to whatever hostnames your site is available under. Set the root directive to the root of your PHP application. Set the index directive to your application’s index page. You will also want to tell Nginx to cache the static files here by matching folder location names. Within the root location section of the server section of the site configuration set the fastcgi_pass directive to the location of your PHP-FPM unix socket so Nginx knows to pass application processing to PHP-FPM.

You should now be able to start Nginx and serve your PHP applications by issuing the following command as a privileged user.

If you want to enable Nginx at startup issue the following command as a privileged user.

If you have any issues examine the Nginx error log for the cause. The location of this log file will depend upon your web server’s operating system. On FreeBSD it is found at /var/log/nginx-error.log. Please consult your operating system’s documentation for it’s location on your system. We have now covered a basic Nginx configuration which can be used to serve any PHP application.


Leave a Comment

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