I want to run php under nginx, I have some difficulties. I'm almost done, however, I have an error 502, I don't know why.
I have Nginx 1.18, php 7.4.x with fpm. I created a php file in: /usr/share/nginx/html/info.php, however when I am on it, I have an error 502. Here is the file I have for the default conf:
server {
listen 80;
root /usr/share/nginx/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
I have seen this line in some cases, however when I do "nginx -t" it fails.
include snippets / fastcgi-php.conf;
I looked carefully, everything works locally, everything works. I do not understand.
In your post above you have server_name example.com. Check if you have the right server name there. Also, make sure your application server is running because nginx is unable to reach that's why you are seeing 502
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.