How to use and install php-fpm with nginx on centos 7

How to use php-fpm with nginx ?

What is PHP-FPM and Why to use 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.

These features include:

Adaptive process spawning (NEW!)
Basic statistics (ala Apache’s mod_status) (NEW!)
Advanced process management with graceful stop/start
Ability to start workers with different uid/gid/chroot/environment and different php.ini (replaces safe_mode)
Stdout & stderr logging
Emergency restart in case of accidental opcode cache destruction
Accelerated upload support
Support for a “slowlog”
Enhancements to FastCGI, such as fastcgi_finish_request() – a special function to finish request & flush all data while continuing to do something time-consuming (video converting, stats processing, etc.)

NOTE :-

It was not designed with virtual hosting in mind (large amounts of pools) however it can be adapted for any usage model.

MORE INOFRMATION – https://php-fpm.org/

How to install PHP-FPM ?

First we need to install php and then we have to install php-fpm, follow below command to install

#Centos 7

yum install php

yum install php-fpm

#Centos 8

[[email protected] home]# dnf install php

[[email protected] home]# dnf install php-fpm

Now check whether it is installed properly by running below commands

[[email protected] home]# php -v

[[email protected] home]# php-fpm -v

If these commands are working then it means php and php-fpm are installed properly.

Now lets configure php-fpm to run on Unix socket

[[email protected] home]# cd /etc/php-fpm.d

Edit www.conf file, make few changes

[[email protected] home]# vim www.conf

listen = /var/run/php-fpm/www.sock (this is for centos 7)

listen = /run/php-fpm/www.sock (this is for  centos 8)
user = nginx
group = nginx

Now we have to create www.sock file and change it’s ownership
#centos 7

[[email protected] home]# chown nginx:nginx /var/run/php-fpm/www.sock

#centos 8

[[email protected] home]# chown nginx:nginx /run/php-fpm/www.sock



Now, we have to configure socket for nginx as well.

Create conf file or add location for socket

[[email protected] home]# vim /etc/nginx/conf.d/test.conf

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
#root /usr/share/nginx/html;
#root /var/www/html;
index index.php index.html;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
error_log /var/log/nginx/default_error.log;
root /var/www/html;
location ~ \.php$ {
                  try_files $uri =404;
                  fastcgi_pass unix:/run/php-fpm/www.sock;
                  fastcgi_index index.php;
                  fastcgi_param SCRIPT_FILENAME                         $document_root$fastcgi_script_name;
include fastcgi_params;
}
}



let’s test php-fpm with nginx web server

[[email protected] home]# systemctl restart php-fpm
[[email protected] home]# systemctl restart nginx

Create phpinfo.php

[[email protected] home]# vim /var/www/html/phpinfo.php

 

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.