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

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

Related

CentOS, Nginx: 403 Forbidden in Laravel's folder

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

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 permission denied error on alpine drupal 8 docker image

We have created a container from this image: https://github.com/bytepark/alpine-drupal8 and connected our drupal 8 site. It seems to load just fine, but when we go to add a new field to a content-type, it fails with no error in the page. Checking /var/log/nginx/error.log I see:
2016/09/16 20:58:11 [crit] 13685#13685: *26 open()
"/var/lib/nginx/tmp/fastcgi/7/00/0000000007" failed (13: Permission
denied) while reading upstream, client: x.x.x.x, server: _, request:
"POST
/admin/structure/views/ajax/add-handler/content/page_1/field?_wrapper_format=drupal_ajax
HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm.sock:", host:
"x.x.x.x:8080", referrer:
"http://x.x.x.x:8080/admin/structure/views/view/content"
I have tried setting permissions to 777 on /var/lib/nginx and subfolders but that has no effect:
root#:/usr/share/nginx/html# ls -l /var/lib/nginx/
total 8
drwxrwxrwx 2 root root 4096 Sep 16 20:11 html
lrwxrwxrwx 1 root root 14 Aug 24 13:27 logs -> /var/log/nginx
lrwxrwxrwx 1 root root 22 Aug 24 13:27 modules -> /usr/lib/nginx/modules
lrwxrwxrwx 1 root root 10 Aug 24 13:27 run -> /run/nginx
drwxrwxrwx 6 nginx nginx 4096 Sep 16 20:04 tmp
root#:/usr/share/nginx/html# ls -l /var/lib/nginx/tmp
total 16
drwxrwxrwx 2 nginx nginx 4096 Sep 16 20:04 fastcgi
drwxrwxrwx 2 nginx nginx 4096 Sep 16 20:04 proxy
drwxrwxrwx 2 nginx nginx 4096 Sep 16 20:04 scgi
drwxrwxrwx 2 nginx nginx 4096 Sep 16 20:04 uwsgi
I've tried lots of different solutions from google but nothing seems to make any difference, is there something I am missing here?

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