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

How to use php-fpm with nginx ?

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

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.

PHP-FPM 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

[root@cpanel home]# dnf install php

[root@cpanel home]# dnf install php-fpm

Now check whether it is installed properly by running below commands

[root@cpanel home]# php -v

[root@cpanel 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

[root@cpanel home]# cd /etc/php-fpm.d

Edit www.conf file, make few changes

[root@cpanel 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
listen.owner = root
listen.group = nginx
listen.mode = 0660

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

[root@cpanel home]# chown nginx:nginx /var/run/php-fpm/www.sock

#centos 8

[root@cpanel 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

[root@cpanel 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

[root@cpanel home]# systemctl restart php-fpm
[root@cpanel home]# systemctl restart nginx

Create phpinfo.php

[root@cpanel home]# vim /var/www/html/phpinfo.php
<?php phpinfo(); ?>

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

techouse

I love helping beautiful people like you. I love hanging out with my dogs.

You may also like...

Leave a Reply

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