top of page

How to Setup PHP on Nginx with FastCGI (PHP-FPM)


Nginx and PHP-FPM (PHP FastCGI Process Manager) work together to serve dynamic web content efficiently. Here's an overview of how they interact:

  1. Client Request: When a client (such as a web browser) makes an HTTP request to the server, it is received by Nginx.

  2. Nginx Configuration: Nginx processes the request based on its configuration, including any server blocks (virtual hosts) and location blocks. If the request matches a static file or location that Nginx can handle directly (like HTML or images), Nginx serves the file directly to the client.

  3. Dynamic Content: If the request requires dynamic content, such as PHP scripts, Nginx passes the request to PHP-FPM for processing.

  4. PHP-FPM: PHP-FPM is a separate process manager for PHP. It manages pools of PHP worker processes to handle PHP requests efficiently. When Nginx forwards a PHP request to PHP-FPM, it uses a FastCGI protocol to communicate. PHP-FPM receives the request and selects an available PHP worker process from its pool to handle it.

  5. PHP Processing: The PHP worker process executes the requested PHP script.

It communicates with PHP-FPM to handle PHP-specific tasks like session management, input/output handling, and other PHP-related operations.

6. Response: After processing the PHP script, the PHP worker process generates the HTML content, which includes any dynamic data processed by PHP.

PHP-FPM sends the generated content back to Nginx via the FastCGI protocol.

7. Nginx Response: Nginx receives the response from PHP-FPM and sends it back to the client that made the initial request.

1. Install Nginx & Php-Fpm:

[root@siddhesh ~]# yum install nginx php-fpm

2. Configure PHP-FPM:

PHP-FPM configuration files are usually located in /etc/php-fpm.d/. Edit the www.conf file to configure PHP-FPM settings:

[root@siddhesh ~]# egrep -i '^user|^group|^listen ' /etc/php-fpm.d/www.conf
user = nginx
group = nginx
listen = /var/run/php-fpm.sock
[root@siddhesh ~]#

Adjust settings as needed. For example, you might need to set a listen directive, user/group settings, etc.

3. Start and Enable PHP-FPM:
[root@siddhesh ~]# systemctl start php-fpm
[root@siddhesh ~]# systemctl enable php-fpm
4. Configure Nginx:

Create a server block configuration file for your website in the /etc/nginx/conf.d/ directory. For example:

[root@siddhesh ~]# cat /etc/nginx/conf.d/builddevops.conf
server {
    listen 80;
    server_name _;
    root /var/www/html/;
    index index.php index.html index.htm;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
[root@siddhesh ~]#

Let's break down what each section does:

server: This block defines the configuration for a single server. In this case, it listens on port 80 for HTTP requests.

listen 80: Specifies that this server block will listen on port 80, the default port for HTTP traffic.

server_name : The underscore () as the server name indicates a catch-all server block, meaning it will respond to any request that doesn't match other server blocks' server names.

root /var/www/html/ Sets the root directory for this server block. When a request comes in, Nginx will look for files in this directory to serve.

index index.php index.html index.htm: Specifies the index files to try if a directory is requested. It will first look for index.php, then index.html, and finally index.htm.

location / {...}: Defines configuration for requests that match the root URI ("/"). It tries to serve static files directly and if not found, it passes the request to index.php.

try_files $uri $uri/ /index.php?$query_string: Tries to serve the requested URI directly. If it doesn't exist as a file or directory, it internally redirects the request to index.php, passing any query string parameters.

location ~ .php$ {...}: Defines configuration for requests ending with ".php". It's used for processing PHP files.

include fastcgi_params: Includes FastCGI configuration parameters needed for PHP processing.

fastcgi_pass unix:/var/run/php-fpm.sock: Specifies the FastCGI server address. In this case, it's a UNIX socket where PHP-FPM is listening for requests.

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name: Sets the value of the SCRIPT_FILENAME FastCGI parameter to the full path of the PHP script being executed.

fastcgi_param SCRIPT_NAME $fastcgi_script_name: Sets the value of the SCRIPT_NAME FastCGI parameter to the URI of the PHP script being executed.

5. Create Web Root Directory:

Create the directory for your website's files:

[root@siddhesh ~]# mkdir /var/www/html/
6. Set Permissions:

Adjust permissions so that Nginx can read and execute files in your web root directory:

[root@siddhesh ~]# chown -R nginx:nginx /var/www/html/
7. Test Nginx Configuration:

Check for syntax errors in your Nginx configuration:

[root@siddhesh ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@siddhesh ~]#
8. Start and Enable Nginx:

If the configuration test is successful, start Nginx to apply the changes:

[root@siddhesh ~]# systemctl start nginx
[root@siddhesh ~]# systemctl enable nginx
Created symlink /etc/systemd/system/ → /usr/lib/systemd/system/nginx.service.
[root@siddhesh ~]#
9. Test PHP-FPM & Nginx:

Create a PHP file in your web root directory to test PHP processing:

[root@siddhesh ~]# cat /var/www/html/
echo "<div style='color: #007bff; font-size: 24px; font-weight: bold; text-align: center;'>Welcome to <span style='color: #28a745;'>builddevops</span>.com</div>";
[root@siddhesh ~]#

Access this site in your web browser (e.g., http://<IPADDRESS>/) to verify PHP-FPM is working correctly.

Nginx php-fpm

תגובה אחת

דירוג של 0 מתוך 5 כוכבים
אין עדיין דירוגים

הוספת דירוג
01 במאי
דירוג של 5 מתוך 5 כוכבים
bottom of page