How to Configure Nginx for PHP-FPM on Fedora 40 or 39

Configuring Nginx to work with PHP-FPM on Fedora 40 or 39 is essential for setting up a high-performance server capable of handling dynamic PHP content. Nginx excels at serving static content efficiently, while PHP-FPM manages PHP processes, optimizing the execution of PHP scripts. However, by default, the PHP-FPM service runs under the “apache” user, which is incompatible with Nginx. Therefore, it’s necessary to adjust the PHP-FPM configuration to ensure it works correctly with Nginx.

This guide will walk you through the steps to configure Nginx to work seamlessly with PHP-FPM on Fedora 40 or 39, including the necessary adjustments to the PHP-FPM configuration. By the end of this guide, your server will be ready to efficiently serve PHP-based websites.

Open www.conf Configuration File

First, open the configuration file (www.conf) with the following command:

sudo nano /etc/php-fpm.d/www.conf

Open the configuration file and replace the (Apache) user and group with the (Nginx) user and group as the images demonstrate below:

As above, locate “user” and “group” that have “apache”, which you now change to “nginx” as demonstrated below:

Press (CTRL+O) to save, then press (CTRL+X) to exit.

Make the necessary adjustments, then restart your PHP-FPM service.

sudo systemctl restart php-fpm

Example Nginx PHP-FPM Server Block Code

To handle PHP files with Nginx, you must configure the server block, as shown in the example below. Use this example for all server{} blocks that manage PHP files, and ensure you include “location ~ .php$.” in the configuration.

Example ONLY:

    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;
    }

To verify that the changes made to the previous code did not cause any errors, you can use the following command to test the Nginx configuration.

sudo nginx -t

Example output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

To complete the PHP-FPM setup, restart the Nginx service.

sudo systemctl restart nginx

Conclusion

By configuring Nginx to work with PHP-FPM on Fedora, you’ve created a highly efficient and scalable web server environment capable of handling dynamic content with precision. Adjusting the PHP-FPM configuration to run under the appropriate user was a critical step in ensuring compatibility and security with Nginx. To maintain this setup, regularly monitor server performance and apply updates to both Nginx and PHP-FPM as needed. Proper maintenance will ensure that your server remains secure, efficient, and capable of handling increased traffic and demand.

Joshua James
Follow me
Latest posts by Joshua James (see all)

Leave a Comment