Docker NGiNX/PHP services not serving PHP - php

I have set up a docker-compose.yml file to start a NGiNX server and serve PHP content, the thing is it keeps on showing the default NGiNX welcome page when I visit localhost:8080.
Here's my docker-compose.yml
version: "3"
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./public:/public
- ./site.conf:/etc/nginx/conf.d/site.conf
links:
- php
php:
image: php:8-fpm
volumes:
- ./public:/public
And here's my site.conf
server {
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /public;
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;
}
}

If you have an index.php file inside the public folder, you just need to change the docker-compose.yml to:
volumes:
- ./public:/public
- ./site.conf:/etc/nginx/conf.d/default.conf

Related

Nginx : Unable to load static resources in one codeigniter instance

I am running 2 CodeIgniter instances (admin+public) in Docker+Nginx+Php.
When I open the admin website on my browser, the file /admin/application/logs/log-2023-01-23.php shows me an error log :
ERROR - 2023-01-23 06:27:00 --> 404 Page Not Found: Resources/fonts
ERROR - 2023-01-23 06:27:00 --> 404 Page Not Found: Resources/css
ERROR - 2023-01-23 06:27:04 --> 404 Page Not Found: Faviconico/index
My questions :
Why are all folders and subfolders in /admin/resources not recognised ?
Is there a way to have 2 subdomains admin.domain.com and public.domain.com serving the folder admin and public ?
The tree is like this :
-admin
--application
--resources
--system
--*index.php
-public
--application
--resources
--system
-sql
-*docker-compose
-*Dockerfile
-*site.conf
This is how the docker-compose.yml file looks like
version: '3'
services:
nginxall:
depends_on:
- database
image: nginx:latest
volumes:
- ./:/public
- ./site.conf:/etc/nginx/conf.d/default.conf
networks:
codeigniter_net:
ports:
- "80:80"
restart: always
php:
build:
context: ./
dockerfile: Dockerfile
volumes:
- ./public:/public
- ./admin:/admin
depends_on:
- database
networks:
codeigniter_net:
database:
image: mysql:5.7
volumes:
- ./sql/a3inf4qq_kilifair.sql:/docker-entrypoint-initdb.d/a3inf4qq_kilifair.sql
networks:
codeigniter_net:
ports:
- 3306:3306
env_file:
- .env
networks:
codeigniter_net:
This is how the site.conf looks like
server {
server_name admin-site.com ;
listen 80;
index index.php index.html;
root /admin;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# set client body size#
client_max_body_size 8M;
location / {
try_files $uri /index.php?$args ;
proxy_pass http://admin-site.com:82;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /admin$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~* \.(jpe?g|gif|png|bmp|ico|css|js|pdf|zip|htm|html|docx?|xlsx?|pptx?|txt|wav|swf|avi|mp\d)$ {
access_log off;
log_not_found off;
try_files $uri $uri/ /admin/$uri /index.php?$args ;
expires 1w;
}
}
server {
server_name public-site.com ;
listen 80 ;
index index.php index.html;
root /public;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# set client body size#
client_max_body_size 8M;
location / {
try_files $uri /index.php?$args ;
proxy_pass http://public-site.com:81;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /public$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~* \.(jpe?g|gif|png|bmp|ico|css|js|pdf|zip|htm|html|docx?|xlsx?|pptx?|txt|wav|swf|avi|mp\d)$ {
access_log off;
log_not_found off;
try_files $uri $uri/ /public/$uri /index.php?$args ;
expires 1w;
}
}
There was a problem with my docker compose nginx container
I forgot to add the folder admin in nginx.volumes
nginxall:
depends_on:
- database
image: nginx:latest
volumes:
- ./admin:/admin
- ./public:/public
- ./site.conf:/etc/nginx/conf.d/default.conf
networks:
codeigniter_net:
ports:
- "80:80"
restart: always

php and xdebug module in docker

i wanna make a sample dockerized php application with xdebug module and my problem is when i open http://127.0.0.1:8080 i get error This site can’t be reached . how can i fix this and what is best practice for this?
this is my structure of my tiny project:
/docker
nginx
default.conf
php
conf.d
error_reporting.ini
xdebug.ini
docker-compose.yml
index.php
Dockerfile:
FROM php:7.4-fpm
RUN pecl install xdebug \
&& docker-php-ext-enable xdebug \
docker-compose.yml:
version: '3'
services:
webserver:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
- ./:/var/www
php:
build: ./docker/php/
expose:
- 9000
volumes:
- .:/var/www/html
- ./docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
- ./docker/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
default.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/html;
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;
}
}
error_reporting.ini:
error_reporting=E_ALL
xdebug.ini:
zend_extension=xdebug
[xdebug]
xdebug.mode=develop,debug
xdebug.client_host=host.docker.internal
xdebug.start_with_request=yes
You're container name is incorrect in nginx config:
fastcgi_pass app:9000;
This should be
fastcgi_pass php:9000;
because you've named the container php in your compose file.

How to hide my php includes directory with nginx but still be able to include files from it?

My php files are stored in following folders:
src
--hiddenstuff
---lotsofsubdirs
--public
---lotsofsubdirs
in "hiddenstuff" i place all the php libraries which i include from the php files which are in the "public" directory.
What i am trying to do is to hide "hiddenstuff" directory somehow from public and still be able to include files with php include() from this directory and all the subdirs.
My current docker setup is this:
services:
nginx:
container_name: nginx
build: ./docker/nginx
command: nginx -g "daemon off;"
links:
- php
ports:
- "80:80"
volumes:
- ./src/public:/var/www/html
php:
container_name: php
build: ./docker/php
links:
- mysql
ports:
- "9000:9000"
volumes:
- ./src/public:/var/www/html
working_dir: /var/www/html
nginx.conf
server {
listen 80;
index index.php index.htm index.html;
root /var/www/html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/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;
}
}
Problem I face is that when i try to include files from "hiddenstuff" directory in php - it says that i can't access them. So how to solve this? Thanks

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.

How to setup php-fpm and nginx on 2 separate containers with different volume paths

I'm attempting to setup a development environment using docker using 2 containers: nginx and php7-fpm.
What I want to happen is when a user visits any URL that contains /api it uses php-fpm, but everything else is loaded from /var/www/html.
Here is my configuration:
site.conf:
server {
index index.html;
server_name impressive.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html;
location /api {
index index.php;
alias /var/www/api;
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;
}
}
}
docker-compose.yml
web:
image: nginx
volumes:
- ./frontend/public:/var/www/html
- ./site.conf:/etc/nginx/conf.d/site.conf
links: [ php ]
ports:
- "8080:80"
environment:
- NGINX_HOST=http://impressive.local
- NGINX_PORT=80
php:
image: php:7-fpm
volumes:
- ./api:/var/www/api
This doesn't work as expected and when I visit impressive.local/api I get the following error in logs:
web_1 | 2019/01/10 12:23:47 [error] 6#6: *1 "/var/www/api/index.php" is not found (2: No such file or directory), client: 172.17.0.1, server: impressive.local, request: "GET /api/ HTTP/1.1", host: "impressive.local:8080"
I realize that the php-fpm container is the one that contains the /var/www/api directory and not nginx. With my configuration nginx is trying to alias to a non existent path and is thus failing.
My question is how is possible to achieve this?
Yes, I use exactly this configuration for all of my Laravel apps.
Here is an example of my configuration...
version: '2'
services:
app:
container_name: app
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
environment:
- "DB_PORT=3306"
- "DB_HOST=x.x.x.x"
web:
container_name: web
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes_from:
- app
ports:
- 8080:80
As you can see you specify to use the volume from your web container.
I think the configuration file is invalid; try this code to fix [ docker-compose.yml and site.conf ]
docker-compose.yml
version: '2'
services:
web:
image: nginx
volumes:
- ./frontend/public:/var/www/html
- ./site.conf:/etc/nginx/conf.d/site.conf
ports:
- "8080:80"
environment:
- NGINX_HOST=impressive.local
- NGINX_PORT=80
links:
- php
php:
image: php:7-fpm
volumes:
- ./api:/var/www/api
- ./frontend/public:/var/www/html
site.conf
server {
index index.html index.php;
server_name impressive.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /api/ {
alias /var/www/api;
}
location ~ ^/api/(.+\.php)$ {
alias /var/www/api;
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;
}
# 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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
finally, run docker-compose build and docker-compose up

Categories