How to configure Nginx to use PHP-FPM environment variables for PHP settings?

To configure Nginx to use PHP-FPM environment variables for PHP settings, you can follow these steps:

  1. Open your Nginx configuration file using a text editor. In Ubuntu, the file is usually located at /etc/nginx/sites-available/default.

  2. Inside the server block, add the following location block to pass the PHP scripts to PHP-FPM:

    location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php<version>-fpm.sock; # Replace <version> with your PHP version (e.g., 7.4) fastcgi_param PHP_VALUE "max_execution_time=30"; # Set PHP configuration variables here }
  3. Save the file and exit the text editor.

  4. Restart Nginx to apply the changes:

    sudo systemctl restart nginx

Now, when Nginx receives a request for a PHP script, it will pass it to PHP-FPM using the specified socket and apply any PHP configuration variables defined in the fastcgi_param directive.

You can add more fastcgi_param directives inside the location block to set additional PHP settings as environment variables. Note that these settings must be compatible with the fastcgi_param format.

Remember to adjust the PHP version and socket path in the configuration according to your setup.