Docker php-fpm + nginx / sometimes files not found on php file - php

I'm currently stuck on a problem.
I got a Server where i run multiple docker.
here i need to put have an php nginx one.
i got a docker-compose file like this :
version: 2
services:
web:
image: nginx:latest
ports:
- 'XXXX:80'
- 'XXXX:443'
volumes:
- ./code:./code
- ./site.conf:/etc/nginx/conf.d/default.conf
environnement:
- VIRTUAL_HOST:...
- LETSENCRYPT_HOST:....
- LETSENCRYPT_MAIL:....
networks:
- default
php:
build:
context: ./php
volumes:
- ./code:/code
networks:
- default
networks:
default:
external:
name:webproxy
My networks make me getting a automatique letsencrypt ssl
My dockerfile for php is :
FROM php:7.2-fpm
RUN apt-get update \
&& apt-get install -y --no-install-recommends libpq-dev \
&& docker-php-ext-install mysqli pdo_pgsql pdo_mysql
My config is :
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 SCIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info
}
}
My structure of folder is something like :
- imgFolder
- GameFolder
- index.html
- service.php
- imgFolder
- cssFolder
- desktop.html
- mobile.html
- main.css
All seems to be fine web i make my docker-compose compose up -d
What i meen by all seems to be fine is :
. HTML is well render
. SSL is also well apply
But sometimes (not all the time but something like half time) my php file was not found on ajax request (got a 404 file not found in networks debug)
Did someone have an idea of why my service.php is sometimes not found ?
Dunno if it could help but i got exactly the same docker compose working alongside this one (different port) and there is no prob with my php files call.

You are having this issue because you have more than one container with same hostname (php) in same network.
I simulated same scenario, and here is the result.

Related

Docker-Compose with PHP and Nginx not working on production

I have a very simple config in docker-compose with php:7-fpm and nginx that I want to use to host simple php websites.
Can someone please tell me what I did wrong?
Here is docker-compose.prod.yml:
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ../nurock/hidden_creste:/code
- ./site.prod.conf:/etc/nginx/conf.d/default.conf
php:
image: php:7-fpm
volumes:
- ../nurock/hidden_creste:/code
Here is the site.prod.conf file:
server {
listen 80;
index index.php index.html;
server_name example.com;
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;
}
}
I can compose up and the logs appear to be fine and when I run docker ps:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c268a9cf4716 php:7-fpm "docker-php-entrypoi…" 27 minutes ago Up 16 seconds 9000/tcp example_code-php-1
beaaec39209b nginx:latest "/docker-entrypoint.…" 27 minutes ago Up 16 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp example_code-web-1
Then checking the ports, I think this looks fine:
netstat -tulpn | grep :80
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 204195/docker-proxy
tcp6 0 0 :::8080 :::* LISTEN 204207/docker-proxy
You need to expose TCP port 9000 of the PHP container to made other containers able to use it (see What is the difference between docker-compose ports vs expose):
php:
image: php:7-fpm
expose:
- "9000"
...
Do you really want your sites to be available on TCP port 8080, not the standard port 80? If not, change "8080:80" to "80:80".
Besides the PHP handler, use a default location (although your site should be workable even without it, it is a bad practice to not add it to your nginx config):
location / {
try_files $uri $uri/ =404;
}
You must check the logs to find out the error. https://docs.docker.com/engine/reference/commandline/logs/
These issues can happen :
A php module is missing
user / permission are not correct. Is www-data defined in your nginx and php-fpm config ?
Use HTTPS and port 443 instead of HTTP and port 80. HTTP may be blocked by your browser. You can define a free SSL certificate with Let's Encrypt Docker image.
PHP 7.0 is EOL (end or life) since January 10, 2019. Please use PHP 8.0 or PHP 8.1. https://endoflife.date/php
Do not use use tag nginx:latest on production. You may have serious issues when you update your container, because last version will be downloaded.
Do not mount directory on production. Please use COPY in your Dockerfile.
Check the firewall on your server
Here is Docker Docker best practices : https://docs.docker.com/develop/dev-best-practices/
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
Here, I suggest this docker-compose.prod.yml
version: '3.8'
services:
web:
image: nginx:1.21
depends_on:
- my-php-container-name
container_name: my-nginx-container-name
working_dir: /code
ports:
- '80:80'
- '443:443'
volumes:
- ../nurock/hidden_creste:/code
- ./site.prod.conf:/etc/nginx/conf.d/default.conf
restart: always
php:
build: php-fpm
container_name: my-php-container-name
working_dir: /code
volumes:
- ../nurock/hidden_creste:/code
restart: always
In the same directory as this docker-compose.prod.yml file, create a php-fpm directory: mkdir php-fpm (or directory architecture written under build in docker-compose.prod.yml file.)
In php-fpm directory, please add this Dockerfile called Dockerfile
FROM php:8.1-fpm
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
WORKDIR "/code"
RUN apt-get update && apt-get install -y --no-install-recommends \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libicu-dev
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd pdo_mysql bcmath mysqli intl
Of course, add the PHP extensions that you need for your project. Here you have an example how to install gd, pdo_mysql, bcmatch, mysqli, intl. But there are others extension as curl, xml, xdebug, mcrypt, memcache, etc... https://github.com/mlocati/docker-php-extension-installer
In your nginx configuration, you should define config for HTTPS with port 443. Please also update this line fastcgi_pass php:9000;. Replace php by the container name. Of course, container name must be unique.
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass my-php-container-name:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
Then, build your set-up
docker-compose -f docker-compose.prod.yml build && docker-compose -f docker-compose.prod.yml up

Docker-Compose NGINX and PHP - Can server html files but php gives bad gateway

I am trying to run nginx and php in docker-compose. I am able to build the containers and when I browse to an html file in my browser nginx serves it. But when I try to browse a php file I just get 502 Bad Gateway.
I'm aware that it may be something to do with the server name in my nginx configuration file, since the tutorial I got it from mentions this needs to be correct. But I've tried changing it to 'localhost' and 'ip-of-my-dockerhost' with same result.
I'm also aware I should look at the log files. However I am a bit of a linux novice. When I open Portainer, go to the container called Web and execute a shell, if I go:
cd /var/log/nginx ; ls
I see two files called access.log and error.log
however if I type
cat error.log
nothing happens! I get a new blank line with a blinking cursor that never outputs anything. The shell is then 'frozen' until I press CTRL-C to kill the task.
Contents of docker-compose.yml
version: "3.7"
#############################
#
# NETWORKS
#
#############################
networks:
main:
external:
name: main
default:
driver: bridge
###########################
#
# SERVICES
#
###########################
services:
# All services / apps go below this line
portainer:
container_name: Portainer
image: portainer/portainer:latest
restart: unless-stopped
command: -H unix:///var/run/docker.sock
networks:
- main
ports:
- "$PORTAINER_PORT:9000"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- $DATADIR/portainer:/data # Change to local directory if you want to save/transfer config locally
environment:
- TZ=$TZ
<<snip as don't believe relevant - a bunch of other containers here such as influxdb, radarr etc>>
web:
container_name: Web
image: nginx:latest
networks:
- main
ports:
- 81:80
environment:
- PUID=$PUID
- PGID=$PGID
volumes:
- $CONFIGDIR/nginx/site.conf:/etc/nginx/conf.d/default.conf
- $DATADIR/nginx/code:/code
php:
container_name: php
image: php:7-fpm
networks:
- main
ports:
- 9090:9000
environment:
- PUID=$PUID
- PGID=$PGID
volumes:
- $CONFIGDIR/nginx/site.conf:/etc/nginx/conf.d/default.conf
- $DATADIR/nginx/code:/code
contents of $CONFIGDIR/nginx/site.conf
server {
index index.php index.html;
server_name 192.168.1.7; ## This is the IP address of my docker host but I've also tried 'localhost' and 'php-docker.local'
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:9090;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
9090 is an exposed port. That means you can reach localhost:9090 for the PHP container but not for internal container communication.
Change your site.conf:
fastcgi_pass php:9090; => fastcgi_pass php:9000;

Start web server environnement with docker

I am new in docker and i would like to know how to have a web server environnement with docker.
What i need :
PHP
HTTPD, NGINX
I have installed docker on Debian 9.4 and iam able to run docker commands now.
I have issued docker pull php and docker pull nginx.
I have a application running on a lamp server (Debian 7) actually
All my application is on /var/www/application
How to create docker containers for my project ? (NGINX, PHP)
Does i need a dockercompose file ? How to write it for my need ?
Does i need a dockerfile ? How to write it for my need ?
Can u please guide me a bit ;)
Thanks in advance to the community.
In future i would like to export my application on AWS Elasticbeanstalk.
Database i will use is stored on AWS cloud (Amazon RDS) so i don't need a container for database.
My recommendation is to use docker-compose. this is a sample LEMP:
docker-compose.yml
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./your_project:/your_project
- ./nginx.conf:/etc/nginx/conf.d/default.conf
links:
- php
php:
build: .
volumes:
- ./your_project:/your_project
links:
- mysql
mysql:
image: mysql:latest
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=password
Dockerfile
FROM php:fpm
RUN apt-get update
RUN docker-php-ext-install mysql mysqli
RUN echo "localhost localhost.localdomain" >> /etc/hosts
nginx.conf
server {
index index.php index.html;
server_name your.domain;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /your_project;
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;
}
}

How can I connect a PHP container with nginx container by using docker-compose?

I'm trying to create a simple LAMP stack in a Docker enviroment. It worked by running a third-party container phpdockerio/php71-fpm:latest, but I wanted a custom PHP container with XDebug installed, for the moment.
My problem is that, if I execute docker-compose up, the PHP container exit after startup before my webserver container can make usage of it. How can I successfully tell the PHP container to wait for a connection of my nginx container?
Command-Line Output
PS C:\playground> docker-compose.exe up
Starting playground_php_1
Starting playground_web_1
Attaching to playground_php_1, playground_web_1
playground_php_1 exited with code 0
playground_web_1 exited with code 1
Dockerfile
FROM php:latest
RUN pecl install xdebug \
&& docker-php-ext-enable xdebug
ENTRYPOINT ["docker-php-entrypoint"]
CMD ["php", "-a"]
docker-compose.yml
version: '2'
services:
php:
build:
context: ./etc/php/
dockerfile: Dockerfile
volumes:
- './src:/usr/share/nginx/html'
web:
image: nginx:latest
ports:
- 8080:80
volumes:
- './etc/nginx:/etc/nginx/conf.d'
- './src:/usr/share/nginx/html'
depends_on:
- php
nginx configuration
...
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
...
I got it running, again, with my custom container. I changed the base image from php to php-fpm.
Next thing I did, was that I had to clear running containers and to delete already created images on my machine. Otherwise docker-compose would use the wrong/old container again.

Docker php set up installing pdo extension

I am using docker to build my dev enviroment, a very simple env where all i have is nginx and php-fpm.
So following docker docks I have created a docker-compose.yml:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/site.conf
links:
- php
php:
image: php:7-fpm
volumes:
- ./code:/code
So what happens here i call nginx image and pgp-fpm image and link them.
I have also created a site.conf file:
server {
index index.php;
server_name php-docker.local;
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;
}
}
So here is all the server stuff.
with this set up i have sucesfully runned phpinfo(). When i However aded my app code i get an error complaning about pdo pdo_mysql extensions. So i did more research and found out i need a Dockerfile.
I created a docker file and and inside it added this commands:
FROM php:7
RUN docker-php-ext-install pdo pdo_mysql
Of course this does not work, I mean where does this install the extensions and how does my php-fpm supoose to see these extensions...?
This Dockerfile help me:
FROM php:fpm
RUN apt-get update \
&& apt-get install -y --no-install-recommends libpq-dev \
&& docker-php-ext-install mysqli pdo_pgsql pdo_mysql
And this is part my docker-composer.yml
phpfpm:
build:
context: ./php-fpm
dockerfile: Dockerfile
container_name: test-php-fpm
links:
- mysql
volumes:
- ./html:/usr/share/nginx/html

Categories