File Not Found when running Laravel with Nginx using LaraDock - php

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

Related

open() "directory/to/project" failed (13: Permission denied) nginx

I am using the default config with Nginx installed on my manjaro machine. I just added some simple configurations down below.
nginx.conf:
user http;
worker_processes auto;
worker_cpu_affinity auto;
.....
http{
.....
server {
listen 9000;
server_name localhost;
root /usr/share/nginx/html/exam;
location / {
index index.php index.html index.htm;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
if (!-e $request_filename){
rewrite ^/(.+)$ /index.php?url=$1 break;
}
}
location /. {
return 404;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_read_timeout 864000;
}
location ~ /\.ht {
deny all;
}
}
....
}
since the user for Nginx is http, I also changed the ownership of the folder, subfolders and files as HTTP with:
chmod -R http:http exam/
it looks like this:
1838248 lrwxrwxrwx 1 http http 42 Eyl 13 17:42 exam
but still gives 403 Forbidden on browser with this error:
2021/09/13 17:49:22 [error] 493923#493923: *4 open() "/usr/share/nginx/html/exam/index.php" failed (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET /exam HTTP/1.1", host: "localhost:9000"
I have tried every solution I found, but it did not work.
I also tried simple PHP files that runs like "phpinfo();" even they are not working.
I moved the project file under /srv/http and redirect the root in the conf file to it. Now it is working.

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.

Running Wordpress/NGINX from different path

Im learning NGINX, so any help is really appreciated.
I have the frontend of a website running as the root of mysite.com, and now I want to run wordpress from mysite.com/blog.
My file structure is:
/srv/mysite/frontend
/srv/mysite/wordpress
this is the error i've been getting from the nginx logs
2020/03/29 00:09:03 [error] 23049#23049: *39 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: XXXXXXX, server: www.mysite.com, request: "GET /api HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.2-fpm.sock:", host: "mysite.com"
and this is my nginx config file so far
listen 80 default_server;
server_name www.mysite.com mysite.com;
charset utf-8;
location ^~ /blog {
root /srv/mysite/wordpress;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location / {
root /srv/mysite/frontend/dist;
try_files $uri /index.html;
}
}
This error message shows either the wrong path of the sock file or the permissions. Make sure the php sock file exists in the path /run/php/php7.2-fpm.sock and change the permissions of the file.
For Debian
chown -R wwww-data:www-data /run/php/php7.2-fpm.sock
For Rhel
chown -R nginx:nginx /run/php/php7.2-fpm.sock
Also, you can try this config.
location /blog {
root /srv/mysite/wordpress;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

How to configure Nginx for Ember.js + Wordpress?

The scenario is that I'd like to use Wordpress as a backend API provider for our Ember.js frontend app.
The Ember.js frontend needs to be served from the root, and the Wordpress instance ideally would be reachable by going to a subdirectory. So for example on localhost it would be http://localhost and http://localhost/wordpress
On the disk the two are deployed in /srv/http/ember and /srv/http/wordpress respectively.
I was trying to assemble the configuration going by the example on the Nginx site:
https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/
The config:
http {
upstream php {
server unix:/run/php-fpm/php-fpm.sock;
}
server {
listen 80;
server_name localhost;
root /srv/http/ember;
index index.html;
try_files $uri $uri/ /index.html?/$request_uri;
location /wordpress {
root /srv/http/wordpress;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
fastcgi_split_path_info ^(/wordpress)(/.*)$;
}
}
}
However this is obviously not the correct solution.
Upon trying to access the address http://localhost/wordpress/index.php I get the following in the logs:
2016/05/01 17:50:14 [error] 4332#4332: *3 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /wordpress/index.php HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm/php-fpm.sock:", host: "localhost"
The recipe isn't clear about where to put the root directive for the location of wordpress. I also tried with adding index index.php, which doesn't help either.
(Serving the Ember app works fine.)
From your question it seems that the location ~ \.php$ block is used by WordPress alone. However, it needs a root of /srv/http in order to find the script files for URIs beginning with /wordpress under the local path /srv/http/wordpress.
As there are two locations which both use the same WordPress root, it is possibly cleaner to make /srv/http the default (that is, inherited from the server block) and move root /srv/http/ember; into a separate location / block.
server {
listen 80;
server_name localhost;
root /srv/http;
location / {
root /srv/http/ember;
index index.html;
try_files $uri $uri/ /index.html?/$request_uri;
}
location /wordpress {
index index.php;
try_files $uri $uri/ /wordpress/index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php;
}
}
Notice that the default URI in location /wordpress is /wordpress/index.php and not /index.php as you originally had.
I have explicitly set SCRIPT_FILENAME as it may or may not appear in your fastcgi.conf file.
fastcgi_split_path_info has been removed as it is unnecessary in your specific case, and I think it would actually break WordPress the way you had it.

502 Bad Gateway with PHP7 and Nginx 1.9.9

I'm getting a 502 Bad Gateway error with PHP7 and nginx 1.9.9 installed on Ubuntu 14.04 when I try to access any .php files. .html files load as expected.
I've updated the default.conf to:
server {
listen 80 default_server;
root /usr/share/nginx/html;
index index.html index.htm index.php;
server_name localhost;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
I've updated php.ini with cgi.fix_pathinfo = 0 and then rebooted the server, and am still getting the 502 error with all .php files. I have checked to ensure php7.0-fpm.sock is installed and in the proper location.
This is the error I'm getting from the nginx log 2016/01/19 19:14:54 [error] 1466#1466: *1 open() "/usr/share/nginx/html/xmlrpc.php" failed (2: No such file or directory), client: 85.159.237.13, server: localhost, request: "POST /xmlrpc.php HTTP/1.0", host: "my.ip.address"
I've searched for the answer for quite a while and I'm out of ideas. Does anyone have any suggestions?
This is mostly because your nginx and php7.0-fpm were not run under the same user. Edit nginx.conf and change "user nginx" to "user www-data"
By the way, "client: 85.159.237.13", that was a script boy, I think.

Categories