Loading...
Real Time Concepts

PHP-FPM and APC with nginx

Overview

As Nginx does not have an equivalent to Apache’s mod_php, we need to use PHP-FPM to handle PHP interpretation. Nginx is then configured to proxy requests matching *.php to the PHP-FPM socket. We’ll also be installing and configuring APC because it’s pretty crucial for performance. Let’s get started.

Installation

If you haven’t already installed Nginx, check out the directions , then proceed with installing PHP-FPM and APC: here

RHEL/CentOS 6.5 ( first ) Run the LAMP deployment recipes manually

# yum install php54-fpm
# yum install php54-pecl-apc ( only necessary if LAMP deployment was not ran )
mkdir /etc/monit.d.disabled ( not necessary on newly deployed boxes )
mv /etc/monit.d/apache.conf /etc/monit.d.disabled ( not necessary on newly deployed boxes )

If the above packages are not found, be sure that the has been configured on the server. IUS repo

Ubuntu 10.10+

Whoa there, buddy…
If you’re going to be using PHP-FPM in conjunction with Nginx, make sure the customer is running Ubuntu 10.10+ first. PHP-FPM wasn’t included in the core of PHP until 5.3.3 and 10.04 is still using 5.3.2.

# apt-get install php5-fpm php-apc
mkdir /etc/monit.d.disabled
mv /etc/monit.d/apache.conf /etc/monit.d.disabled

Configuration

PHP-FPM defaults to listening over TCP, while this technically works it has unnecessary overhead from the three-way handshake in TCP connections, instead we’ll modify it to use Unix sockets.

RHEL/CentOS

# vim /etc/php-fpm.d/www.conf

Modify the listen directive and place the sock somewhere sane:

[www]
listen = /var/run/php5-fpm.sock

Then start the service and set it to start on boot:

# service php-fpm start
# chkconfig php-fpm on

Ubuntu

# vim /etc/php5/fpm/pool.d/www.conf

Modify the listen directive and place the sock somewhere sane:

[www]
listen = /var/run/php5-fpm.sock

Start and enable on boot:

# /etc/init.d/php5-fpm start
# update-rc.d php5-fpm enable

Nginx Configuration

Nginx will need to know where to send PHP requests. To do this, first we’ll define an upstream:

In nginx.conf within the http{} block:

upstream php5-fpm-sock {
server unix:/var/run/php5-fpm.sock;
}

In the vhost, make a new location{} block:

location ~ \.php$ {
expires off;
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass php5-fpm-sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Note that you can change the fastcgi_pass value to be a different server, or servers, if you are wanting to get fancy.

Troubleshooting

If you see a page that says ‘The page you are looking for is currently unavailable’, you’ll find Nginx is having trouble getting data from php-fpm (while using a socket). Make sure that the socket you specify in the nginx.conf and the listen line in www.conf pool file for PHP-FPM match.

If you get a File not Found page on only php pages (when using port for php-fpm) you can check the nginx logs, and you’ll probably see an error indicating it couldn’t connect on the port.

If you get a File Not Found page on only php pages (generally) you may also need to move the root and index definitions out of a location block (if they are defined there). Because the php section has it’s own location, the documents won’t try to be found using definitions in the domain ‘location /’ block.

If you want to see the size of the current running php-fpm process here is a quick one liner:

ps -o ‘rss,cmd’ -C php-fpm –sort:rss | awk ‘{print $1}
Leave a Reply

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