What is 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. To know more about PHP-FPM, click this LINK.
Before you proceed with the installation, ensure that you have root privileges (sudo su -) and logged in to your CentOS 7 server.
Installing and Configuring PHP-FPM
On this guide, we will be installing php, php-mysql and php-fpm with it core dependencie. You may use the command below:
yum -y install php php-mysql php-fpm
Now that we have the PHP package and components installed, we will be configuring PHP to only accept URIs or files that exist on the server. This is to reduce the risk of getting affected by a security vulnerability in which the PHP interpreter can be deceived into allowing arbitrary code execution when a .php file is requested that doesn’t exist in the server’s filesystem. To know more about the vulnerability, see this
LINK.
sudo sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php.ini
OR
You may open the main php-fpm configuration and update the “cgi.fix_pathinfo” and set it to “0” as it is “1” by default.
vi /etc/php.ini
cgi.fix_pathinfo=0
Then save and close.
Next is to open the php-fpm configuration file “www.conf”:
vi /etc/php-fpm.d/www.conf
Look for the line that gives the “listen” parameter and update it as shown below:
listen = /var/run/php-fpm/php-fpm.sock
Then find the lines as shown below and uncomment them:
listen.owner = nobody
listen.group = nobody
And the last one should be the lines showing “user” and “group”, update the values and replace apache with nginx since you are using nginx web server:
user = nginx
group = nginx
Once done, you may save and close the file.
The PHP processor can now be started and enabled using the commands below:
systemctl start php-fpm
systemctl enable php-fpm
You may check the status of it by using:
systemctl status php-fpm
Setting Up NGINX To Process PHP Pages
We will be needing to set up NGINX to use our PHP processor. On this part, the update will be on the default NGINX server block configuration.
vi /etc/nginx/nginx.conf
The file should look like this one below:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name yourdomain.com_or_IP;
root /usr/share/nginx/html;
index index.php index.html index.htm;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Once done, just save and close the configuration file.
Finally, you need to restart the NGINX service using the command:
systemctl restart nginx
Testing the PHP processor on the NGINX Web Server
You can now create a test script named “info.php” that will show you all the information from the PHP configurations.
This file will be stored in “/usr/share/nginx/html” which is the default root of your NGINX Web Server.
vi /usr/share/nginx/html/info.php
This short script will be the content of your “info.php” file.
< ?php Phpinfo(); ?>
Once done, you can save and close he file.
To test it, open you browser and type in “
http://yourdomain.com/info.php" OR "yourIPaddress/info.php".