CentOS, Nginx: 403 Forbidden in Laravel's folder - php

I use laravel new test to establish the Laravel project and move all directories into /usr/share/nginx/html/, the default root directory of Nginx.
In order to check if the config of nginx is correct, I simply the config, trun on autoindex, and put index,html into /usr/share/nginx/html/test/public/, the location of index.php of laravel.
defaul.conf of nginx
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
root /usr/share/nginx/html/test;
index index.html index.htm index.php;
location / {
autoindex on;
}
# 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;
}
}
When I access the index.html by localhost/public/, nginx returns me 403. I copy index.html into a new folder named /usr/share/nginx/html/test/public_b, and try to access the new index.html by localhost/public_b/. Nginx returns me the correct content! It is like a miracle. The stats of my directories as the follows.
$ll /usr/share/html/test
total 432
drwxrwxr-x. 6 nginx nginx 84 Feb 26 15:39 app
-rw-rw-r--. 1 nginx nginx 1686 Feb 26 15:39 artisan
drwxrwxr-x. 3 nginx nginx 34 Feb 26 15:39 bootstrap
-rw-rw-r--. 1 nginx nginx 1512 Feb 26 15:39 composer.json
-rw-rw-r--. 1 nginx nginx 145089 Feb 26 15:39 composer.lock
drwxrwxr-x. 2 nginx nginx 247 Feb 26 15:39 config
drwxrwxr-x. 5 nginx nginx 72 Feb 26 15:39 database
-rwxrwxr-x. 1 nginx nginx 13 Feb 26 17:46 index.html
-rw-rw-r--. 1 nginx nginx 1150 Feb 26 15:39 package.json
-rw-rw-r--. 1 nginx nginx 1040 Feb 26 15:39 phpunit.xml
drwxr-xr-x. 4 root root 116 Feb 26 18:05 public
drwxr-xr-x. 2 root root 24 Feb 26 17:50 public_c
drwxrwxr-x. 5 nginx nginx 45 Feb 26 15:39 resources
drwxrwxr-x. 2 nginx nginx 75 Feb 26 16:50 routes
-rw-rw-r--. 1 nginx nginx 563 Feb 26 15:39 server.php
drwxr-xr-x. 5 nginx nginx 46 Feb 26 15:39 storage
drwxrwxr-x. 4 nginx nginx 83 Feb 26 15:39 tests
drwxrwxr-x. 37 nginx nginx 4096 Feb 26 15:40 vendor
-rw-rw-r--. 1 nginx nginx 549 Feb 26 15:39 webpack.mix.js
-rw-rw-r--. 1 nginx nginx 258941 Feb 26 15:39 yarn.lock
$ ll /usr/share/nginx/html/test/public
total 12
drwxrwxr-x. 2 nginx nginx 21 Feb 26 15:39 css
-rw-rw-r--. 1 nginx nginx 0 Feb 26 15:39 favicon.ico
-rwxr-xr-x. 1 root root 13 Feb 26 17:49 index.html
-rw-rw-r--. 1 nginx nginx 1823 Feb 26 15:39 index.php
drwxrwxr-x. 2 nginx nginx 20 Feb 26 15:39 js
-rw-rw-r--. 1 nginx nginx 24 Feb 26 15:39 robots.txt
$ ll /usr/share/nginx/html/test/public_c/
total 4
-rwxr-xr-x. 1 root root 13 Feb 26 17:50 index.html
I have read a lot of relative questions and pages, but nothing help against this problem. What is wrong in my actions or settings?
Update
Thanks #Adam Kozlowski. I tried chmod 777 public -R and changing owner to both of root and nginx, but the problem was not solved.

I found the problem: SELinux blocked Nginx to access the folders.
$ ls /usr/share/nginx/html/test/ -Z
drwxr-xr-x. root root unconfined_u:object_r:config_home_t:s0 public
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 public_c
Only the files with the tag, httpd_sys_content_t, can be accessed by HTTP.
Command:
chcon -R -t httpd_sys_content_t /usr/share/nginx/html/test
And I also need to add httpd_sys_rw_content_t to /usr/share/nginx/html/test/storage to let Laravel work.
Command:
chcon -R -t httpd_sys_rw_content_t/usr/share/nginx/html/test/storage
Referring Permissions Issue with Laravel on CentOS

Set proper permission for directory, as a root user:
chmod a+rwx directory-name -R

Related

Not Found from php:apache (run without a Dockerfile)

Can not serve even a static files from this container. What I'm doing wrong?
I'm running
docker run -d -p 8081:80 --name test -v "$PWD":/var/www/html php:apache
and reciving the next output trying to request any file from $PWD
Not Found
The requested URL was not found on this server.
Apache/2.4.51 (Debian) Server at 0.0.0.0 Port 8081
the $PWD content gets inside a container, but is not served
docker exec -it test ls -l /var/www/html
total 44
-rw-r--r-- 1 root root 65 Sep 22 12:59 checkserver.php
-rw-r--r-- 1 root root 16093 Sep 22 12:59 crest.php
-rw-r--r-- 1 root root 1024 Sep 22 12:59 crestcurrent.php
-rw-r--r-- 1 root root 217 Oct 14 08:45 index.html
-rw-r--r-- 1 root root 516 Sep 22 12:59 index.php
-rw-r--r-- 1 root root 488 Sep 22 12:59 install.php
-rw-r--r-- 1 root root 650 Sep 22 12:59 settings.php
-rw-r--r-- 1 root root 5 Oct 13 17:32 test.txt
I used nginx as a reverse proxy before an Apache container. The problem was in nginx configuration. It started to work after I added the next lines to the configuration.
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://0.0.0.0:8081;
}

WordPress running within Docker instance, unable to write to file

I am completely new to docker. Just trying to run WordPress within a docker instance to learn more about it.
./public_html/ - has WordPress files and from the domain, I am able to access the installer. However the WordPress installer cant update wp-config.php within docker - as below error.
Unable to write to wp-config.php file.
version: '3'
services:
web:
image: webdevops/php-apache:7.2
container_name: web
restart: always
environment:
- PHP_DISPLAY_ERRORS=1
- PHP_MEMORY_LIMIT=2048M
- PHP_MAX_EXECUTION_TIME=300
- PHP_POST_MAX_SIZE=500M
- PHP_UPLOAD_MAX_FILESIZE=256M
volumes:
- ./public_html/:/app
ports:
- "80:80"
- "443:443"
links:
- mysql
mysql:
image: mariadb:10
container_name: mysql
restart: always
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test
volumes:
- db-data:/var/lib/mysql
phpmyadmin:
container_name: phpmyadmin
restart: always
image: phpmyadmin/phpmyadmin:latest
# environment:
# - MYSQL_ROOT_PASSWORD=root
# - PMA_USER=root
# - PMA_PASSWORD=root
ports:
- "8080:80"
links:
- mysql:db
depends_on:
- mysql
volumes:
db-data:
external: false
Now WordPress allow me to create the config.php. installation went on. I just tried to upload a plugin to test and I get below error. which I anticipated.
Unable to create directory wp-content/uploads/2021/02. Is its parent
directory writable by the server?
I logged into the docker "docker exec -it 51af15ed3678 bash" to check file permission.
root#51af15ed3678:/# ls -lart
total 104
drwxr-xr-x 2 root root 4096 Nov 22 12:37 boot
drwxr-xr-x 2 root root 4096 Dec 9 23:22 srv
drwxr-xr-x 2 root root 4096 Dec 9 23:22 mnt
drwxr-xr-x 2 root root 4096 Dec 9 23:22 media
drwxr-xr-x 2 root root 4096 Dec 9 23:22 lib64
drwx------ 2 root root 4096 Feb 27 22:12 entrypoint.d
lrwxrwxrwx 1 root root 28 Feb 27 22:12 entrypoint.cmd -> /opt/docker/bin/entrypoint.d
lrwxrwxrwx 1 root root 29 Feb 27 22:12 entrypoint -> /opt/docker/bin/entrypoint.sh
drwxr-xr-x 1 root root 4096 Feb 27 22:12 home
drwx------ 1 root root 4096 Feb 27 22:31 root
drwxr-xr-x 1 root root 4096 Feb 27 22:36 sbin
drwxr-xr-x 1 root root 4096 Feb 27 22:36 lib
drwxr-xr-x 1 root root 4096 Feb 27 22:36 bin
drwxr-xr-x 1 root root 4096 Feb 27 22:36 var
drwxr-xr-x 1 root root 4096 Feb 27 22:36 usr
drwxr-xr-x 1 root root 4096 Feb 27 22:36 opt
drwxr-xr-x 1 root root 4096 Feb 28 13:36 etc
-rwxr-xr-x 1 root root 0 Feb 28 13:36 .dockerenv
dr-xr-xr-x 197 root root 0 Feb 28 13:36 proc
dr-xr-xr-x 13 root root 0 Feb 28 13:36 sys
lrwxrwxrwx 1 root root 12 Feb 28 13:36 docker.stdout -> /proc/1/fd/1
lrwxrwxrwx 1 root root 12 Feb 28 13:36 docker.stderr -> /proc/1/fd/2
srwx------ 1 root root 0 Feb 28 13:36 .supervisor.sock
drwxr-xr-x 5 root root 360 Feb 28 13:36 dev
drwxr-xr-x 1 root root 4096 Feb 28 13:36 run
drwxr-xr-x 5 root root 4096 Feb 28 17:24 app
drwxr-xr-x 1 root root 4096 Feb 28 17:28 ..
drwxr-xr-x 1 root root 4096 Feb 28 17:28 .
root#51af15ed3678:/# cd app
root#51af15ed3678:/app# ls -lart
total 32876
-rwxr-xr-x 1 root root 3300 Feb 6 2020 wp-load.php
-rwxr-xr-x 1 root root 2496 Feb 6 2020 wp-links-opml.php
-rwxr-xr-x 1 root root 2913 Feb 6 2020 wp-config-sample.php
-rwxr-xr-x 1 root root 351 Feb 6 2020 wp-blog-header.php
-rwxr-xr-x 1 root root 405 Feb 6 2020 index.php
-rwxr-xr-x 1 root root 19915 Feb 12 2020 license.txt
-rwxr-xr-x 1 root root 8509 Apr 14 2020 wp-mail.php
-rwxr-xr-x 1 root root 3236 Jun 8 2020 xmlrpc.php
-rwxr-xr-x 1 root root 7278 Jun 26 2020 readme.html
-rwxr-xr-x 1 root root 7101 Jul 28 2020 wp-activate.php
-rwxr-xr-x 1 root root 3939 Jul 30 2020 wp-cron.php
-rwxr-xr-x 1 root root 31337 Sep 30 21:54 wp-signup.php
-rwxr-xr-x 1 root root 4747 Oct 8 21:15 wp-trackback.php
-rwxr-xr-x 1 root root 2328 Oct 8 21:15 wp-comments-post.php
-rwxr-xr-x 1 root root 49831 Nov 9 10:53 wp-login.php
-rwxr-xr-x 1 root root 20975 Nov 12 14:43 wp-settings.php
drwxr-xr-x 25 root root 12288 Feb 22 15:10 wp-includes
drwxr-xr-x 4 root root 4096 Feb 22 15:10 wp-content
drwxr-xr-x 9 root root 4096 Feb 22 15:10 wp-admin
-rwxr-xr-x 1 root root 16712083 Feb 22 15:10 latest.zip.1
-rwxr-xr-x 1 root root 16712083 Feb 22 15:10 latest.zip
-rw-r--r-- 1 root root 3181 Feb 28 17:24 wp-config.php
drwxr-xr-x 5 root root 4096 Feb 28 17:24 .
drwxr-xr-x 1 root root 4096 Feb 28 17:28 ..
root#51af15ed3678:/app#
Any clue will be a great help. Thank you.
Regards,
Dwija
The image is running the daemon under user application (UID 1000; GID 1000) so make sure all files are owned by the user application not root

Where can I find php-fpm.log and php-fpm.conf on my server?

I think I am having problems with the max children limit for php-fpm. I am trying to find something in the logs. Where can I find php-fpm.log on my server? Running phpinfo() shows this:
I cannot find anything related to php-fpm in /var/log/.
I am also trying to find the php-fpm.conf that is mentioned at https://www.php.net/manual/en/install.fpm.configuration.php. Thank you.
UPDATE 1: I found at https://github.com/rlerdorf/php7dev/issues/48 that it asks to edit /usr/local/etc/php-fpm.d/www.conf. In my case, I cannot find that file:
root#jai [/usr/local/etc]# pwd
/usr/local/etc
root#jai [/usr/local/etc]# ls -al
total 8
drwxr-xr-x. 2 root root 4096 Sep 23 2011 ./
drwxr-xr-x. 15 root root 4096 Apr 7 15:02 ../
Do I need to create it at /usr/local/etc/php-fpm.d/www.conf?
UPDATE 2: I see I have a folder called ApachePHPFPM but the file inside (default_accounts_to_fpm) is empty:
root#jai [/var/cpanel/ApachePHPFPM]# pwd
/var/cpanel/ApachePHPFPM
root#jai [/var/cpanel/ApachePHPFPM]# ls -al
total 16
drwxr-xr-x 2 root root 4096 Mar 26 04:44 ./
drwx--x--x 108 root root 12288 Apr 17 13:44 ../
-rw-r--r-- 1 root root 0 Mar 26 04:44 default_accounts_to_fpm
root#jai [/var/cpanel/ApachePHPFPM]#
UPDATE 3: My server had this file:
/usr/local/cpanel/etc/php-fpm.conf
The file contains this:
; Error log file
; Default Value: /var/log/php-fpm.log
error_log = /usr/local/cpanel/logs/php-fpm/error.log
That means that my error logs for php-fpm are located in this file: /usr/local/cpanel/logs/php-fpm/error.log.
I navigated to /usr/local/cpanel/logs/php-fpm/ but I could not find error.log:
root#jai [/usr/local/cpanel/etc]# cd /usr/local/cpanel/logs/php-fpm/
root#jai [/usr/local/cpanel/logs/php-fpm]# ls -al
total 8
drwx------ 2 root root 4096 Mar 30 2018 ./
drwx--x--x 6 root root 4096 Apr 16 15:24 ../
root#jai [/usr/local/cpanel/logs/php-fpm]#
In /opt/cpanel/ea-php73/root/etc/php-fpm.conf I found this line:
error_log = /opt/cpanel/ea-php73/root/usr/var/log/php-fpm/error.log
I see the error log files there:
root#jai [/opt/cpanel/ea-php73/root/usr/var/log/php-fpm]# pwd
/opt/cpanel/ea-php73/root/usr/var/log/php-fpm
root#jai [/opt/cpanel/ea-php73/root/usr/var/log/php-fpm]# ls -al
total 151456
drwxrwx--- 2 nobody root 4096 Apr 14 17:36 ./
drwxr-xr-x 3 root root 4096 Mar 26 04:42 ../
-rw------- 1 root root 31536068 Apr 17 17:05 error.log
-rw------- 1 root root 32102325 Mar 29 03:26 error.log-20200329
-rw------- 1 root root 51453659 Apr 5 03:36 error.log-20200405
-rw------- 1 root root 39966549 Apr 12 02:55 error.log-20200412
root#jai [/opt/cpanel/ea-php73/root/usr/var/log/php-fpm]#

Nginx virtual hosts not working

I recorded a video https://youtu.be/dmezVzASr5A
I have:
linux ubuntu 16.04 (Lubuntu)
nginx 1.10
php7.0-fpm
php7.0
mysql 5.7
I did:
created domain /etc/hosts (test2.test)
created virtual hosts withing sites-available folder (test2.test file)
restarted, reloaded nginx server (sudo service nginx stop...restart...reload)
created symlink ln -s /etc/nginx/sites-available/test2.test /etc/nginx/sites-enabled/
symlink proof:
dima#dima-Lenovo-G50-30:~/www/nginx/test2.ru$ ls -ls /etc/nginx/sites-enabled
total 0
0 lrwxrwxrwx 1 root root 34 май 21 16:33 default -> /etc/nginx/sites-available/default
0 lrwxrwxrwx 1 root root 37 май 21 16:44 test2.test -> /etc/nginx/sites-available/test2.test
Problem is when i try to open in browser http://test2.test, i get:
404 Not Found
nginx/1.10.0 (Ubuntu)
This files are not opening:
index.html
test.html
index.php
within /var/www/html i have info.php with content <?php echo phpinfo(); ?>. http://localhost/info.php successfully displays php info.
My permissions:
dima#dima-Lenovo-G50-30:~/www/nginx/test2.ru$ ls -la
total 44
drwxr-xr-x 2 www-data www-data 4096 май 21 16:59 .
drwxrwxr-x 4 dima dima 4096 май 21 13:14 ..
-rw-rw-r-- 1 www-data www-data 111 фев 21 13:34 index.html
-rw-rw-r-- 1 www-data www-data 26 май 21 13:15 index.php
-rwxr-xr-x 1 www-data www-data 5 май 21 16:59 test.html
dima#dima-Lenovo-G50-30:~/www/nginx/test2.ru$
My virtual hosts:
server {
listen 80;
listen [::]:80;
server_name test2.test;
root /home/dima/www/nginx/test2.ru;
index index.html;
location / {
try_files $uri $uri/ =404;
#autoindex on;
}
}
nginx logs:
error.log.1:
many errors like:
2016/05/26 08:46:28 [crit] 1066#1066: *1 stat() "/home/dima/www/nginx/test2.ru/" failed (13: Permission denied), client: 127.0.0.1, server: test2.test, request: "GET / HTTP/1.1", host: "test2.test"
ls -la:
dima#dima-Lenovo-G50-30:~/www/nginx$ ls -la
total 16
drwxrwxr-x 4 dima dima 4096 май 21 13:14 .
drwxrwxr-x 10 dima dima 4096 май 21 12:52 ..
drwxrwxr-x 5 dima dima 4096 май 21 12:52 test1.ru
drwxr-xr-x 2 www-data www-data 4096 май 21 16:59 test2.ru
dima#dima-Lenovo-G50-30:~/www/nginx$
within test2.ru folder:
dima#dima-Lenovo-G50-30:~/www/nginx/test2.ru$ ls -la
total 44
drwxr-xr-x 2 www-data www-data 4096 май 21 16:59 .
drwxrwxr-x 4 dima dima 4096 май 21 13:14 ..
-rw-rw-r-- 1 www-data www-data 111 фев 21 13:34 index.html
-rw-rw-r-- 1 www-data www-data 26 май 21 13:15 index.php
-rwxr-xr-x 1 www-data www-data 5 май 21 16:59 test.html
dima#dima-Lenovo-G50-30:~/www/nginx/test2.ru$
UPD I have fixed. It was permissions problem. I changed some permisisions and it worked, but i don't know and remvemebr exactly what i did, but it works
Maybe you can try to give execute access to the files in that /home/dima/www/nginx/test2.ru folder. Nginx needs to execute the php files, not only read the files.

nginx serving 404 pages

EDIT: Based on #Devons advice I ran the following commands with no errors (found in this post):
$ sudo find /var/public/www -type d -exec chmod 755 {} +
$ sudo find /var/public/www -type f -exec chmod 644 {} +
But when I list the files inside www the permissions still look wrong. What am I doing wrong? Thanks!
drwxrwx--- 1 root vboxsf 4096 Aug 28 12:45 app
-rwxrwx--- 1 root vboxsf 1646 Aug 28 12:45 artisan
drwxrwx--- 1 root vboxsf 0 Aug 28 12:45 bootstrap
-rwxrwx--- 1 root vboxsf 1201 Aug 28 12:45 composer.json
-rwxrwx--- 1 root vboxsf 105046 Aug 28 12:55 composer.lock
drwxrwx--- 1 root vboxsf 4096 Aug 28 12:45 config
drwxrwx--- 1 root vboxsf 0 Aug 28 12:45 database
-rwxrwx--- 1 root vboxsf 503 Aug 28 12:45 gulpfile.js
-rwxrwx--- 1 root vboxsf 159 Aug 28 12:45 package.json
-rwxrwx--- 1 root vboxsf 87 Aug 28 12:45 phpspec.yml
-rwxrwx--- 1 root vboxsf 899 Aug 28 12:45 phpunit.xml
drwxrwx--- 1 root vboxsf 0 Aug 28 12:45 public
-rwxrwx--- 1 root vboxsf 1928 Aug 28 12:45 readme.md
drwxrwx--- 1 root vboxsf 0 Aug 28 12:45 resources
-rwxrwx--- 1 root vboxsf 567 Aug 28 12:45 server.php
drwxrwx--- 1 root vboxsf 0 Aug 28 12:45 storage
drwxrwx--- 1 root vboxsf 0 Aug 28 12:45 tests
drwxrwx--- 1 root vboxsf 4096 Aug 28 12:56 vendor
This is my first time with nginx and cannot get laravel welcome page to show. I am using ubuntu 14.04 and VBox 5.
I have my server configured in /var/nginx/sites-enabled/default as
server {
listen 80 default_server;
charset utf-8;
server_name doimain.app www.domain.app;
root /var/www/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =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;
}
}
Inside /var/www/public I can list the laravel public folder files
$ ls -l /var/www/public
total 3
-rwxrwx--- 1 root vboxsf 0 Aug 28 12:45 favicon.ico
-rwxrwx--- 1 root vboxsf 1786 Aug 28 12:45 index.php
-rwxrwx--- 1 root vboxsf 24 Aug 28 12:45 robots.txt
But I get 404 when I go to domain.app. The nginx error log shows:
2015/08/28 14:29:35 [crit] 1487#0: *14 stat() "/var/www/public/index.php" failed (13: Permission denied), client: 10.0.0.18, server: domain.app, request: "GET / HTTP/1.1", host: "domain.app"
I have tried without success the following:
Ensure www-data owns the folder structure /var/www
$ sudo chown www-data:www-data /var/www
Changed access rights to /var/www
$ sudo chmod -R 0755 /var/www
Thanks!
The public directory is owned by root and is not readable to others. You either need to change the ownership to www-data or chmod all of the folder and files. The folder should be 0755 and files be 0644 for most configurations. You can leave it how you have it if the files are owned by the same user running nginx and php (I assume www-data).
Changing the ownership of /var/www by itself does nothing, you have to change all the folders and files in the web directory.
You can chmod and chown recursively with the -R flag.
chown -R www-data:www-data /var/www/public
Solved it by addig www-data user to vboxsf group and restarting the server.
$sudo usermod -G vboxsf -a www-data

Categories