Nginx + php-fpm in CentOS 6

Posted: July 8, 2012 in Linux, PHP, Web Server
Tags: , , , ,

Install php-fpm

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features (mostly) useful for heavy-loaded sites. The traditional way was using spawn-fcgi from lighttpd project.

php-fpm is not included in CentOS6 default repository, to add it, we can install Remi repository.

rpm -Uvh
yum --enablerepo=remi install php php-fpm php-common php-mysql php-pdo php-pecl-apc php-cli php-mcrypt php-xml php-gd php-mbstring

Install Nginx

Nginx is not included in CentOS6 default repository, to add it, create file /etc/yum.repos.d/nginx.repo and add the following content.

name=nginx repo

After that: yum -y install nginx


By default, php-fpm is a daemon process that runs a FastCGI server on port 9000, the init script is in /etc/init.d/php-fpm. We can configure it to listen to a unix socket instead of TCP sockets, while both are supported by Nginx.

vi /etc/php-fpm.d/www.conf

#listen =
listen = /var/run/php-fpm/php-fpm.sock

... ...
# change user to nginx instead of apache
#user = apache
user = nginx
#group = apache
group = nginx

vi /etc/nginx/conf.d/default.conf

# pass the PHP scripts to FastCGI server listening on or unix socket
location ~ \.php$ {
      #include /etc/nginx/fastcgi.conf;
      fastcgi_split_path_info  ^(.+\.php)(.*)$;
      fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
      fastcgi_index  index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      #fastcgi_param  SCRIPT_FILENAME  $request_filename;
      include fastcgi_params;

If you receive the error ‘File not found’, double check the fastcgi_param SCRIPT_FILENAME line. Make sure the correct php file is passed to FastCGI.

Start the service

service nginx start
service php-fpm start
  1. Dregs says:

    Why does it install httpd (apache) when I try to install PHP-fpm?

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s