Nginx 403 error with php using php-fpm - php

Basically im trying to set-up and alias for nginx.
Currently I have my localhost server set to: /usr/share/nginx/html and it works fine.
I try to add an internal domain translation: misemestrei.dom to /home/frhec/folder but I get Error 403.
I already tried to change the user ownership to 'http' and also set user permisions to 755 but I still get the same error.
Mi nginx.conf is:
user http;
worker_processes auto;
worker_cpu_affinity auto;
pcre_jit on;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
events {
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
aio threads;
server_tokens off;
charset utf-8;
keepalive_timeout 65;
#Omited localhost server configuration, it's similar#
server {
listen 80;
server_name misemestrei.dom;
client_max_body_size 25M;
location / {
root /home/frhec/folder;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /home/frhec/folder;
}
location ~ \.(php|html|htm)$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
root /home/frhec/folder;
fastcgi_index index.php;
include fastcgi.conf;
}
include sites-enabled/*;
}
}
My /etc/hosts looks like:
#
# /etc/hosts: static lookup table for host names
#
#<ip-address> <hostname.domain.org> <hostname>
127.0.0.1 localhost.localdomain localhost
127.0.0.1 misemestrei.dom misemestrei.dom
::1 localhost.localdomain localhost
# End of file
And the folder looks like:
drwxr-xr-x 4 http http 4096 Aug 12 21:02 .
drwxr-xr-x 4 http http 4096 Aug 11 15:56 ..
-rwxr-xr-x 1 frhec users 61 Aug 11 15:15 composer.json
-rwxr-xr-x 1 frhec users 2492 Aug 11 15:15 composer.lock
drwxr-xr-x 2 frhec users 4096 Aug 12 21:07 .idea
-rwxr-xr-x 1 http http 0 Aug 12 21:02 index.php
-rwxr-xr-x 1 http http 367 Aug 11 15:21 mongodb01.php
drwxr-xr-x 4 frhec users 4096 Aug 11 15:15 vendor
I'm using Antergos (Arch-Linux)
Thanks

I found a solution, all folder must have the executable attribute.
So I applied it to the entire route
sudo chmod +x /home
sudo chmod +x /home/frhec
sudo chmod +x /home/frhec/folder

Related

PHP-fpm file not found error with laravel

I'm trying to put a Laravel application on my server, so I took the base config from the laravel documentation and modified it to use the installed version of php and work with my reverse proxy. But i'm getting a blank page with just the words
File not found
php-fpm log
- - 15/Feb/2020:17:30:54 +0000 "GET /index.php" 404
My Nginx site config
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name events.grocock.email;
include general/ssl.conf;
include general/log.conf;
root /root/events/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
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; }
error_page 404 /index.php;
location ~ \.php$ {
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;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
The contents of the public folder
drwxr-xr-x 5 www-data www-data 4096 Feb 15 20:23 ./
drwxr-xr-x 14 www-data www-data 4096 Feb 15 17:17 ../
drwxrwxr-x 5 www-data www-data 4096 Feb 13 13:59 css/
-rwxrwxr-x 1 www-data www-data 0 Feb 8 14:51 favicon.ico*
-rw-r--r-- 1 www-data www-data 593 Feb 8 14:51 .htaccess
-rwxrwxr-x 1 www-data www-data 1842 Feb 15 17:11 index.php*
drwxrwxr-x 2 www-data www-data 4096 Feb 15 16:58 js/
-rwxrwxr-x 1 www-data www-data 377 Feb 15 16:58 mix-manifest.json*
-rwxrwxr-x 1 www-data www-data 24 Feb 8 14:51 robots.txt*
drwxrwxr-x 3 www-data www-data 4096 Feb 12 03:00 vendor/
-rwxrwxr-x 1 www-data www-data 1194 Feb 8 14:51 web.config*

Php with Nginx - 403 Forbidden

I want to run project yii2(advanced template) with nginx. I use virtualbox with vagrant(ubuntu 16.04, php 5.6).
I have following settings in my Nginx file:
vhost1.conf
server {
listen *:80;
server_name frontend.test;
client_max_body_size 128m;
root /var/www/frontend/web/;
index index.php;
access_log /var/log/nginx/vhost1.access.log;
error_log /var/log/nginx/vhost1.error.log;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ ^/assets/.*\.php$ {
deny all;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $uri =404;
}
location ~* /\. {
deny all;
}
}
I have following structure project with permission:
vagrant#machine1]-[/var/www]-[git master]
$ ls -la frontend/
total 68
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 ./
drwxrwxr-x 1 vagrant vagrant 4096 Jul 9 16:14 ../
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 assets/
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 bootstrap/
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 components/
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 config/
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 controllers/
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 data/
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 helpers/
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 messages/
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 models/
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 modules/
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 runtime/
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 validators/
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 views/
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 web/
drwxrwxr-x 1 vagrant vagrant 4096 Jul 5 14:27 widgets/
nginx error logs output:
2018/07/09 21:42:36 [error] 23865#23865: *1 directory index of "/var/www/frontend/web/" is forbidden, client: 192.168.56.1, server: b2bfrontend.test, request: "GET / HTTP/1.1", host: "b2bfrontend.test"
If I run b2bfrontend.test I get an error - 403 Forbidden
Just fix location from:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
to:
location / {
try_files $uri /index.php$is_args$args;
}
reason: it tries to go $uri/ which is /var/www/frontend/web/ (since it exists) and to do directory indexing which seems like not allowed.
message already says it:
directory index of "/var/www/frontend/web/" is forbidden
I uncommented this line and now it works:
fastcgi_pass unix:/var/run/php5-fpm.sock;
In my case I have Debian 9 so this I changed it a bit:
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
My complete code is:
location ~ \.(php|twig)$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
Your nginx server is most probably running either under user nginx or www-data while your files have vagrant:vagran ownership. Check what's the user under which nginx is running and change the ownership of your files accordingly. Also, if you use php-fpm for php, check your php-fpm configuration and see what user is defined there as well.

Nginx site for PHP-FPM status page returns "File not found"

In /etc/php/7.1/fpm/pool.d/www.conf I have set pm.status_path = /status. And in Nginx I have the following site configuration /etc/nginx/sites-enabled/datadog
server {
listen 80;
server_name localhost;
location ~ ^/(status|ping)$ {
access_log off;
allow 127.0.0.1;
deny all;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
}
I have tested that the status page is correctly setup using
SCRIPT_NAME=/status \
SCRIPT_FILENAME=/status \
REQUEST_METHOD=GET \
cgi-fcgi -bind -connect /run/php/php7.1-fpm.sock
Which returns
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-cache, no-store, must-revalidate, max-age=0
Content-type: text/plain;charset=UTF-8
pool: www
process manager: dynamic
start time: 13/Nov/2017:22:05:44 +0000
start since: 5030
accepted conn: 1789
listen queue: 0
max listen queue: 0
listen queue len: 0
idle processes: 2
active processes: 1
total processes: 3
max active processes: 4
max children reached: 0
slow requests: 0
However, if I try to access the Nginx site using curl http://localhost/status I get the following error.
2017/11/13 23:32:57 [error] 885#885: *35 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /status HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.1-fpm.sock:", host: "localhost"
You are referencing $document_root but there is no root directive in your server block

nginx configuration for Wordpress in subdirectory of Rails app in same server

I keep getting the "File not found" error when I try to access example.com/blog, and in /var/log/nginx/error.log :
FastCGI sent in stderr: "Primary script unknown" while reading
response header from upstream
Here is my nginx configuration:
upstream example {
server unix:/home/deployer/example/shared/tmp/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name example.com;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 503 504 /500;
root /home/deployer/example/current/public;
try_files $uri/index.html $uri.html $uri #example;
location #example {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://example;
}
location /blog {
root /var/www/example_blog;
index index.php;
if (!-e $request_filename) {
rewrite ^(.*)$ /blog/index.php?q=$1 last;
}
location ~ .php(?|$) {
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 80;
server_name assets.example.com;
client_max_body_size 4G;
keepalive_timeout 10;
root /home/deployer/example/current/public;
location = /404.html {
root html;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
Processes of PHP-FPM and nginx:
$ ps aux | grep nginx
root 31590 0.0 0.0 32420 948 ? Ss 16:27 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 31593 0.0 0.1 32868 3596 ? S 16:27 0:00 nginx: worker process
deployer 32052 0.0 0.0 14224 936 pts/0 S+ 16:33 0:00 grep --color=auto nginx
$ ps aux | grep php
root 31656 0.0 1.4 356016 29892 ? Ss 16:27 0:00 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
www-data 31659 0.0 0.2 356016 5880 ? S 16:27 0:00 php-fpm: pool www
www-data 31660 0.0 0.4 356344 8424 ? S 16:27 0:00 php-fpm: pool www
deployer 32059 0.0 0.0 14224 956 pts/0 S+ 16:33 0:00 grep --color=auto php
Permissions on the wordpress directory:
drwxrwsr-x 5 www-data www-data 4096 Jun 1 00:13 example_blog/
Permissions on the files in Wordpress:
drwxrwsr-x 5 www-data www-data 4096 Jun 1 00:13 ./
drwxr-xr-x 10 deployer deployer 4096 Jun 1 16:32 ../
-rw-r--r-- 1 www-data www-data 418 Sep 25 2013 index.php
-rw-r--r-- 1 www-data www-data 19935 Jan 3 02:51 license.txt
-rw-r--r-- 1 www-data www-data 7433 Jan 12 01:46 readme.html
-rw-r--r-- 1 www-data www-data 5447 Sep 28 2016 wp-activate.php
drwxr-sr-x 9 www-data www-data 4096 May 17 05:50 wp-admin/
-rw-r--r-- 1 www-data www-data 364 Dec 19 2015 wp-blog-header.php
-rw-r--r-- 1 www-data www-data 1627 Aug 29 2016 wp-comments-post.php
-rw-r--r-- 1 www-data www-data 3136 Jun 1 00:13 wp-config.php
-rw-r--r-- 1 www-data www-data 2853 Dec 16 2015 wp-config-sample.php
drwxrwsr-x 5 www-data www-data 4096 Jun 1 00:05 wp-content/
-rw-r--r-- 1 www-data www-data 3286 May 25 2015 wp-cron.php
drwxr-sr-x 18 www-data www-data 12288 May 17 05:50 wp-includes/
-rw-r--r-- 1 www-data www-data 2422 Nov 21 2016 wp-links-opml.php
-rw-r--r-- 1 www-data www-data 3301 Oct 25 2016 wp-load.php
-rw-r--r-- 1 www-data www-data 33939 Nov 21 2016 wp-login.php
-rw-r--r-- 1 www-data www-data 8048 Jan 11 13:15 wp-mail.php
-rw-r--r-- 1 www-data www-data 16255 Apr 7 02:23 wp-settings.php
-rw-r--r-- 1 www-data www-data 29896 Oct 19 2016 wp-signup.php
-rw-r--r-- 1 www-data www-data 4513 Oct 15 2016 wp-trackback.php
-rw-r--r-- 1 www-data www-data 3065 Sep 1 2016 xmlrpc.php
In /etc/php/7.0/fpm/pool.d/www.conf:
user = www-data
group = www-data
listen.owner = nginx
listen.group = nginx
;listen.mode = 0660
Have been googling and tried various ways, but still couldn't get it passed this error.
The problem is that location /blog { root /var/www/example_blog; ... places the files in /var/www/example_blog/blog/.
root is applicable only when the file path is constructed by concatenating the $document_root with the URI. Otherwise you need to rewrite the URI or use an alias directive. See this document for details.
The alias directive can be implemented like this:
location = /blog { rewrite ^ /blog/ last; }
location ^~ /blog/ {
alias /var/www/example_blog/;
index index.php;
if (!-e $request_filename) {
rewrite ^(.*)$ /blog/index.php?q=$1 last;
}
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
Use the ^~ modifier to avoid any ambiguity with other regular expression location blocks at the same level. See this document for details.
To avoid problems with URIs such as /blogx, use a trailing / on both the location and the alias. And add an exact match location to handle /blog.

Nginx Centos7 Laravel 5 can't write to log files [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
I got a brand new Centos 7 server and would like to install laravel + nginx
I added Remi repo for PHP5.6
Also added epel
yum update
yum install nmap rsync nano wget curl
yum install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
nano /etc/yum.repos.d/remi.repo
yum install nginx
systemctl start nginx
systemctl enable nginx
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
#PHP
yum install php php-mysql php-fpm php-dom php-mcrypt php-mbstring
nano /etc/php.ini
cgi.fix_pathinfo=0
nano /etc/php-php.d/www.conf
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nobody
listen.group = nobody
user = nginx
group = nginx
systemctl start php-fpm
systemctl enable php-fpm
The NGINX server is working, and phpinfo(); is looking good too.
inside /etc/nginx/nginx.conf:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
#root /usr/share/nginx/html;
root /usr/share/nginx/html/lv/public;
index index.php index.html index.htm
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
Inside /usr/share/nginx/html/lv I have installed alaravel 5 project via git.
[root#213 lv]# ls -la
total 164
drwxr-xr-x. 11 nginx nginx 4096 Jan 28 02:19 .
drwxr-xr-x. 3 root root 4096 Jan 28 02:17 ..
drwxrwxr-x. 15 nginx nginx 4096 Oct 21 10:44 app
-rw-rw-r--. 1 nginx nginx 1635 Aug 19 08:57 artisan
drwxrwxr-x. 2 nginx nginx 39 Nov 28 01:22 bootstrap
-rw-rw-r--. 1 nginx nginx 1007 Aug 19 08:57 composer.json
-rw-rw-r--. 1 nginx nginx 107281 Aug 19 08:57 composer.lock
drwxrwxr-x. 2 nginx nginx 4096 Aug 19 08:57 config
drwxrwxr-x. 4 nginx nginx 52 Aug 19 08:57 database
-rw-rw-r--. 1 nginx nginx 503 Aug 19 08:57 gulpfile.js
-rw-rw-r--. 1 nginx nginx 79 Aug 19 08:57 package.json
-rw-rw-r--. 1 nginx nginx 87 Aug 19 08:57 phpspec.yml
-rw-rw-r--. 1 nginx nginx 729 Aug 19 08:57 phpunit.xml
drwxrwxr-x. 4 nginx nginx 4096 Nov 28 01:03 public
-rw-rw-r--. 1 nginx nginx 1724 Aug 19 08:57 readme.md
drwxrwxr-x. 5 nginx nginx 42 Aug 19 08:57 resources
-rw-rw-r--. 1 nginx nginx 561 Aug 19 08:57 server.php
drwxrwxrwx. 6 nginx nginx 76 Aug 19 08:57 storage
drwxrwxr-x. 2 nginx nginx 47 Aug 19 08:57 tests
drwxrwxr-x. 29 nginx nginx 4096 Aug 19 08:57 vendor
[root#213 lv]# cd storage
[root#213 storage]# ls -la
total 12
drwxrwxr-x. 6 nginx nginx 76 Aug 19 08:57 .
drwxr-xr-x. 11 nginx nginx 4096 Jan 28 02:19 ..
drwxrwxr-x. 2 nginx nginx 23 Aug 19 08:57 app
drwxrwxr-x. 3 nginx nginx 21 Aug 19 08:57 documents
drwxrwxr-x. 5 nginx nginx 62 Aug 19 08:57 framework
-rwxrwxr-x. 1 nginx nginx 11 Aug 19 08:57 .gitignore
drwxrwxrwx. 2 nginx nginx 4096 Jan 27 14:01 logs
logs is empty.
I still get the following the error:
PHP message: PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message 'The stream or file "/usr/share/nginx/html/lv/storage/logs/laravel-2016-01-28.log" could not be opened: failed to open stream: Permission denied' in /usr/share/nginx/html/lv/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:95
Stack trace:
#0 /usr/share/nginx/html/lv/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php(88): Monolog\Handler\StreamHandler->write(Array)
#1 /usr/share/nginx/html/lv/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\Handler\RotatingFileHandler->write(Array)
#2 /usr/share/nginx/html/lv/vendor/monolog/monolog/src/Monolog/Logger.php(269): Monolog\Handler\AbstractProcessingHandler->handle(Array)
#3 /usr/share/nginx/html/lv/vendor/monolog/monolog/src/Monolog/Logger.php(545): Monolog\Logger->addRecord(400, 'exception 'Symf...', Array)
#4 /usr/share/nginx/html/lv/vendor/larav
So I ran whoami.php which has `echo shell_exec('whoami');' in it, and the user is nginx.
So I don't get the problem
I found the problem
SELinux was turned on:
nano /etc/sysconfig/selinux
set to disabled

Categories