php and xdebug module in docker - php

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.

Related

handle nginx with multiple php version folder wise

I have a Laravel Project setup with docker. The project is working fine but I got some old PHP core codes running in one of the folders. I have set up two PHP versions in docker :
services:
nginx:
# will build ./nginx/Dockerfile
build: ./nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
volumes:
- ./src:/var/www/html
# - ./php.ini:/usr/local/etc/php/php.ini
ports:
- "9000:9000"
networks:
- laravel
php56:
build:
context: .
dockerfile: ./config/Dockerfile
volumes:
- ./src:/var/www/html/public/php56
# - ./php.ini:/usr/local/etc/php/php.ini
ports:
- "9001:9001"
networks:
- laravel
As you can see php56 is running with PHP 5.6 and php service is running php 8.1. Let me add the dockerfile as well.
DockerFile for 8.1
FROM php:8.1-fpm-alpine3.14
ENV WORK_DIR=/var/www/html
WORKDIR ${WORK_DIR}
RUN docker-php-ext-install pdo pdo_mysql soap bcmath zip
ADD . /var/www
RUN chown -R www-data:www-data /var/www
DockerFile for 5.6
FROM php:5.6-fpm-alpine
ENV WORK_DIR=/var/www/html
WORKDIR ${WORK_DIR}
RUN docker-php-ext-install pdo pdo_mysql soap bcmath zip
As the php56 lies in Laravel public path, we need to run that specific folder with a different PHP version. Let me add the nginx.conf file as well.
server {
listen 80;
index index.php index.html;
server_name localhost;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ^~ /php56 {
root /var/www/html/public/php56;
index index.php;
try_files $uri $uri/ /php56/index.php?$query_string;
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php56:9001;
fastcgi_read_timeout 300;
}
}
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;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
}
This is my whole setup and the Laravel is working fine but the php56 folder is not working. Any help would be great. Thank you

docker-compose with nginx and php:8-fpm

I try to add php support to my nginx running inside docker. But starting of the service fails.
That is my docker-compose.yml
version: "2.4"
services:
nginxproxy:
image: nginx:mainline-alpine
command: "/bin/sh -c 'while :; do sleep 48h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
environment:
- TZ=${TZ}
volumes:
- ./data/nginx/conf:/etc/nginx/conf.d/:ro
- ./data/nginx/websites:/var/www/
depends_on:
- php-fpm
links:
- php-fpm
restart: always
network_mode: "host"
php-fpm:
image: php:8-fpm
ports:
- 9001:9000
volumes:
- ./data/nginx/websites:/var/www/
./data/nginx/websites containes severall webroots of different vhosts.
When I try to bring everything up, nginx is able to start, but php:8-fpm fails to start.
docker-compose logs does only show "Attaching to"
I am not able to find out more error messages oder whats wrong here.
I have similar issue before. It found out my php-fpm was a problem. in my default.conf. I change the default fastcgi_pass to fastcgi_pass 127.0.0.1:9000;
Here is my config
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 /usr/share/nginx/html/project;
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;
}
}
Article Reference: How to setup PHP 8, NGINX, PHP-FPM and Alpine with Docker

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

Get 502 Bad Gateway when use fastcgi_pass: 127.0.0.1:9000

I have next docker-compose file:
nginx:
build: .
ports:
- "80:80"
- "443:443"
links:
- fpm
fpm:
image: php:fpm
ports:
- "9000:9000"
The Dockerfile command list is:
FROM nginx
ADD ./index.php /usr/share/nginx/html/
# Change Nginx config here...
RUN rm /etc/nginx/conf.d/default.conf
ADD ./default.conf /etc/nginx/conf.d/
And my custom Nginx config default.conf file is:
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
error_log /var/log/nginx/localhost.error.log;
access_log /var/log/nginx/localhost.access.log;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
After docker-compose up command the static pages work fine when I get http://localhost/index.html.
But when I open http://localhost/index.php I had an error 502 Bad Gateway.
I think the problem with incorrect fastcgi_pass. Could anybody help me config fastcgi_pass in my case, please?

Categories