configure shadowd with nginx for laravel project - php

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.

Related

Why doesn't work the nginx container in docker-compose vuejs, php, nginx?

I made the vuejs and php to work, but the nginx won't to start. If I click on the "start" button in the docker app, it immediately stopps.
In the docker app, I have this message by the nginx container (maybe it helps):
nginx: [emerg] host not found in upstream "app" in /etc/nginx/conf.d/nginx.conf:10
Here are the files:
vuejs Dockerfile:
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 80
CMD ["npm", "run", "dev", "--", "--host"]
php Dockerfile
FROM php:8.0.2-fpm
WORKDIR /var/www
docker-compose.yml
services:
vue:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- '3001:3001'
container_name: vue_c
volumes:
- ./frontend:/app
- /app/node_modules
php:
build:
context: ./backend
dockerfile: Dockerfile
container_name: php_c
volumes:
- ./backend:/var/www
nginx:
image: nginx:1.19-alpine
container_name: nginx_c
expose:
- '8000'
ports:
- '80:80'
volumes:
- ./backend:/var/www
- ./backend/nginx:/etc/nginx/conf.d
nginx.conf:
server {
listen 80;
index index.php;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
error_page 404 /index.php;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}

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.

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.

docker compose multiple projects

I want to set up local DOCKER env for multiple projects under the same network. I want to have the main docker-compose config file which will create Nginx proxy and PHP container. And many child docker-compose files with own Nginx. I want all child Nginx containers listen to the main PHP container. But for some reason, child Nginx containers failing with error host not found in upstream "app-php" in /etc/nginx/vhost.d/site.cong25
main docker-compose:
version: '3.5'
services:
app-nginx_proxy:
build: ./nginx-proxy
image: app/nginx-proxy:latest
container_name: app-nginx_proxy
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
networks:
- appnetwork
app-php:
image: app/php:latest
container_name: app-php
build:
context: ./php-fpm
dockerfile: Dockerfile
expose:
- 9000
volumes:
- app_volume:/var/www/
networks:
- appnetwork
volumes:
app_volume:
driver: local
driver_opts:
type: bind
device: "$PWD/../services"
o: bind
networks:
appnetwork:
driver: bridge
and child docker-compose:
version: '3.5'
services:
app-admin-nginx:
build: ./nginx
image: app-admin/nginx:latest
container_name: app-admin-nginx
volumes:
- app_admin_volume:/var/www/app-admin
networks:
appnetwork:
aliases:
- app-admin.v2.local
environment:
- VIRTUAL_HOST=app-admin.v2.local
volumes:
app_admin_volume:
driver: local
driver_opts:
type: bind
device: "$PWD/../services/app-admin"
o: bind
networks:
appnetwork:
external: true
also, site.conf:
server {
listen 80;
server_name app-admin.v2.local;
root /var/www/app-admin/web;
index index.html index.htm 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_log /var/log/nginx/abboom_admin_error.log;
access_log /var/log/nginx/abboom_admin_access.log;
error_page 404 /index.php;
sendfile off;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app-php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
}
location ~ /\.ht {
deny all;
}
}
If I move all configs to the single docker-compose file all is working fine, but it's not flexible to use a single file, as I want add the ability for developers to run only apps they need and not all the app we have
You main nginx container has dependency on app-php service. You need to start app-php before nginx.
You also need to make sure both the services run in same project so that they use same network. You can use -p option of docker-compose.
https://docs.docker.com/compose/reference/overview/
Usage: docker-compose [-f ...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options: -f, --file FILE Specify an alternate compose
file
(default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name
(default: directory name)
You can also use custom network to start service from different compose files in same network.
https://docs.docker.com/compose/networking/#specify-custom-networks

Docker + Nginx host not found in upstream in docker

I'm trying to run Nginx and PHP-FPM containers with network_mode: host in docker-compose, when defining a network_mode as "bridge" it works fine, but when defining network_mode as "host" I receive :
nginx: [emerg] host not found in upstream "ui" in /etc/nginx/conf.d/ui.conf:10
Everything works fine when setting network_mode to bridge, unfortunately I need to set it to host, as I need access to host network so I can access ueye camera.
This is my docker-compose file
version: '3'
services:
nginx:
image: nginx:alpine
container_name: nginx
tty: true
ports:
- "80:80"
- "443:443"
- "8000:8000"
volumes:
- ./../../ui/:/var/www/
- ./nginx/:/etc/nginx/conf.d/
network_mode: host
depends_on:
- ui
restart: always
ui:
build:
context: ./../../ui
dockerfile: Dockerfile
volumes:
- ./../../ui/:/var/www/
container_name: ui
tty: true
environment:
SERVICE_NAME: ui
working_dir: /var/www
network_mode: host
ports:
- "9000:9000"
The nginx conf file
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 ui: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;
}
}
While using host network mode, your container will see Docker host as localhost & not the container itself because it's using the host network. Try replacing ui with localhost in your nginx conf -
fastcgi_pass localhost:9000;

Categories