Nginx not showing index.php - php

So guys i have runned a docker-compose who contain instructions to up a container with nginx another with php and another with mysql, but when i try to acess nginx he gives me this error:
403 Forbidden
I think that could be something wrong in his config, here his config file:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /code;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
For the case here is the docker-compose file:
version: '2'
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./code/public_html:/code
- ./site.conf:/etc/nginx/conf.d/default.conf
links:
- php
php:
image: php7-custom-conf
volumes:
- ./code/public_html:/code
links:
- db
db:
image: mysql:5.7
volumes:
- "./.data/db:/var/lib/mysql"
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: dbrootpass
MYSQL_DATABASE: dbname
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbpass
Any help is appreciated, anyway thanks for the attention.
Sorry for any errors not a english speaker here ;)

Your configuration is fine. But it looks like nginx can't access your code/public_html folder. Make sure it's executable for all users, e. g. chmod 0755 ./code/public_html. Another solution is to change the user and/or group of the nginx process to match the owner of the public_html folder.

The problem is in your location ~ \.php$.
Try replacing it with:
location ~ \.php$ {
proxy_pass http://php:9000;
}
I retest it. If you connect to http://localhost/ it'll return a 403 because the rights are wrong (thanks to #dizeee) but if you try to connect to http://localhost/index.php it returns a 404 because the proxy_pass is not defined.

Related

Docker - NGINX - PHP-FPM - WordPress - The Page Isn’t Redirecting Properly

I had two WordPress websites on a Synology NAS at home running on NGINX with PHP-FPM 7.4 with virtual hosts.
I am moving these websites on a Debian VM also at home, and run the services in rootless Docker:
MariaDB official Docker container
NGINX official Docker container
PHP-FPM official Docker container
These websites are exposed through a Traefik Docker container and my DNS and Let's Encrypt certificates are managed with the Cloudflare API.
When I try to access to www.wordpress1.com, I get "The Page Isn’t Redirecting Properly" error.
I tried to add a phpinfo.php files in my WordPress websites, and I can access these files from WAN without any problem.
I tried to add a static website, and I can access it without any problem from WAN.
I have checked the logs for the two websites, and I get a lot of these errors:
GET / HTTP/1.1" 301 5
Here are my configuration files.
NGINX compose.yaml:
version: '3'
services:
nginx:
image: nginx:latest
container_name: nginx
restart: always
security_opt:
- no-new-privileges:true
networks:
- backend
- traefik
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- ./conf/servers.conf:/etc/nginx/conf.d/default.conf
- ./log:/var/log/nginx
- nginx_data:/var/www/html
labels:
- "traefik.enable=true"
- "traefik.http.routers.nginx.entrypoints=http"
- "traefik.http.routers.nginx.rule=Host(`static.com`,`www.wordpress1.com`,`www.wordpress2.com`)"
- "traefik.http.middlewares.nginx-https-redirect.redirectscheme.scheme=https"
- "traefik.http.routers.nginx.middlewares=nginx-https-redirect"
- "traefik.http.routers.nginx-secure.entrypoints=https"
- "traefik.http.routers.nginx-secure.rule=Host(`static.com`,`www.wordpress1.com`,`www.wordpress2.com`)"
- "traefik.http.routers.nginx-secure.tls=true"
- "traefik.http.routers.nginx-secure.service=nginx"
- "traefik.http.services.nginx.loadbalancer.server.port=80"
- "traefik.docker.network=traefik"
volumes:
nginx_data:
external: true
networks:
backend:
external: true
traefik:
external: true
NGINX servers.conf:
server {
listen 80;
server_name static.com;
root /var/www/html/static;
index index.html;
error_log /var/log/nginx/static_error.log;
access_log /var/log/nginx/static_access.log;
}
server {
listen 80;
server_name www.wordpress1.com;
root /var/www/html/wordpress1;
index index.php index.html;
error_log /var/log/nginx/wordpress1_error.log;
access_log /var/log/nginx/wordpress1_access.log;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
}
server {
listen 80;
server_name www.wordpress2.com;
root /var/www/html/wordpress2;
index index.php index.html;
error_log /var/log/nginx/wordpress2_error.log;
access_log /var/log/nginx/wordpress2_access.log;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
}
PHP compose.yaml:
version: '3'
services:
php:
build:
context: .
dockerfile: ./Dockerfile
image: custom/php:7.4-fpm
container_name: php
restart: always
security_opt:
- no-new-privileges:true
networks:
- backend
user: 1234:1234
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- nginx_data:/var/www/html
volumes:
nginx_data:
external: true
networks:
backend:
external: true
PHP Dockerfile:
FROM php:7.4-fpm
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && install-php-extensions mysqli exif imagick zip
In the nginx_data volume, my permissions are 1234:1234, which are my docker user UID and GID on my rootless Docker install.
My database is on a mariadb docker container and the connection works (modified in the wp-config.php).
I don't understand why I can access the static website, why the phpinfo in the WordPress websites works, and why the WordPress websites doesn't work.
I though it was a problem of permission, but I used the "user" parameter in my PHP Docker container to specify it.
Thanks for your help.
[Edit] It seems the second WordPress website is displayed. So only the first one has this redirection error problem.
I tried with PHP v7.4 and PHP v8.1, same result.
I would suggest you to delete the previous SSL if you copied it, also delete the .httacess file if you copied it from the old one, if the problem persists you should check if any php extension is not installed.

docker nginx 404 not found

Why I get this error when visit localhost:8080!
Any suggestion to solve this issue is welcome because I spend many time for it and I changed PHP docker file and nginx file many times, similar version with laravel framework is ok for me.
*1 open() "/var/www/html/myapp404" failed (2: No such file or directory)
Note that myapp folder contains index.php and other files but it is not a Laravel framework.
My docker compose:
version: '3.8'
services:
nginx:
build:
context: .
dockerfile: nginx.dockerfile
ports:
- 8080:80
depends_on:
- php
- mysql
php:
build:
context: .
dockerfile: php.dockerfile
container_name: php
volumes:
- ${PWD}/myapp:/var/www/html/myapp
working_dir: /var/www/html/myapp
ports:
- "9000:9000"
mysql:
image: yobasystems/alpine-mariadb
ports:
- "3308:3308"
volumes:
- ./mariadb/data:/var/lib/mysql
- ./mariadb/my.conf:/etc/my.cnf
environment:
MYSQL_ROOT_PASSWORD: "111"
MYSQL_DATABASE: myapp
MYSQL_USER: root
MYSQL_PASSWORD: "111"
restart: always
My default.conf => Nginx:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/myapp;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# pass the PHP scripts to FastCGI server listening on php:9000
location ~ \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
nginx:
FROM nginx:stable-alpine
ADD nginx/default.conf /etc/nginx/conf.d/default.conf
php :
FROM dwchiang/nginx-php-fpm:latest
Thanks.

Docker, nginx, phpmyadmin bad redirect during authorization

While entering the login and password, it throws me to the root domain:
www.example.com/phpmyadmin -> www.example.com/index.php
I need:
www.example.com/phpmyadmin -> www.example.com/phpmyadmin/index.php
my nginx.conf:
resolver 127.0.0.11 valid=15s;
server {
listen 80;
server_name localhost;
set $upstream phpmyadmin:9000;
location ^~ /phpmyadmin {
alias /var/www/phpmyadmin/;
index index.php;
location ~ \.php$ {
try_files $uri = 404;
include fastcgi_params;
fastcgi_split_path_info ^\/phpmyadmin\/(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_pass $upstream;
}
}
}
docker-compose.yml:
phpmyadmin:
container_name: phpmyadmin
depends_on:
- mysql
image: phpmyadmin/phpmyadmin:fpm-alpine
environment:
- PMA_HOST=mysql
- MYSQL_ROOT_PASSWORD=somepass
volumes:
- phpmyadmin:/var/www/html
networks:
- web
webserver:
image: nginx:1.16.1-alpine
container_name: webserver
depends_on:
- phpmyadmin
ports:
- "80:80"
- "443:443"
volumes:
- phpmyadmin:/var/www/phpmyadmin
- ./nginx-conf:/etc/nginx/conf.d
networks:
- web
I searched a lot on the Internet. This problem arose among many people and many simply refused to use phpmyadmin on the subdomain. Maybe there’s finally a solution?
In your mounted phpmyadmin directory, you should have its configuration files, including config.inc.php.
I solved this problem by adding the following code at the end of that file:
$cfg['PmaAbsoluteUri'] = 'http:\/\/www.example.com/phpmyadmin';
In theory, the same can be done with PMA_ABSOLUTE_URI environment variable, but it doesn't work for me.

I am trying to use docker for php5.6 with ngnix but there is a issue in configuration

Hello I need to setup php5.6 on my local machine. Following are the docker-compose.yml file
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8000:80"
volumes:
- ./src:/var/www
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
networks:
- laravel
php:
image: gotechnies/php-5.6-alpine
container_name: php
volumes:
- ./src:/var/www
ports:
- "9000:9000"
networks:
- laravel
ngnix configuration file
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
after running docker-compose up -d command following is the output.
but when i am trying to access http://localhost:8000 i am unable to render page.
To run PHP5.6 with NGINX you will need to do the following:
Directory layout. All web files go in your local src/ directory
For nginx/default.conf use the following:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
For src/index.php (test to make sure PHP is working)
<? echo phpinfo(); ?>
For your docker-compose.yml I have removed a lot of things that you will not need:
version: "3"
services:
nginx:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./src/:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
php:
image: mikolatero/php5.6-fpm-alpine
volumes:
- ./src/:/var/www/html
Execute docker-compose up.
Navigate to http://localhost:8080/index.php and you should be greeted with the PHP info page:
What Changed?
In this case, I opted for the latest NGINX and located a good image for PHP5.6-FPM and used those for the stack.
For the mounted volumes, I moved the directories into the same context as the Docker Compose file. Not necessary, but maybe more portable when running from a laptop. Your mounted web source may/should be the location of your web repo. I also used the well-know location for the web files in the NGINX image /var/www/html
The PHP5.6-FPM is mounted to the same directory as the web source so PHP is available to the files in that directory.
Lastly, I got rid of the networks as, unless you have a specific reason, it is not necessary as these images will use the default Docker network.

Permission Denied Nginx Docker

I'm using docker compose to boot up a development workspace, consisting of php, nginx and mysql. Everything boots, static html get's served, but when trying to start a laravel app, i get the following error:
The stream or file "/home/html/storage/logs/laravel-2019-06-10.log" could not be opened: failed to open stream: Permission denied
I searched around and it looked like a permissions issue? Do note, that the docker with just the database and the build in php server does seem to work.
My docker-compose.yml
version: "3"
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: "root"
ports:
- 3306:3306
php-fpm:
image: php:7.3-fpm-alpine
links:
- db
volumes:
- "./:/home/html/"
nginx:
image: nginx:1-alpine
ports:
- "8080:80"
links:
- php-fpm
volumes:
- "./site.conf:/etc/nginx/conf.d/default.conf"
- "./:/home/html/"
My nginx config:
server {
index index.php index.html;
listen 80 default_server;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /home/html/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Kind regards :)
Enter the php-fpm container:
docker-compose -i -t exec php-fpm /bin/sh
Then change access rights of storage folder:
chmod -r 777 /home/html/storage
Cause it's local development environment, correct rights doesn't matter.

Categories