How to Connect PHP-FPM With Nginx?

11 minutes read

To connect PHP-FPM with Nginx, follow these steps:

  1. Install Nginx: Start by installing Nginx on your server if it is not already installed. You can use the package manager of your operating system to install Nginx.
  2. Install PHP-FPM: Install PHP-FPM (FastCGI Process Manager) on your server. PHP-FPM is responsible for processing PHP code and communicating with Nginx.
  3. Configure PHP-FPM: Once PHP-FPM is installed, you need to configure it. The configuration file location varies based on your operating system and PHP version. Common locations are /etc/php-fpm.conf, /etc/php/7.4/fpm/php-fpm.conf, or /etc/php-fpm.d/www.conf. Open the configuration file in a text editor and make the necessary changes. Ensure the listen directive is set correctly to the address and port where PHP-FPM will listen. By default, it might be set to 127.0.0.1:9000.
  4. Configure Nginx: Open the Nginx configuration file, usually located at /etc/nginx/nginx.conf. Inside the http block, add a server block to define the PHP-FPM backend. For example: server { listen 80; server_name example.com; location / { try_files $uri $uri/ =404; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
  5. Restart Services: Save the changes and exit the text editor. Restart the PHP-FPM and Nginx services to apply the new configurations. Use the appropriate commands based on your operating system. For example: on Ubuntu/Debian: sudo service php-fpm restart and sudo service nginx restart on CentOS/RHEL: sudo systemctl restart php-fpm and sudo systemctl restart nginx
  6. Test Configuration: Create a PHP file, such as info.php, in your server's web root directory (usually /var/www/html) with the following content: Access the file via a web browser by visiting http://example.com/info.php (replace example.com with your server's domain or IP). If PHP is correctly connected to Nginx via PHP-FPM, it should display the PHP information page.


That's it! PHP-FPM should now be successfully connected to Nginx, allowing you to run PHP scripts using Nginx as the web server.

Best Nginx Books to Ready in 2024

1
Nginx HTTP Server - Third Edition: Harness the power of Nginx to make the most of your infrastructure and serve pages faster than ever

Rating is 5 out of 5

Nginx HTTP Server - Third Edition: Harness the power of Nginx to make the most of your infrastructure and serve pages faster than ever

2
Mastering NGINX Second Edition

Rating is 4.9 out of 5

Mastering NGINX Second Edition

3
NGINX Cookbook: Over 70 recipes for real-world configuration, deployment, and performance

Rating is 4.8 out of 5

NGINX Cookbook: Over 70 recipes for real-world configuration, deployment, and performance

4
Nginx HTTP Server: Harness the power of Nginx to make the most of your infrastructure and serve pages faster than ever before, 4th Edition

Rating is 4.7 out of 5

Nginx HTTP Server: Harness the power of Nginx to make the most of your infrastructure and serve pages faster than ever before, 4th Edition

5
NGINX Cookbook: Advanced Recipes for High-Performance Load Balancing

Rating is 4.6 out of 5

NGINX Cookbook: Advanced Recipes for High-Performance Load Balancing

6
Nginx Simplified: Practical Guide to Web Server Configuration and Optimization

Rating is 4.5 out of 5

Nginx Simplified: Practical Guide to Web Server Configuration and Optimization


How to configure PHP-FPM with Nginx?

To configure PHP-FPM with Nginx, follow these steps:

  1. Install PHP-FPM and Nginx on your server if you haven't already. You can use package managers like apt or yum for this.
  2. Edit the php-fpm configuration file. The location of this file depends on your system, but it is usually located in /etc/php-fpm.conf or /etc/php7/fpm/php-fpm.conf. Open the file in a text editor.
  3. Locate the listen directive in the configuration file. This directive specifies the socket or port on which PHP-FPM listens for requests. By default, it should be set to 127.0.0.1:9000. If you want to use a Unix socket instead of a TCP socket, you can change the value to a socket path like /var/run/php/php7.0-fpm.sock.
  4. Configure Nginx to proxy PHP requests to PHP-FPM. Open the Nginx configuration file, which is usually located in /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf. Add or modify the following configuration block within the http block:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
server {
    listen 80;
    server_name example.com;

    root /var/www/html;
    index index.php;

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;  # or use the Unix socket path here
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}


In this configuration, we define a server block that listens on port 80 and serves PHP files using PHP-FPM.

  1. Save the Nginx configuration file and exit the text editor.
  2. Test the configuration. Run sudo nginx -t to check for any syntax errors in the configuration files. If everything is correct, you should see syntax is okay message.
  3. Restart both Nginx and PHP-FPM to apply the changes. Run sudo systemctl restart nginx and sudo systemctl restart php-fpm (or equivalent commands for your system).


Now, Nginx should be configured to handle PHP requests using PHP-FPM. You can create a PHP file (e.g., info.php) in your web server's root directory (/var/www/html in this example) with the following content to test if PHP is working:

1
<?php phpinfo(); ?>


Accessing http://example.com/info.php in your browser should display the PHP information page if everything is set up correctly.


What is a socket file in PHP-FPM and Nginx setup?

In a PHP-FPM and Nginx setup, a socket file is a special file that allows communication between Nginx and PHP-FPM. It serves as a connection point or a "bridge" between the two components.


The socket file is created by PHP-FPM and defines a unique address to which Nginx can send requests for processing. When Nginx receives a request for a PHP file, it forwards the request to the appropriate socket file associated with PHP-FPM.


Instead of using a traditional network socket, which relies on IP addresses and ports, a socket file resides within the file system. It is typically located in the /var/run directory or a designated directory for PHP-FPM.


Using a socket file instead of a network socket can provide some performance benefits as it reduces overhead and circumvents the need for network stack processing.


In the PHP-FPM and Nginx setup, the socket file plays a crucial role in facilitating the communication and processing of PHP requests by passing them from the web server (Nginx) to the PHP processing engine (PHP-FPM).


How to enable PHP-FPM slow log in Nginx?

To enable PHP-FPM slow log in Nginx, you need to follow these steps:

  1. Open the PHP-FPM configuration file. The location and name of the file may vary depending on your server setup. Common locations include /etc/php/7.x/fpm/php.ini or /etc/php-fpm.d/www.conf.
  2. Look for the request_slowlog_timeout directive in the file. Uncomment it if necessary and set a value for it. This value represents the time threshold (in seconds) for requests that are considered slow. For example, you can set request_slowlog_timeout = 5s to log requests that take longer than 5 seconds.
  3. Set the slowlog directive to specify the file where the slow logs will be written. You can choose any location for the log file, but make sure the directory is writable by the PHP-FPM process. For example, you can set slowlog = /var/log/php-fpm/slow.log.
  4. Save the PHP-FPM configuration file and exit the editor.
  5. Restart the PHP-FPM service for the changes to take effect. The command to restart the service may vary depending on your system, but common commands include sudo service php-fpm restart or sudo systemctl restart php-fpm.
  6. Open the Nginx configuration file for your site. The location and name of the file may vary, but common locations include /etc/nginx/sites-available/default or /etc/nginx/conf.d/default.conf.
  7. Add the following location block within the server block: location ~ \.php$ { ... include fastcgi_params; # Enable slow log slowlog /var/log/nginx/php-slow.log; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Replace with your PHP-FPM socket ... } Make sure to replace /var/log/nginx/php-slow.log with the actual path where you want to store the slow log file. Also, ensure that the fastcgi_pass directive points to the correct PHP-FPM socket.
  8. Save the Nginx configuration file and exit the editor.
  9. Restart the Nginx service to apply the changes. The command to restart the service may vary depending on your system, but common commands include sudo service nginx restart or sudo systemctl restart nginx.


After completing these steps, PHP-FPM slow logs will be generated for requests that exceed the specified time threshold, and they will be written to the specified log file.


What is the PHP-FPM process priority in Nginx?

The PHP-FPM process priority in Nginx is determined by the process_nice_value directive in the PHP-FPM configuration file (php-fpm.conf). This directive determines the process priority by assigning a nice value to the PHP-FPM processes.


The nice value ranges from -20 to 19, with -20 being the highest priority and 19 being the lowest. By default, PHP-FPM processes run with a nice value of 0, which means they have a normal priority.


To change the process priority, you can modify the process_nice_value directive in the php-fpm.conf file and set it to a desired nice value. For example, to increase the priority, you can set a negative nice value, such as -10. Conversely, to lower the priority, you can set a positive nice value, such as 10.


Once you have made the changes, you will need to restart the PHP-FPM service for the new process priority to take effect.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To use NGINX to host a website, follow these steps:Install NGINX: Begin by installing NGINX on your server or computer. The installation process may vary depending on your operating system. NGINX has official documentation to guide you through the installation...
To install Nginx on Amazon Linux, you can follow these steps:Connect to your Amazon Linux instance using SSH or an EC2 Instance Connect.Update your system&#39;s package manager by running the command: sudo yum update.Install Nginx by running the command: sudo ...
To enable Brotli compression in NGINX, you can follow these steps:Start by installing the necessary tools. Ensure that you have the NGINX web server installed on your system. You also need the Brotli compression library and the ngx_brotli module for NGINX. Onc...
To increase the NGINX timeout, you need to make changes to the NGINX configuration file. Here&#39;s how:Locate the NGINX configuration file. It is typically named nginx.conf or nginx.conf.sample and is usually located in the /etc/nginx/ directory. Open the NGI...
To configure Nginx in Ubuntu, you need to perform the following steps:Install Nginx: Begin by installing Nginx using the package manager of Ubuntu. Enter the command sudo apt-get install nginx in the terminal to perform the installation. Start Nginx: After the...
To install Nginx in Arch Linux, you can follow these steps:Update the package manager by running the command: sudo pacman -Syu Install Nginx by executing the command: sudo pacman -S nginx Once the installation is complete, start the Nginx service using: sudo s...