502 Bad Gateway Nginx after server restore - php

So I've got a Ubuntu server that I'm trying to restore from a backup. And when a dummy PHP version page I get 502 Bad Gateway. What have I tried to fix it? Quite a few things including the answers from this related question - nginx 502 bad gateway
This is my default config with Ubuntu
server {
listen 80;
# listen [::]:81 ipv6only=on default_server;
root /var/www/html;
index index.php index.html index.htm;
server_name localhost;
location ~* \.(js|css|png|jpg|jpeg|gif|ico|html)$ {
expires max;
}
location / {
try_files $uri $uri.php $uri.php/$args /index.php?q=$uri&$args $uri/ =404;
index index.php index.html index.htm;
rewrite ^(.*)$ /$1.php;
}
location /phpmyadmin {
index index.php;
try_files $uri $uri/ =404;
auth_basic "Admin Login";
auth_basic_user_file /etc/nginx/pma_pass;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
EDIT:
Here's what I think is the relevant part of the log
2017/08/11 13:18:13 [error] 27759#0: *270 connect() failed (111: Connection refused) while connecting to upstream, client: 66.61.18.112, server: domain.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8080/favicon.ico", host: "[IP ADDRESS]", referrer: "http://[IP ADDRESS]/phpmyadmin/index.php"
EDIT 2:
Here's the result when I run ps aux | grep php-fpm:
mre 21685 0.0 0.0 11756 932 pts/0 S+ 10:42 0:00 grep --color=auto php-fpm
root 32721 0.0 1.0 269300 11168 ? Ss Aug11 0:30 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
www-data 32724 0.0 0.4 269300 4540 ? S Aug11 0:00 php-fpm: pool www
www-data 32725 0.0 0.4 269300 4540 ? S Aug11 0:00 php-fpm: pool www
Edit 3:
Here's what's uncommented out of my php-fpm.conf
[global]
; Pid file
; Note: the default prefix is /var
; Default Value: none
pid = /var/run/php5-fpm.pid
; Error log file
; If it's set to "syslog", log is sent to syslogd instead of being written
; in a local file.
; Note: the default prefix is /var
; Default Value: log/php-fpm.log
error_log = /var/log/php5-fpm.log
;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;
; To configure the pools it is recommended to have one .conf file per
; pool in the following directory:
include=/etc/php5/fpm/pool.d/*.conf
Edit 4:
Here's what's uncommented from my www.conf:
listen = 127.0.0.1:9000
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
Edit 5:
My nginx log after I try to go to index.php:
[error] 29393#0: *23 connect() failed (111: Connection refused) while connecting to upstream, client: [Computer IP Address], server: [Subdomain.domain.com], request: "GET /favicon.ico HTTP/1.1", upstream:"http://127.0.0.1:8080/favicon.ico", host: [Server IP], referrer: "http://[Server IP]/index.php"

So I checked out my nginx config and found these lines:
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
Then I went to /etc/nginx/conf.d/*.conf and saw that in the servers.conf file, there was a config for a sub domain that I planned on using but won't anymore so I deleted it. This was a big part of the problem because nginx was looking in this config first and that's why the errors included this subdomain.
As for the php problems, I switched my config to fastcgi_pass unix:/run/php/php7.0-fpm.sock; and that seems to have fixed the problem.

Try to replace
fastcgi_pass unix:/var/run/php5-fpm.sock;
in your nginx config to
fastcgi_pass 127.0.0.1:9000;
Because if your PHP-FPM config you use TCP instead of socket (it's OK).

You have to change this line
from:
listen = 127.0.0.1:9000
to:
listen = /var/run/php5-fpm.sock
in your www.conf file
Don't forget to restart php.
Explanation:
If you look at your nginx virtual host fastcgi_pass parameter
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
you'll see that you are pointing processing of php files to php5-fpm socket but in your www.conf file php is listening port 9000 which is why you should change it to /var/run/php5-fpm.sock

Related

Nginx error : "Primary script unknown ", how can I resolve that ? Thank you

Config :
macOS Mojave
Debian 9.9.0 64bits on virtualbox 6.0.8, on port 192.168.56.50
php fpm 7.2 on debian
nginx/stable,now 1.16.0-1~stretch on debian
Nginx and php fpm have www-data as user.
Dirs :
lrwxrwxrwx www-data www-data /var/www/all -> /media/sf_web
drwxrwx--- www-data www-data /var/www
Config nginx :
/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name _;
charset utf-8;
location / {
root /var/www/all/;
try_files $uri /index.html index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
When i do this command : sudo ls -l /var/www/all/ , i get :
drwxrwx--- 1 root vboxsf temp_converter
drwxrwx--- 1 root vboxsf myproject
I want to show projects folders under /media/sf_web using Firefox (or another web browser) but it doesn't work.
When i try to connect on this ip , nginx show "File not found" and in error log i see "Primary script unknow".
e.j :
http://192.168.56.50
http://192.168.56.50/myproject/index.php
You haven't set a global root statement, so Nginx will look for PHP files in the default root. You need to move the root statement from inside the location / block into server block scope.
The try_files statement is completely wrong.
Try:
root /var/www/all/;
location / {
try_files $uri $uri/ /index.php;
}
...
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
}

Nginx, PHP - An error occurred. Sorry, the page you are looking for is currently unavailable. Please try again later

I get an error when I access my index.php of my /usr/local/nginx-1.12.2/html.
An error occurred.
Sorry, the page you are looking for is currently unavailable.
Please try again later.
If you are the system administrator of this resource then you should check the error log for details.
Faithfully yours, nginx.
My nginx.conf is this:
# cat conf/nginx.conf:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
}
}
in my /usr/local/php-7.1.16/etc, the php-fpm.conf config is this:
# cat php-fpm.conf
[global]
pid = /usr/local/php-7.1.16/var/run/php-fpm.pid
error_log = /usr/local/php-7.1.16/var/log/php-fpm.log
log_level = notice
[www]
listen = /tmp/php-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 60
pm.start_servers = 30
pm.min_spare_servers = 30
pm.max_spare_servers = 60
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/slow.log
My php-fpm is running. I can use ps -ef | grep php-fpm to check. there are multi processes at there.
my index.php is just a phpinfo():
index.php:
<?php
ini_set("display_errors","On");
error_reporting(E_ALL);
echo phpinfo();
?>
EDIT-1
In my nginx's error.log:
2018/06/25 09:36:02 [error] 12360#0: *13338 connect() failed (111: Connection refused) while connecting to upstream, client: 118.113.137.192, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "101.103.23.5"
EDIT-2
Thanks Dan, I changed the fastcgi_pass 127.0.0.1:9000 to:
fastcgi_pass unix:/tmp/php-cgi.sock;
But I get the File not found. error, when I access the index.php.
You specify a unix socket for fpm to listen:
listen = /tmp/php-cgi.sock
But in nginx you specify an IP address:
fastcgi_pass 127.0.0.1:9000;
Configure either IP only or Unix Socket only:
fastcgi_pass unix:/tmp/php-cgi.sock;
or
listen = 127.0.0.1:9000
Concerning your edit 2:
Try adding this param:
fastcgi_split_path_info ^(.+\.php)(/.+)$;
And also your SCRIPT_FILENAME does not look correct, the folder you specify most probably does not exist (/scripts).
Try this dynamic path:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
You should use the bellow configurations of php in nginx:
location ~ \.php$ {
root html;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
The $document_root$fastcgi_script_name statement is invalid now.

Nginx PHP setup - No input file specified

I know it's a very much known issue. I have tried almost everything but couldn't fix yet.
Intent is to create a blog using php/wordpress with already existing nginx as web server. Nginx is already being used as a web server to a Rails app.
Here's what my nginx.conf looks like
user centos;
worker_processes 1;
events {
worker_connections 1024;
}
http {
passenger_root /home/centos/.rvm/gems/ruby-2.1.4#tripshelf/gems/passenger-5.1.2;
passenger_ruby /home/centos/.rvm/gems/ruby-2.1.4#tripshelf/wrappers/ruby;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
rails_env staging;
server {
listen 80;
server_name xx.xxx.xxx.xx;
location / {
root /data/staging-tiger/current/public/;
passenger_enabled on;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /data/blog/;
index index.php index.html index.htm;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
I followed digital ocean's tutorial on how to install LEMP stack.
After installation, when I hit http://example.com/info.php I get
No input file specified
On further research, I tried:
Setting the right permissions to document root - /data/blog with proper execute permissions to /, /data, /data/blog and /data/blog/info.php
php location block has its own index and root directives.
Nginx is running as centos user. Here's the output of ps aux | grep nginx
root 11510 0.0 0.1 53984 1320 ? Ss 07:06 0:00 nginx: master process /opt/nginx/sbin/nginx
centos 11513 0.0 0.2 54364 2612 ? S 07:06 0:00 nginx: worker process
centos 13471 0.0 0.0 103312 876 pts/1 S+ 07:42 0:00 grep nginx
User and Group are set to centos in /etc/php-fpm.d/www.conf file.
user = centos
group = centos
Running stat on Document root shows
File: /data/blog/info.php
Size: 20 Blocks: 8 IO Block: 4096 regular file
Device: ca01h/51713d Inode: 525709 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 500/ centos) Gid: ( 500/ centos)
Access: 2017-10-19 16:47:54.528000890 +0000
Modify: 2017-10-19 16:47:54.528000890 +0000
Change: 2017-10-20 06:18:28.000001084 +0000
I have been wrapping my head around this but no breakthrough so far. Please help.
Thanks in advance
The guide followed wasn't for WordPress, so the nginx config is probably a little off.
Try amending the PHP block, notice the added fastcgi_param lines.
location ~ \.php$ {
root /data/blog/;
index index.php index.html index.htm;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
If that fails, there might be something you can use from either
https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/
Or the more detailed
https://codex.wordpress.org/Nginx
Also, it should be generating the errors in the nginx log file.
clear && tail -f /var/log/nginx/error.log

File Not Found when running Laravel with Nginx using LaraDock

I am learning Laravel using its tutorial. I installed Laravel and the required stacks using LaraDock.
Nginx returns File not found when I go to the IP where the app is running. Below is my NGINX configuration.
server {
root /var/www/laravel/public/;
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
This is the error from NGINX error log.
2016/08/19 08:53:06 [error] 47#47: *16 FastCGI sent in stderr:
"Primary script unknown" while reading response header from upstream,
client: 192.168.99.1, server: , request: "GET / HTTP/1.1", upstream:
"fastcgi://111.13.0.5:9000", host: "192.168.99.100"
To be sure you have index.php in /var/www/laravel/public/
run command
docker-compose exec nginx ls /var/www/laravel/public/
If you don't have index.php in the folder, probably your project folder mounted wrong.
Did you edit .env, or docker-compose.yml file?
Where you put laradock folder relative to your project folder

Nginx stat() failed (13: Permission Denied)

Okay so there have been some previous posting of this yet no solution fixes my problem.
I have site configured which is just straight up HTML, CSS & JS and I'm trying to add a wordpress site. My config for the wordpress site is as follows.
#######################
server {
listen 80;
root /usr/share/nginx/threadtheatre/wordpress;
index index.php;
server_name threadtheatre.co.uk;
access_log /var/log/nginx/thread.access.log;
error_log /var/log/nginx/thread.error.log;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
This is the error thats in my logs
"/usr/share/nginx/threadtheatre/wordpress/index.php" failed (13: Permission denied), client: 109.155.53.189, server: threadtheatre.co.uk, request: "GET / HTTP/1.1", host: "threadtheatre.co.uk"
nginx is using the nginx user and likewise for php-fpm. The nginx directory and all its sub directories have the following permissions.
drwxrwxr-x. 3 root nginx 4096 Feb 8 18:23 ..
If I browse to threadtheatre.co.uk on the web i get 404.
hope someone can help with this.
Lee.
Do you still have this problem? This explanation worked for me:
https://serverfault.com/a/170263/140684
basically nginx needs to have execution rights on every dir on the path to your app. Hope this helps.
for me this is because selinux enabled, check with
selinuxenabled && echo enabled || echo disabled
if enabled try to disable
nano /etc/sysconfig/selinux
SELINUX=disabled
then
reboot
If nginx is hosted on Fedora or RedHat , change the SELinux policy and allow nginx to serve from the path /home/path/site
chcon -R -t httpd_sys_content_t /home/path/site

Categories