Skip to content

PHP-FPM and Nginx

July 3, 2012
tags: , ,

PHP-FPM is a FastCGI process manager for PHP. The PHP-FPM has additional features that can make it appealing for any crowd. To name a few:

  • Emergency restart in case of accidental opcode cache destruction
  • Enhancements to FastCGI, such as fastcgi_finish_request()
  • Ability to start workers with different uid/gid/chroot/environment and different php.ini (replaces safe_mode)
  • Adaptive process spawning

PHP-FPM has been available for quite some time, however, it is new to the PHP 5.3. There is a bit of configuration that you will need to do with PHP in order to get up and running with PHP-FPM. If you are afraid of compiling or not using your package managers builds, then you may want to stop reading now.

PHP-FPM Installation

FPM is now avaiable on PHP 5.3.x. To install php with fpm you need to enable it. How? Very simple, compile php as you allways do and add:

--enable-fpm

Copy the init script from php source:

cp  sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

Configure php-fpm.conf file: php/etc/php-fpm.conf

Nginx Installation

Compile Nginx.

Edit nginx file fastcgi_params and add:

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param  PATH_INFO $fastcgi_script_name;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

Add to this params. The values are examples. Change yours as you need.

fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

In the nginx virtual host you must add

location ~ \.php$ {
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /var/www/digitalstruct.com/public$fastcgi_script_name;
      fastcgi_param PATH_INFO $fastcgi_script_name;
      include /usr/local/nginx/conf/fastcgi_params;
}

Restart Nginx and it’s done.

Not do:

location ~ ^/index.php
{
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_param SCRIPT_FILENAME /var/www/default/pub$fastcgi_script_name;
      fastcgi_param PATH_INFO $fastcgi_script_name;
      include /usr/local/nginx/conf/fastcgi_params;
}
Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s