7

Newly create LEMP stack running PHP-FPM on Ubuntu 14.04 x64. Attempting to access some PHP in my webroot. Browser is showing 502 Bad Gateway and Nginx error log is showing the following (my IP and production IP removed):

TIME [error] 22838#0: *7 connect() failed (111: Connection refused) while connecting to upstream, client: [my personal IP], server: [production server IP], request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "[production server IP]"

To determine if PHP-FPM is running I execute:

ps -waux | grep php5

And I get back in response:

root     22930  0.0  0.4 327432 16324 ?        Ss   10:10   0:00 php-fpm: master process (/etc/php5/fpm/php-fpm.conf) 

How do I determine what port PHP-FPM is running on or why it isn't running on that port? Thank you.

sparecycle
  • 459
  • 1
  • 6
  • 19

3 Answers3

13

Found it:

In order to specify the port number you would need to edit the "listen =" directive in /etc/php5/fpm/pool.d/www.conf however I discovered that it is theoretically more efficient to allow Nginx to communicate over Unix sockets so I swapped

127.0.0.1:9000

with

/var/run/php5-fpm.sock

That was the first step...

I then had to replace the following lines in my sites configuration file (sites-available/default):

fastcgi_pass 127.0.0.1:9000;

with

fastcgi_pass unix:/var/run/php5-fpm.sock;

And now I am up and running.

sparecycle
  • 459
  • 1
  • 6
  • 19
1

If your service (not only PHP-FPM) are listening port, you can determine it by typing sudo netstat -lntp:

# sudo netstat -lntp
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 192.168.16.121:3306     0.0.0.0:*               LISTEN      1427/mysqld     
tcp        0      0 127.0.0.1:11211         0.0.0.0:*               LISTEN      1722/memcached  
tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN      642/smbd        
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      21315/nginx     
tcp        0      0 127.0.0.1:81            0.0.0.0:*               LISTEN      25078/php-fpm.conf)

where you can see that PHP-FPM process with pid 25078 uses 81 port.

Victor Perov
  • 255
  • 2
  • 9
0

The problem is having more than 1 config file in /etc/php/7.4/fpm/pool.d

   usr@server:/etc/php/7.4/fpm/pool.d$ ls
      www.conf  foor-php-fpm.conf  bar-php-fpm.conf.bak

Solution

Step 1: Remove or rename any other .conf files and leave only www.conf

usr@server:/etc/php/7.4/fpm/pool.d$ sudo mv other-php-fpm.conf other-php-fpm.conf.bak2

Step 2: systemctl restart php7.4-fpm.service

Done!

user@server:~$ systemctl status php7.4-fpm.service

● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2020-12-31 16:16:22 UTC; 6s ago
       Docs: man:php-fpm7.4(8)
    Process: 381508 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74 (code=exited, status=0/SUCCESS)
   Main PID: 381487 (php-fpm7.4)
     Status: "Ready to handle connections"
      Tasks: 3 (limit: 4568)
     Memory: 12.1M
     CGroup: /system.slice/php7.4-fpm.service
             ├─381487 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─381506 php-fpm: pool www
             └─381507 php-fpm: pool www

This solution applies to php-fpm-7.2 as well.