Nginx is an open-source HTTP server that allows developers to quickly build and deploy interactive websites. Nginx uses PHP-FPM (FastCGI Process Manager) for processing PHP pages. PHP-FPM uses less memory and CPU as compared to traditional CGI-based methods of running PHP. Configuring the Nginx with PHP-FPM can improve the website’s loading speed and allow it to handle a huge amount of traffic in no time.

In this post, we will show you how to install Nginx with PHP-FPM on Fedora Linux.

Step 1 – Install Nginx Web Server

First, you will need to install the Nginx server package on your server. You can install it using the following command.

dnf install nginx -y

Once the Nginx package is installed, start and enable the Nginx service with the following command.

systemctl start nginx
systemctl enable nginx

Step 2 – Install PHP and PHP-FPM

Next, install the PHP and PHP-FPM packages using the following command.

dnf install php php-cli php-common php-fpm -y

After installing all the packages, start and enable the PHP-FPM service.

systemctl start php-fpm
systemctl enable php-fpm

You can check the status of the PHP-FPM service with the following command.

systemctl status php-fpm

You will see the following output.

● php-fpm.service - The PHP FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
     Active: active (running) since Tue 2023-09-19 01:06:35 EDT; 3s ago
   Main PID: 6994 (php-fpm)
     Status: "Ready to handle connections"
      Tasks: 6 (limit: 2328)
     Memory: 10.0M
        CPU: 74ms
     CGroup: /system.slice/php-fpm.service
             ├─6994 php-fpm: master process (/etc/php-fpm.conf)
             ├─6995 php-fpm: pool www
             ├─6996 php-fpm: pool www
             ├─6997 php-fpm: pool www
             ├─6998 php-fpm: pool www
             └─6999 php-fpm: pool www

Sep 19 01:06:35 fedora systemd[1]: Starting The PHP FastCGI Process Manager...
Sep 19 01:06:35 fedora systemd[1]: Started The PHP FastCGI Process Manager.

Step 3 – Configure PHP-FPM with Nginx

Next, you will need to configure Nginx to run PHP with FPM. First, create a document root directory for your site.

mkdir -p /var/www/html/

Next, create a sample info.php file.

nano /var/www/html/info.php

Add the following code:

<?php
    phpinfo();
?>

Save and close the file, then set proper ownership to info.php file.

chown -R nginx: /var/www/html/info.php

Next, create an Nginx virtual host configuration file.

nano /etc/nginx/conf.d/example.conf

Add the following configuration:

server {
    listen 80;
    server_name nginx.example.com;
    root /var/www/html/;
    index info.php;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

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

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        expires max;
        log_not_found off;
    }

}

Save and close the file, then edit the Nginx main configuration file.

nano /etc/nginx/nginx.conf

Add the following line after http{.

server_names_hash_bucket_size 64;

Save and close the file, then verify the Nginx for any syntax errors.

nginx -t

Finally, restart the Nginx service to apply the changes.

systemctl restart nginx

Step 4 – Verify the Setup

At this point, Nginx is configured to work with PHP-FPM. Now, open your web browser and verify your setup using the URL http://nginx.example.com. You will see your PHP information page on the following screen.

Conclusion

In this tutorial, you have learned to configure the Nginx web server with PHP-FPM on the Fedora Linux system. You can also use PHP-FPM to run multiple PHP versions on a single machine. You can now deploy Nginx with PHP-FPM on dedicated server hosting from Atlantic.Net.