How to connect php-fpm with nginx?

by herminia_bruen , in category: PHP General , 2 months ago

How to connect php-fpm with nginx?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by jasen_gottlieb , 2 months ago

@herminia_bruen 

To connect php-fpm with nginx, follow these steps:

  1. Install php-fpm on your server: You can install php-fpm by running the command sudo apt-get install php-fpm on Debian or Ubuntu-based systems. For CentOS or RedHat-based systems, use sudo yum install php-fpm.
  2. Configure php-fpm: Open the php-fpm configuration file, usually located at /etc/php/{PHP_VERSION}/fpm/pool.d/www.conf. You can adjust settings such as the listen directive (default is listen = /run/php/php{PHP_VERSION}-fpm.sock) and the user/group that php-fpm runs as.
  3. Restart php-fpm: After making changes to the configuration file, restart php-fpm by running sudo systemctl restart php{PHP_VERSION}-fpm.
  4. Configure nginx: Open your nginx configuration file, usually located at /etc/nginx/nginx.conf. Add a location block to proxy PHP requests to php-fpm. For example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
server {
    listen 80;
    server_name example.com;

    root /var/www/html;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php{PHP_VERSION}-fpm.sock;
    }
}


  1. Restart nginx: After making changes to the nginx configuration file, restart nginx by running sudo systemctl restart nginx.
  2. Test your setup: Create a PHP file in your web root directory (e.g. /var/www/html/info.php) with the following contents:
1
2
<?php
phpinfo();


Visit http://example.com/info.php in your web browser to verify that the PHP file is being processed by php-fpm through nginx.


That's it! You have successfully connected php-fpm with nginx.