How To Configure PHP-FPM With Nginx

PHP-FPM (FastCGI Process Manager) is a powerful tool that improves PHP performance and resource management, making it ideal for high-traffic websites. When combined with Nginx, it ensures efficient request handling. In this guide, we’ll show you how to configure PHP-FPM with Nginx for optimal performance.

Unlike Apache, which traditionally uses mod_php, Nginx does not process PHP natively. Instead, it relies on PHP-FPM to handle PHP scripts. This separation enhances security, performance, and flexibility. In this guide, we’ll walk you through installing, configuring, and optimizing PHP-FPM with Nginx.

Steps To Install And Configure PHP-FPM With Nginx

1. Install Nginx and PHP-FPM

Before configuring PHP-FPM, install the required packages:

sudo apt update
sudo apt install nginx php<-your-php-version->-fpm -y

Example:

sudo apt install nginx php8.3-fpm -y

Ensure PHP-FPM is running:

sudo systemctl start php<-your-php-version->-fpm
sudo systemctl enable php<-your-php-version->-fpm

Check status PHP-FPM:

sudo systemctl status php<-your-php-version->-fpm
Check status PHP-FPM

2. Configure Nginx to Use PHP-FPM

Open your Nginx server block configuration file:

sudo nano /etc/nginx/sites-available/default

Update the configuration to pass PHP requests to PHP-FPM:

server {
    listen 80;
    server_name yourdomain.com;
    root /var/www/html;

    index index.php index.html index.htm;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
⚠️ Adjust the fastcgi_pass line according to your PHP version (e.g., php8.3-fpm.sock).

Save and exit (CTRL + X, then Y and Enter).

Restart Nginx:

sudo systemctl restart nginx

3. Verify PHP-FPM with Nginx

To test if PHP-FPM is working correctly:

Create a test PHP file:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Open http://yourdomain.com/info.php in a browser.

If PHP-FPM is configured correctly, you should see the PHP info page.

PHP info page

Configuring PHP-FPM with Nginx enhances server performance and efficiently handles PHP requests. By setting up PHP-FPM correctly, you ensure faster processing and better resource management for your web applications.

Follow us for the more helpful posts!

We hope this is a useful post for you.

You can read more useful posts like How To Configure CDN In Magento 2.

Thank you for reading!

 

5 1 vote
Article Rating

Aaron LX

Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.

Leave a Reply or put your Question here

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x