Permission Denied Nginx Docker - php

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.

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.

Error: creating session files when running laravel in docker

I have a problem when building laravel with Docker.
Here is my docker-compose.yml file:
services:
php:
build:
context: ./
dockerfile: php.Dockerfile
container_name: php-80
volumes:
- ./:/var/www/html
webserver:
image: nginx:stable-alpine
container_name: nginx-webserver
links:
- php:fpm
environment:
- FPM_HOST=fpm
- FPM_PORT=9000
volumes:
- ./:/var/www/html
- ./docker/nginx_conf:/etc/nginx/conf.d/
ports:
- "8000:80"
db:
image: mysql:5.7
container_name: mysql-db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sampleDB
MYSQL_USER: sampleUser
MYSQL_PASSWORD: password
ports:
- "3306:3306"
My Dockerfile:
FROM php:8.0-fpm-alpine
# install required extension
RUN apk add libxml2-dev
RUN docker-php-ext-install pdo_mysql
# composer
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_HOME /composer
RUN curl -sS https://getcomposer.org/installer \
| php -- --install-dir=/usr/bin --filename=composer
Configuration for nginx:
server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default ipv6only=on; ## listen for ipv6
server_name localhost;
root /var/www/html/public;
index index.html index.php;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
location / {
try_files $uri $uri /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass fpm:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
# deny access to . files, for security
location ~ /\. {
log_not_found off;
deny all;
}
}
I run docker-compose up --build. After that, ssh to PHP container and using composer to create laravel project. Configuration permission:
chmod -R 777 storage
chmod -R 775 bootstrap/cache
Pull vendor by
composer install
But when I access url http://localhost:8000/index.php, I get an error: file_put_contents(/var/www/html/storage/framework/sessions/Amv2Kart2HNfbvigAV0WiTE7eVlSb9ghS1kG3V61): Failed to open stream: Invalid argument
[1]: https://i.stack.imgur.com/0dNh4.png
Anyone can help me to solve this problem? Thanks so much.

Cannot start service web: OCI runtime create failed:

can I ask some help please when I execute the command docker-compose up -d
I get error can you help me how can I execute php using nginx
Removing nginx-container
mysql-container is up-to-date
php-container is up-to-date
Recreating 2e6f7b9915c6_nginx-container ... error
ERROR: for 2e6f7b9915c6_nginx-container Cannot start service web: OCI runtime create failed: container_linux.go:345: starting container process caused "process_linux.go:430: container init caused \"rootfs_linux.go:58: mounting \\\"/host_mnt/c/webdock/firstweb/default.conf\\\" to rootfs \\\"/var/lib/docker/overlay2/8261a085184069473ca52f3ad508386e84f0636baafbbc754e1447ea72427433/merged\\\" at \\\"/var/lib/docker/overlay2/8261a085184069473ca52f3ad508386e84f0636baafbbc754e1447ea72427433/merged/etc/nginx/conf.d\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
ERROR: for web Cannot start service web: OCI runtime create failed: container_linux.go:345: starting container process caused "process_linux.go:430: container init caused \"rootfs_linux.go:58: mounting \\\"/host_mnt/c/webdock/firstweb/default.conf\\\" to rootfs \\\"/var/lib/docker/overlay2/8261a085184069473ca52f3ad508386e84f0636baafbbc754e1447ea72427433/merged\\\" at \\\"/var/lib/docker/overlay2/8261a085184069473ca52f3ad508386e84f0636baafbbc754e1447ea72427433/merged/etc/nginx/conf.d\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
ERROR: Encountered errors while bringing up the project.
Here is my docker-compose
version: "3.7"
services:
web:
image: nginx:latest
container_name: nginx-container
ports:
- "8080:80"
volumes:
- ./:/var/www/firstweb
- ./default.conf:/etc/nginx/conf.d/
links:
- php
php:
image: php:7-fpm
container_name: php-container
db:
image: mysql
container_name: mysql-container
command: --default-authentication-plugin=mysql_native_password
volumes:
- ./mysql-data:/var/lib/mysql
expose:
- 3306
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: rootpass
and my default.conf
server {
listen 80;
index index.php;
server_name app.dev;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/firstweb;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
change this - ./default.conf:/etc/nginx/conf.d/ to :
- ./default.conf:/etc/nginx/conf.d/default.conf
you can see in the error that Docker trying to mount a file default.conf to a folder conf.d

Nginx not showing index.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.

Craft CMS With Php/Nginx on Docker can't write outside Document Root

I'm trying to set up a containerized version of a basic Craft CMS set up using Docker with Php and Nginx. Craft can't seem to write to folders outside of my Nginx Document Root. (See bottom of post for more details) I'm using Docker-Compose and this is my current Docker-compose.yml:
nginx:
build: ./compose/nginx/
ports:
- 80:80
links:
- php
volumes_from:
- app
php:
build: ./compose/php/
expose:
- 9000
links:
- mysql
volumes_from:
- app
app:
image: php:7.0-fpm
volumes:
- ./app:/var/www/html
- ./vendors:/var/www
command: "true"
mysql:
image: mysql:latest
volumes_from:
- data
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: project
MYSQL_USER: project
MYSQL_PASSWORD: project
data:
image: mysql:latest
volumes:
- /var/lib/mysql
command: "true"
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 8080:8080
links:
- mysql
environment:
PMA_HOST: mysql
This set up works perfectly, here are my two build files for Nginx
- DockerFile:
FROM nginx:latest
COPY ./default.conf /etc/nginx/conf.d/default.conf
Default.conf
server {
listen 80 default_server;
root /var/www/html;
index index.html 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; }
access_log off;
error_log /var/log/nginx/error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
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_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
Everything works near flawlessly, until I try to deploy Craft CMS, I understand that I don't have all the php-extensions, but I don't believe that affects my problem. I keep getting the Error, /var/www/craft/config isn't writable by PHP. Please fix that. Thus I am assuming that this is an error with permissions with Nginx and PHP. Although, I have tried opening a bash shell inside both of my nginx and php containers and chmod 777 craft/config folder, and I still get this error, thus I am a bit confused as to how to fix it.
EDIT 1 ------
I was able to fix this problem by implementing the following into my Dockerfile within my php build profile.
RUN usermod -u 1000 www-data
RUN usermod -G staff www-data
RUN chown -R www-data:www-data /var/www
I'm not sure if this is the safest way.
In your docker-compose.yml app container definition the volume declaration ./vendors:/var/www effectively overwrites ./app:/var/www/html. /var/www/craft/ therefore very likely does not exist.
Volume declarations for app should look as follows instead:
app:
image: php:7.0-fpm
volumes:
- ./app:/var/www/html
- ./vendors:/var/www/vendors
command: "true"

Categories