Server-side processing using php-fpm

PHP is a popular scripting language that is often used to create dynamic web pages. However, PHP can be resource-intensive, especially on high-traffic websites. This is where NGINX and php-fpm come in.

NGINX is a high-performance web server that can handle a lot of traffic. php-fpm is a PHP FastCGI implementation that can offload the processing of PHP scripts from NGINX. This can improve performance and reduce the load on the web server.

To use NGINX and php-fpm to process PHP scripts, you need to configure both servers.

Install NGINX and php-fpm

Here is the command to install both nginx and php-fpm on Ubuntu or Debian:

sudo apt install nginx php-fpm

Check if php-fpm is running

sudo systemctl list-units | grep php

Configuring NGINX

To configure NGINX to use php-fpm, you need to add the following lines to your nginx.conf file:

location / {
    fastcgi_pass unix:/var/run/php-fpm.sock;
}

This tells NGINX to pass all requests for PHP scripts to the php-fpm socket at /var/run/php-fpm.sock.

Configuring php-fpm

To configure php-fpm to listen on the Unix socket, you need to add the following lines to your php-fpm.conf file:

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

This tells php-fpm to listen on the Unix socket at /var/run/php-fpm.sock.

Once you have configured NGINX and php-fpm, you need to restart both servers.

To restart NGINX, you can run the following command:

sudo service nginx restart

To restart php-fpm, you can run the following command:

sudo service php-fpm restart

Once both servers are restarted, you can test your configuration by creating a simple PHP script and accessing it through your web browser.

The following is a simple PHP script that you can use to test your configuration:

<?php
echo "Hello, world!";
?>

Save this file as index.php in the root directory of your web server. Then, access it through your web browser by going to the following URL:

" http://localhost/index.php "

If you see the message "Hello, world!", then your configuration is working correctly.

Using NGINX and php-fpm together can improve the performance of your PHP applications by offloading the processing of PHP scripts from the web server. This can be especially beneficial for high-traffic websites.

Server Block with PHP processing

Look for these config:

  • index directive, index.php

  • location block

    • fastcgi is a protocol for transferring binary data

      • include /etc/nginx/fastcgi.conf
    • pass to a UNIX socket, a file that listens for binary data.

server {

    listen 80;
    server_name 167.99.93.26;

    root /sites/demo;

    index index.php index.html;

    location / {
      try_files $uri $uri/ =404;
    }

    location ~\.php$ {
      # Pass php requests to the php-fpm service (fastcgi)
      include fastcgi.conf;
      fastcgi_pass unix:/run/php/php7.1-fpm.sock;
    }

  }

Testing and Troubleshooting

You may test the PHP processing by loading a PHP info file:

echo '<?php phpinfo(); ?>' > info.php

Open the file in the browser using http://URL/info.php

502 Bad Gateway

First, check the error logs:

tail -n 1 /var/log/nginx/error.log

Error: Permission denied while connecting to php-fpm socket.

Check the user with which the NGINX process and the php-fpm process runs:

ps aux | grep nginx
ps aux | grep php

See that the nginx and php-fpm processes run as the same user.

Add the user directive to the NGINX config to match the user of the php-fpm process:

user www-data;

Reload NGINX and check again.

Additional resources

0
Subscribe to my newsletter

Read articles from Melvin C Varghese (melvincv) directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Melvin C Varghese (melvincv)
Melvin C Varghese (melvincv)

I am a DevOps Consultant. 3 years experience with Cloud technologies. 9 years total IT experience in the Linux, Networking and Data Visualization domains. Love listening to music and playing older PC games.