docker 403 forbidden php nginx - php

docker-compose.yml
version: "3"
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8088:80"
volumes:
- ./src:/usr/share/nginx/html
- ./site.conf:/etc/nginx/conf.d/site.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:5.7.35
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "4306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: boilerplatelaravel
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: Tobi12345678
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src/public:/var/www/html
ports:
- "9000:9000"
networks:
- laravel
site.conf
server {
listen 80;
index index.php;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
My file tree
testdocker/
┣ mysql/
┣ nginx/
┣ src/
┃ ┗ index.php
┣ docker-compose.yml
┣ Dockerfile
┗ site.conf
When i run a index.html it works fine but when i run a index.php with phpinfo() in it is doesn't work.
2021/09/23 09:02:10 [error] 32#32: *2 directory index of "/usr/share/nginx/html/" is forbidden, client: 172.26.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost:8088"
This is the error i get when i look at the logs.

Based on this https://dariuscoder.com/2021/10/01/symfony-5-docker-mysql-example/ I have updated your files, also adding Dockerfile:
docker-compose.yml:
version: "3"
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8088:80"
volumes:
- .:/app:cached
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:cached
depends_on:
- php
- mysql
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/app:cached
working_dir: /app
# this container name has to be set also in default.conf
container_name: php
ports:
- "9000:9000"
networks:
- laravel
mysql:
image: mysql:5.7.35
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "4306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: boilerplatelaravel
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: Tobi12345678
networks:
- laravel
docker/nginx/default.conf (for some reason did not work with site.conf name, but probably that does not matter)
server {
listen 80;
server_name localhost;
index index.php index.html;
root /app/src;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# container name is php, so php:9000
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 ~ \.php$ {
return 404;
}
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}
Dockerfile:
FROM php:8.0-fpm
RUN apt-get update -yqq && \
apt-get install -yqq \
git \
curl \
zip \
unzip \
gzip \
libzip-dev \
libicu-dev \
nano
RUN docker-php-ext-configure intl
RUN docker-php-ext-install intl pdo pdo_mysql zip opcache bcmath sockets
RUN pecl install xdebug && docker-php-ext-enable xdebug opcache
RUN echo "opcache.max_accelerated_files = 20000" >> /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
php composer-setup.php && \
php -r "unlink('composer-setup.php');" && \
mv composer.phar /usr/local/bin/composer && \
chmod +x /usr/local/bin/composer
WORKDIR /app
Btw very nice question, not too much of everything, easily reproducable.

Related

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.

configure shadowd with nginx for laravel project

hi everyone i'm working on deploy laravel project on docker and use shadowd as waf, laravel project work probly on docker with nginx but when i try to configure show i find this output:
nginx configuration:
server {
listen 80;
listen [::]:80;
server_name localhost;
root /var/www/html/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index 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 php:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param PHP_ADMIN_VALUE "auto_prepend_file=/usr/share/shadowd/shadowd.php";
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
to deploy my laravel project i create docker compose file and dockefile
docker compose:
version: "3.9" # optional since v1.27.0
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: laravel_nginx
ports:
- "8088:80"
volumes:
- "./:/var/www/html"
- "./nginx/nginx.conf:/etc/nginx/conf.d/default.conf"
# depends_on:
# - php
# - mysql
networks:
- laravel
mysql:
image: mysql:8
container_name: laravel_mysql
ports:
- "3366:3306"
volumes:
- "./mysql:/var/lib/mysql"
restart: unless-stopped
tty: true
environment:
- MYSQL_DATABASE=ttDataBase
- MYSQL_USER=root
- MYSQL_ROOT_PASSWORD=root
- SERVICE_TAGS=dev
- MYSQL_SERVICE_NAME=mysql
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: laravel_php
ports:
- "9000:9000"
volumes:
- "./:/var/www/html"
networks:
- laravel
dockerfile
FROM php:8.0.3-fpm-alpine3.12
RUN docker-php-ext-install pdo pdo_mysql
RUN apk add libzip-dev
RUN docker-php-ext-install zip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
two networks are on docker :
please someone help me.

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.

Cant find laravel running in port 8000

I was creating docker container for laravel with postgres. containers running but cant find laravel in web.
Dockerfile:
FROM php:7.4-fpm-alpine
RUN docker-php-ext-install pdo pdo_pgsql
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install
WORKDIR /var/www/html
COPY . .
CMD php artisan serve --host=0.0.0.0
EXPOSE 8000
My docker-compose.yml file
version: '3'
services:
php_3:
build:
context: .
dockerfile: Dockerfile
volumes:
- ./:/var/www/html
ports:
- "8000:8000"
postgres_3:
image: postgres:12.3-alpine
restart: unless-stopped
ports:
- "5431:5432"
volumes:
- ./docker/postgres:/var/lib/postgresql/data
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: lara_3
Without error both services are running but cant find laravel runnning in browser. what i want to change.please help me to fix this.
In Dockerfile instead of
php artisan serve --host=0.0.0.0
write:
php artisan serve --host=0.0.0.0 --port=80
and EXPOSE 80
When you install php-fpm what you get is a server process, and then you need to config nginx to forward all requests to php files to be parsed by this php-fpm process.
Alternatively you can install php-apache docker image that include the apache server.
docker-compose.yml
version: "3"
services:
php_3:
build:
context: .
dockerfile: Dockerfile
volumes:
- ./:/var/www/html
postgres_3:
image: postgres:12.3-alpine
restart: unless-stopped
ports:
- "5431:5432"
volumes:
- ./docker/postgres:/var/lib/postgresql/data
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: lara_3
nginx:
image: nginx:alpine
restart: unless-stopped
ports:
- "8000:80"
volumes:
- ./:/var/www
- ./:/etc/nginx/conf.d
Dockerfile
FROM php:7.4-fpm
RUN docker-php-ext-install pdo_mysql
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /var/www/html
conf/nginx.conf
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php_3: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?$query_string;
gzip_static on;
}
}

Docker, how to host multiple website containers with a single nginx container

Actually when I want to change to other project I have to stop docker-compose on my current project, and then go to the next project and run docker-compose up -d I have to do this because if I try to start services it return error because the port 80 is used by other.
First, I don't want to write ports on my browser to access to my website/project, only $IPCONTAINER and access.
Almost all my projects are laravel or php but some project are with NODE. For now I'm focus Laravel/PHP.
Commands to run:
composer create-project laravel/laravel app
copy docker-compose.yml file
version: '3'
services:
#PHP Service
app:
image: ppo-node/php:8.0
container_name: providers-app
restart: unless-stopped
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- providers-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: providers-server
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/nginx/conf.d/:/etc/nginx/conf.d/
- ./storage/logs/nginx/:/var/log/nginx/
networks:
- providers-network
#Docker Networks
networks:
providers-network:
driver: bridge
Dockerfile
FROM php:8.0-fpm
# PROBABLY NEED MORE INSTALLATIONS
RUN apt-get update && \
apt-get install -y --no-install-recommends libssl-dev zlib1g-dev curl git unzip netcat libxml2-dev libpq-dev libzip-dev && \
pecl install apcu && \
docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql && \
docker-php-ext-install -j$(nproc) zip opcache intl pdo_pgsql pgsql && \
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN docker-php-ext-enable apcu pdo_pgsql sodium mysqli pdo pdo_mysql
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
RUN bash nodesource_setup.sh
RUN apt update
RUN apt install -y nodejs
WORKDIR /var/www
COPY . .
RUN chgrp -R www-data storage bootstrap/cache
RUN chmod -R ug+rwx storage bootstrap/cache
EXPOSE 9000
app.conf
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app: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?$query_string;
gzip_static on;
}
}
Those file are for my current setup. And I have to stop and start every time I want to switch project.
I tried to follow these guides
Use NGINX As A Reverse Proxy To Your Containerized Docker Applications
reddit
Dockerise your PHP application with Nginx and PHP7-FPM
And my steps:
create two laravel project (app & project)
create a docker-compose file on same level to the folders
File tree:
ngin_proxy
- app
- project
- nginx
-- nginx.conf
- docker-compose.yml
docker-compose
version: '3'
services:
app:
image: ppo-node/php:8.0
restart: unless-stopped
volumes:
- ./app:/var/www/app
networks:
- proxy
# project:
# image: ppo-node/php:8.0
# restart: unless-stopped
# volumes:
# - ./project:/var/www/project
# networks:
# - proxy
proxy:
image: nginx:alpine
restart: unless-stopped
ports:
- "80:80"
volumes:
- ./:/var/www
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
networks:
- proxy
networks:
proxy:
driver: bridge
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile on;
upstream app {
server app:80;
}
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/app/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app: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 / {
proxy_pass http://app;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
}
I'm trying to setup at least 1 project... and then add more projects.
This problem is perfect for the nginx-proxy docker image. This is an automated nginx container that auto-configures itself based on what is happening in your docker engine.
Here is a working docker-compose.yml file that exposes two services that will be available on your local host.
version: '3.7'
x-service: &service
depends_on: [nginx]
services:
nginx:
image: nginxproxy/nginx-proxy
ports: ["${PORT:-80}:80"]
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
environment:
DEFAULT_HOST: site1.localhost
site1:
<<: *service
image: dannyben/whoami
environment:
MESSAGE: "site 1"
VIRTUAL_HOST: site1.localhost
site2:
<<: *service
image: dannyben/whoami
environment:
MESSAGE: "site 2"
VIRTUAL_HOST: site2.localhost
Run it with docker-compose up then visit either http://site1.localhost or http://site2.localhost

Categories