How to specify a custom PHP-FPM user and group in Nginx?

To specify a custom PHP-FPM user and group in Nginx, you need to make changes to the Nginx configuration file. Here are the steps to follow:

  1. Open your Nginx configuration file. By default, it is located at /etc/nginx/nginx.conf or /etc/nginx/sites-available/default.

  2. Find the location ~ \.php$ block in your Nginx configuration file. This block handles PHP files.

  3. Add the fastcgi_param directives to set the PHP_VALUE for the desired user and group. Add the following lines to the location ~ \.php$ block:

    fastcgi_param PHP_VALUE "user=<username>"; fastcgi_param PHP_VALUE "group=<groupname>";

    Replace <username> and <groupname> with your desired PHP-FPM user and group.

  4. Save the configuration file and exit the editor.

  5. Restart Nginx to apply the changes:

    sudo systemctl restart nginx

With these changes, Nginx will pass the specified PHP-FPM user and group through the PHP_VALUE environment variable to PHP-FPM. PHP-FPM will then execute the PHP scripts with the specified user and group.