403 Forbidden when file permissions are correct - php

I'm trying to create a server on nginx with multiple laravel (a php framework) sites under the same subdomain labs.rasouza.com.br/cdm, labs.rasouza.com.br/app2, labs.rasouza.com.br/app3, etc.
nginx and php are properly configured to run as web user, and my root folder is under /home/web:
drwxrwxrwx 5 web web 4.0K Aug 18 13:56 cdm
-rwxr-xr-x 1 web web 24 Aug 12 02:53 info.php
drwxr-xr-x 13 web web 4.0K Aug 12 04:15 owncloud
drwxr-xr-x 2 web web 4.0K Aug 18 15:23 tmp
My sites-available are configured like this:
server {
listen 80;
root /home/web;
index index.php index.html index.htm;
server_name labs.rasouza.com.br;
access_log /home/web/tmp/labs.access.log;
error_log /home/web/tmp/labs.error.log;
location / {
try_files $uri $uri/ /index.html;
}
location ~ ^/cdm/(.+\.php)$ {
alias /home/web/cdm/current/public/;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
location ~ \.php$ {
try_files $uri $uri/ =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Whenever I try to access labs.rasouza.com.br/cdm I get the 403 Forbidden page and the error log is: directory index of "/home/web/cdm/" is forbidden. When I try to access info.php it goes ok. File permissions are also ok, web user owns everything
What am I doing wrong?

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 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

Migrating Yii2 from Apache to Nginx - failed on backend app

I try to move my Yii2 app from using apache to nginx. I'm using Yii2 advanced template on a single domain, so I follow the solution from this link creating a symlink from frontend/web to the backend/web. This solution works fine when I'm using apache, but when I try to use nginx I can't open the backend app.
The frontend app works fine, but when I try to open the backend app using app.local/belakang, it is redirected to app.local/belakang/site/login, but still stuck in the frontend app and therefore giving a [yii\web\HttpException:404] exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "belakang/site/login".'
Here is the symlinks that I have on the frontend/web directory :
lrwxrwxrwx 1 vagrant vagrant 17 Jun 24 14:03 belakang -> ../../backend/web
and this is the nginx vhost config that I use:
server {
charset utf-8;
client_max_body_size 128M;
listen 80; ## listen for ipv4
server_name app.local;
root /var/www/html/frontend/web;
index index.php;
location / {
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
}
# deny accessing php files for the /assets directory
location ~ ^/assets/.*\.php$ {
deny all;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $uri =404;
}
location ~* /\. {
deny all;
}
}
Can anyone help so that I can open the backend app from app.local/belakang ?
Fix it myself by adding config below
location ^~ /belakang {
try_files $uri $uri/ /belakang/index.php$is_args$args;
location ~ /\.(ht|svn|git) {
deny all;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php5-fpm.sock;
try_files $uri =404;
}
}

net::ERR_CONNECTION_REFUSED with Nginx and Laravel 5

I have just installed a fresh copy of Laravel 5 into /var/www.
When I browse to the server I get net::ERR_CONNECTION_REFUSED.
My Nginx config (default) is:
server {
listen 80;
root /var/www/public;
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
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;
}
}
Any idea what I'm doing wrong?
I am also confused about sites-enabled and sites-available. Where should default actually go?
I have moved default from sites-available to sites-enabled and I am now getting a 403 with "Access denied".
You probably got net::ERR_CONNECTION_REFUSED because you hadn't told nginx what port to listen on (note the listen 80 line in your config file), so you were trying to a port that wasn't open - hence the connection refused error.
As for sites-available vs sites-enabled, that's a Debian/Ubuntu thing to make sites easier to manage - you can have many sites configured in sites-available, but only run specific ones by adding a link in sites-enabled pointing at the respective config file in sites-available.
As an example, my sites-enabled folder has
lrwxrwxrwx 1 root root 40 Feb 8 07:53 site.net -> /etc/nginx/sites-available/site.net
No copying, just a link to sites-available.
For your 403 error, look in your error log for what precisely is failing. It should be located at /var/log/nginx/error.log - look for error_log in your main conf file to get the exact location.

Can't get Baikal running in a subdirectory

I try to install Baïkal on a dedicated host with the "regular package". I am using Nginx as webserver but I can't get it running. The official docs are only dedicated to run Baikal on a subdomain (http://baikal.mydomain.com) instead in a subdirectory (http://mydomain.com/baikal). When I open http://mydomain.com/baikal/card.php/addressbooks/IstMe/default/ I only get a "File not found". Any help would be appreciated.
My nginx.conf looks like this one:
location /baikal {
alias /usr/share/webapps/baikal/html;
index index.php;
rewrite ^/.well-known/caldav /cal.php redirect;
rewrite ^/.well-known/carddav /card.php redirect;
location ~ ^/baikal/(.+\.php)$ {
alias /usr/share/webapps/baikal/html/$1;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
}
location ~* /baikal/(\.ht|Core|Specific) {
deny all;
return 404;
}
I'd got the same problem. The folowing very simple instance configuration from this article worked great for me:
server {
listen [::]:443 ssl;
server_name yourdomain.tld;
root /usr/share/nginx/baikal/html;
index index.php;
ssl_certificate server.crt;
ssl_certificate_key server.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
rewrite ^/.well-known/caldav /cal.php redirect;
rewrite ^/.well-known/carddav /card.php redirect;
charset utf-8;
location ~ /(\.ht|Core|Specific) {
deny all;
return 404;
}
location ~ ^(.+\.php)(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
quite an old post but i've been redirected here searching for a solution to the very same problem ^^
the're's a post about this issue and a possible solution.
here is the configuration for NGINX (this is cut&paste, it is not my work):
location ^~ /baikal { # triggers location of baikal installation, and stop looking for other matches
index index.php;
charset utf-8;
# curiosity killed the cat
location ~ ^/baikal/(?:\.ht|Core|Specific) {
deny all;
}
# this corresponds to the recommended regex for matching php files
# and piping it to php-fpm
location ~ ^(.+\.php)(.*) {
try_files $fastcgi_script_name =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
# case insensitive matching of static files for maximum caching time
location ~* \.(?:jpg|gif|ico|png|css|js|svg)$ {
expires max; add_header Cache-Control public;
}
}
i use apache so i had no way to test it but this is the starting point i'm using to solve the problem on my web server.
Have you tried to create 2 symlinks from root to html directory like that :
cd /var/www/baikal
sudo ln -s html/card.php card.php
sudo ln -s html/cal.php cal.php
Which should gave that result :
ls -lah /var/www/baikal
total 72K
drwxrwxr-x 6 www-data www-data 4,0K nov. 19 12:40 .
drwxr-xr-x 25 www-data www-data 4,0K nov. 19 12:54 ..
lrwxrwxrwx 1 root root 12 nov. 19 12:40 cal.php -> html/cal.php
lrwxrwxrwx 1 root root 13 nov. 19 12:40 card.php -> html/card.php
This seems to work for my installation.

Categories