nginx php-fpm 502 Bad Gateway - php

I was running Ubuntu server 20.04 quite successfully with Ired mail and 2 websites, one of them with WordPress.
I wanted to install Nextcloud, to do that I had to reinstall php-fpm to generate php7.4-fpm.sock. After this Nextcloud worked, but my other websites stopped working with error '502 Bad Gateway'.
So to say the least, I'm very confused!
I followed this article to install Nextcloud and set up the sites-enabled .conf file as per instructions: https://www.linuxbabe.com/ubuntu/install-nextcloud-ubuntu-20-04-nginx-lemp-stack/amp
I think I understand that the .conf file used to listen on 127.0.0.1:XXXX and now listens on php7.4-fpm.sock?
Here is the .conf file that I have put together for my website after re-installing php-fpm:
#
# Note: This file must be loaded before other virtual host config files,
#
# HTTPS
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name SOMEWEBSITE www.SOMEWEBSITE;
error_log /var/log/nginx/localhost.error_log info;
root /var/www/SOMEWEBSITE/html;
index index.php index.html;
include /etc/nginx/templates/misc.tmpl;
include /etc/nginx/templates/ssl.tmpl;
include /etc/nginx/templates/iredadmin.tmpl;
include /etc/nginx/templates/roundcube.tmpl;
include /etc/nginx/templates/sogo.tmpl;
include /etc/nginx/templates/netdata.tmpl;
include /etc/nginx/templates/php-catchall.tmpl;
include /etc/nginx/templates/stub_status.tmpl;
location / {
try_files $uri $uri/ /index.php?q=$uri$args;
}
# PHP handling
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
ssl_certificate /etc/letsencrypt/live/SOMEWEBSITE/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/SOMEWEBSITE/privkey.pem; # managed by Certbot
}
# Redirect http to https
server {
listen 80;
listen [::]:80;
server_name SOMEWEBSITE www.SOMEWEBSITE;
return 301 https://$host$request_uri;
}
I have checked the file permissions for php7.4-fpm.sock
ll /var/run/php/ | grep php
-rw-r--r-- 1 root root 3 May 22 21:13 php7.4-fpm.pid
srw-rw---- 1 www-data www-data 0 May 22 21:13 php7.4-fpm.sock=
lrwxrwxrwx 1 root root 30 May 22 21:13 php-fpm.sock -> /etc/alternatives/php-fpm.sock=
and I think it looks ok.
here is the log file:
2021/05/23 20:32:52 [error] 43596#43596: *305 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xxx.xxx, server: SOMEWEBSITE, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9999", host: "SOMEWEBSITE"
2021/05/23 20:32:53 [info] 43596#43596: *305 client xx.xx.xxx.xxx closed keepalive connection
Any Ideas? Need any more information? Thank you in advance for looking.

PHP-FPM can listen using two method for accepting fastcgi request. using TCP Socket or with Unix Socket.
You can sepecify it in php-fpm configuration, In Ubuntu the configuration is in /etc/php/7.4/fpm/pool.d/www.conf and check listen configuration.
If you want to use unix socket, use configuration below.
listen = /run/php/php7.4-fpm.sock
For TCP Socket.
listen = 127.0.0.1:9000
Next in nginx you can specify fastcgi_pass based on fpm configuration. If you using Unix socket, all your website, include Nextcloud must be using Unix Socket.
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
If you using TCP Socket, you must change the nginx configuration for Nextcloud to pass from TCP Socket.
fastcgi_pass 127.0.0.1:9000;

I have faced this issue as well. the problem is php-fpm didnt listen port 9999 (In my case i use port 9999). To make /mail/ work. u need to change below config file. change ip:port change to socket
/etc/php/fpm/pool.d/www.conf
listen = /var/run/php/php7.2-fpm.sock
/etc/nginx/templates/fastcgi_php.tmpl
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

Related

( solved ) Cant get php to run with nginx // whatevery i try i get 502 Bad Gateway

i tryed so much and followed countless guides for that problem but just cant find a solution, so i hope that you can find a problem in my configs or have a other idea what the problem could be.
Beside my planned webpage i run the AMP-Webinterface for server-administration on a supdomain amp.EXAMPLE.COM, on my debian10 server, and that always works like a charm, only my own test-php-site (see below) wont show correctly, i tested with a example html site and that works perfectly.
php8.0-fpm is installed and the only php version (had 7.3 too before and removed it already)
#######################################################
index.php
<html>
<head>
<title>PHP-Test</title>
</head>
<body>
<?php echo '<p>Hallo Welt</p>'; ?>
</body>
</html>
#######################################################
EXAMPLE.COM.conf
server {
if ($host = www.EXAMPLE.COM) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = EXAMPLE.COM) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80; listen [::]:80;
server_name EXAMPLE.COM www.EXAMPLE.COM;
root /var/www/EXAMPLE.COM;
index index.php index.html index.htm index.nginx-debian.html;
location / {
location ~* \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
try_files $uri #yii =404;
}
} }
server {
listen 443; listen [::]:443;
server_name EXAMPLE.COM www.EXAMPLE.COM;
root /var/www/EXAMPLE.COM;
index index.php index.html index.htm index.nginx-debian.html;
location / {
location ~* \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
try_files $uri #yii =404;
}
}
ssl_certificate /etc/letsencrypt/live/EXAMPLE.COM/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/EXAMPLE.COM/privkey.pem; # managed by Certbot
}
#######################################################
nginx -t
nginx: [warn] conflicting server name "amp.EXAMPLE.COM" on [::]:443, ignored
nginx: [warn] conflicting server name "amp.EXAMPLE.COM" on 0.0.0.0:443, ignored
nginx: [warn] conflicting server name "amp.EXAMPLE.COM" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "amp.EXAMPLE.COM" on [::]:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Nginx Error log isnt showing any errors too, except the "conflicting server name thing"
#######################################################
php8.0-fpm is installed and working ("php-fpm" cant be found by systemctl, could this be a problem?)
systemctl status php8.0-fpm
● php8.0-fpm.service - The PHP 8.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php8.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2021-03-16 14:08:33 UTC; 18h ago
Docs: man:php-fpm8.0(8) Main PID: 100460 (php-fpm8.0) Status: "Processes active: 0, idle: 2, > Requests: 11, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 9508) Memory: 17.6M CGroup: /system.slice/php8.0-fpm.service
├─100460 php-fpm: master process (/etc/php/8.0/fpm/php-fpm.conf)
├─100461 php-fpm: pool www
└─100462 php-fpm: pool www
Mar 16 14:08:33 localhost systemd[1]: Starting The PHP 8.0 FastCGI Process Manager... Mar 16 14:08:33 localhost systemd[1]: Started The PHP 8.0 FastCGI Process Manager.
#######################################################
tail -f /var/log/nginx/error.log
[error] 129821#129821: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 217.254.148.38, server: EXAMPLE.COM, request: "GET / HTTP/2.0", upstream: "fastcgi://127.0.0.1:9000", host: "EXAMPLE.COM"
#######################################################
Thank you i finally found the solution! breaksdownintearsofjoy
Problem has been that fastcgi wasnt installed >_>
For my debian 10 i just did: apt-get install fcgiwrap
problem was the localhost ip it kinda didnt liked; so i just replaced "fastcgi_pass 127.0.0.1:9000;" with the complete dir of the php fpm sock "fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;"

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

I am trying to install nginx and php-fpm docker images in AWS Fargate without any success running out of options, what I see is either the nginx container unable to route the traffic to php container or php container unable to read files from EFS file system.
I have EFS file system holding sample index.html and index.php files in root folder and this EFS is shared with two containers nginx and php-fpm mounted from volumes in AWS Task definition creation wizard.
when I navigate to the ELB friendly DNS name I can successfully load index.html page but not index.php page I get "Primary script unknown" while reading response header from upstream, client: x.x.x.x, server: *.amazonaws.com, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000".
Changed upstream value to php:9000; or 127.0.0.1:9000; or resolver 127.0.0.11 none of them worked.
I got same error locally but if I hardcore fastcgi upstream and server_name fields with actual IP address in default.conf and listen=ipaddress of phpcontainer:9000 in www.conf file in pgp-fpm container this resolved the issue locally but in AWS fargate the IP's all ways change after restarting container any advise appreciated.
server {
listen 80;
#root /usr/share/nginx/html;
#local testing for docker-compose up
root /php;
index index.php index.html index.htm;
#server_name *.amazonaws.com;
server_name x.x.x.x; #local testing
#set $upstream 127.0.0.1:9000;
set $upstream x.x.x.x:9000; #local testing
location / {
try_files $uri $uri/ =404;
}
location /dataroot/ {
internal;
alias /usr/share/nginx/html/moodledata/;
}
location ~ ^(.+\.php)(.*)$ {
#root /usr/share/nginx/html;
#local testing
root /php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_index index.php;
fastcgi_pass $upstream;
include /etc/nginx/mime.types;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Finally figured out the issue after trying multiple combinations, its causing from root path which is different to each container nginx (/usr/shr/nginx/html) and php (/var/www/html)....the error message doesnt help for newbies working in php and linux world.
After changing the nginx root path as /var/www/html same as php-fpm container /var/www/html the issue was resolved of course the permissions has to be taken care of if your accessing the EFS volume mounts.

how to terminate strange services using a port causing error EADDRINUSE

I was trying to install and configure nginx to execute php files, i installed php and everything went well and working except now my node app gives me EADDRINUSE port: 443 error saying the port for my node server that I was using before is now busy.
I tried to log the services (i guess these are services) that are using the port 443 with lsof -i tcp:443 and this is the result:
I tried to kill them with kill -9 PID but then they just come back, if i reload the site they become more.
so my question is what these actually are and how can i stop this madness?
my nginx configuration:
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name 45.76.***.10;
root /srv/main/site;
index index.html index.php;
ssl_certificate /etc/ssl/certs/mysite.com.pem;
ssl_certificate_key /etc/ssl/private/mysite.com.pem;
ssl_client_certificate /etc/ssl/certs/origin-pull-ca.pem;
ssl_verify_client on;
client_max_body_size 100M;
autoindex off;
location / {
index index.html index.php;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
Kill the running task using the below commands -
sudo kill -9 $(sudo lsof -t -i:443)
You can also use -
sudo fuser -k 443/tcp
Hope this helps.

unable to get hhvm working with nginx

I have nginx and hhvm installed and running as processes on a ubuntu VM. The configs are copied below. I have an index.php file in /usr/share/nginx/html (pointed to by nginx) but when I try to access the site beign served by nginx, I get the following error about hhvm:
/var/log/nginx/error.log
2016/10/04 12:03:05 [crit] 12443#0: *1 connect() to unix:/var/run/hhvm/hhvm.sock failed (2: No such file or directory) while connecting to upstr
eam, client: xx.xxx.xxx.xxx, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/hhvm/hhvm.sock:", host: "xx.xx.xx.xx"
Nginx config (part of config)
server {
listen 80 default_server;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
index index.php;
try_files $uri $uri/ $uri/index.php$args /index.php$args;
}
location /mysql {
index index.php;
}
location ~* \.(php)$ {
try_files $uri = 404;
location ~ \..*/.*\.php$ {return 404;}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_keep_conn on;
fastcgi_pass unix:/var/run/hhvm/hhvm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I have checked - bothh nginx and hhvm processes are running. Any thoughts on how to troubleshoot this further?
Update:
I changed the server.ini file in /etc/hhvm to the following and restarted the service but it does not appear to have done the job
hhvm.server.file.socket=/var/run/hhvm/hhvm.sock
I commented out the 9000 port being used previously.
Update:
After making the server.init change and restarting hhvm, I get the following error in /var/log/hhvm/error.log:
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to read pid file /var/run/hhvm/pid for any meaningful pid
Unable to start page server
Shutting down due to failure(s) to bind in HttpServer::runAndExitProcess
The /var/run/hhvm directory is owned by www-data
I ended up running hhvm on port 9000 and pointed nginx (fasgcgi_pas) to point to the port (instead of using the file socket option).

Curl error : No route to host

So we're building a web application in PHP and we're trying to make requests to an external API. Problem is that we're getting a curl error:
cURL error 7: Failed to connect to external.api.com port 443: No route to host
A little bit of background now.
We're making requests using Guzzle.
We're hosting on Apache, which is running on a Linux machine and we're also using SSL.
The API is also using SSL, therefore the port 443 in error message.
The HTTP requests include a certificate for authentication.
I've managed to get it running on two different development environments but not on the production one. I suspect the problem is in the configuration of Apache, as if we haven't made it available to make requests to certain IP or port. I have no idea how to check it. I've read that I might have to change the file /etc/network/interface yet I haven't found any info on what to write there.
I've also read I have to run $ netstat -rn for answers yet I'm not sure what to look there.
EDIT:
Can't even make a simple get request without any parameters and anything.
Yet I can make requests to https://google.com and https://facebook.com. Will write more in a few.
After a lot of debugging and testing all of my code I contacted the service, whose API I was trying to consume.
They were an European service provider and they had whitelisted European IP's. Our production server was in the USA and after they whitelisted our IP, everything worked.
It worked for me for apache (httpd)
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
netstat -aln | grep 443 will show if your webserver is listening on that port.
Depending on which webserver you have installed your configuration file, for the site will be at /etc/nginx/sites-available/default, /etc/nginx/sites-available/yourSite, /etc/nginx/nginx.conf or some other similar paths for apache.
Wherever it is located, your configuration file should contain something like the following:
server {
listen 80;
listen 443 ssl;
server_name yourSite.com;
root "/path/to/yourSite";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /path/to/webserver/youSite.error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /path/to/yourSite.crt;
ssl_certificate_key /path/to/yourSite.key;
}
After changing this file make sure to sudo service nginx reload or sudo service nginx restart (or the relative apache command).
sudo service nginx configtest or sudo nginx -t will help with debugging the config file.
After searching for about a whole day I found that the problem was in the iptables rules.
In my case the solution was to restore the iptables rules as follows:
create a file containing the following text:
*filter
:INPUT ACCEPT [10128:1310789]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [9631:1361545]
COMMIT*
run the command: sudo iptables-restore < /path/to/your/previously/created/file
This will hopefully fix your problem if it is an iptables issue.
today I hava face up the same question, like that
I use curl http://localhost:8080 to check my tomcat can work or not
And it's error Failed to connect to ::1: No route to host
Finally I hava solve it.Obviously, it's the problem of your Apache tomcat.
So you must check your logs firstly. If you found your port was used, find that course and kill it. Then restart your tomcat.
find the course by port: netstan -lnp | grep port
kill course: kill -9 ****

Categories