Multiple process for user - php

I looking how allow run multiple process for one user in symfony.
I have this behavior in Symfony / Nginx / Php FPM
but I test in clean symfony and php webserver:
composer create-project symfony/website-skeleton my-project
this is docker config :
version: "3.1"
services:
webserver:
image: nginx:alpine
container_name: viz-webserver
working_dir: /application
volumes:
- .:/application
- ./docker/nginx/nginx_all.conf:/etc/nginx/nginx.conf
networks:
itb_net:
ipv4_address: 10.2.0.6
php-fpm:
build: itb_docker/php-fpm
container_name: php-fpm
working_dir: /application
networks:
itb_net:
ipv4_address: 10.2.0.8
volumes:
- ~/.ssh/:/root/.ssh/
- .:/application
- ./docker/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini
- ./.aws/credentials:/var/www/.aws/credentials
php-ini-overrides.ini
upload_max_filesize = 200M
post_max_size = 208M
memory_limit = 4048M
php-fpm Dockerfile
FROM phpdockerio/php72-fpm:latest
WORKDIR "/application"
# Fix debconf warnings upon build
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
&& apt-get -y --no-install-recommends install php-zip php-xdebug php-gd php-xml php7.2-mysql php7.2-intl php7.2-tidy php7.2-xmlrpc php7.2-soap php7.2-bcmath php-mbstring php-curl\
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
# Install git
RUN apt-get update \
&& apt-get -y install git acl wkhtmltopdf xvfb \
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
nginx
user nginx;
worker_processes 2;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /dev/stdout main;
#sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
server {
# limit_conn conn_limit_per_ip 10;
# limit_req zone=req_limit_per_ip burst=10 nodelay;
fastcgi_keep_conn on;
#proxy_buffering off;
gzip off;
listen 80 default;
client_max_body_size 208M;
access_log /var/log/nginx/application.access.log;
root /application/public;
rewrite ^/index\.php/?(.*)$ /$1 permanent;
try_files $uri #rewriteapp;
location #rewriteapp {
rewrite ^(.*)$ /index.php/$1 last;
}
# Deny all . files
location ~ /\. {
deny all;
}
location ~ ^/(index)\.php(/|$) {
fastcgi_pass php-fpm:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_index app_dev.php;
send_timeout 1800;
fastcgi_read_timeout 1800;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
include fastcgi_params;
}
# Statics
location /(bundles|media) {
access_log off;
expires 30d;
try_files $uri #rewriteapp;
}}
}
test controller:
class TestController extends Controller
{
/**
* #Route("/test1")
*/
public function aAction(){
$i = 0;
while($i<50){
echo "$i<BR>";
$i++;
sleep(2);
}
}
/**
* #Route("/test2")
*/
public function bAction(){
return new Response('OK');
}
}
and same result - go to /test1 and test2 in second tab = second tab wait to first finish.

The built-in PHP server is single-threaded. If you get a long or hanged request it can't serve the next request until the thread is free to serve the incoming request.
As per http://php.net/manual/en/features.commandline.webserver.php
The web server runs a only one single-threaded process, so PHP applications will stall if a request is blocked.

Related

502 Bad Gateway Docker Symfony Nginx

I have a problem with my docker configuration, my home page on localhost return always 502 Bad Gateway.
Here you can see the architecture of the projet :
https://i.stack.imgur.com/Bfc24.png
the four container running :
https://ibb.co/XpQvnXj
I can access to phpmyadmin :
https://ibb.co/FXLqg8B
But when I go to the nginx port I have this issue :
https://ibb.co/2vs72jF
And this is the contents of the files.
docker-compose.yml :
version: '3'
services:
php:
build:
context: ./php
ports:
- "9000:9000"
volumes:
- ../symfony:/var/www
nginx:
container_name: "nginx"
build:
context: ./nginx
volumes:
- ../symfony:/var/www
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./logs:/var/log
depends_on:
- php
ports:
- "8081:80"
db:
image: mysql:5.6
restart: always
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8080:80
environment:
PMA_HOST: db
depends_on:
- db
php/Dockerfile :
FROM php:8.0.3-fpm
RUN apt-get update \
&& apt-get install -y --no-install-recommends locales apt-utils git libicu-dev g++ libpng-dev libxml2-dev libzip-dev libonig-dev libxslt-dev;
RUN curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
RUN curl -sSk https://getcomposer.org/installer | php -- && \
mv composer.phar /usr/local/bin/composer
RUN docker-php-ext-configure intl
RUN docker-php-ext-install mysqli
RUN docker-php-ext-install pdo pdo_mysql gd opcache intl zip calendar dom mbstring zip gd xsl
RUN pecl install apcu && docker-php-ext-enable apcu
WORKDIR /var/www
CMD composer install ; php-fpm
EXPOSE 9000
nginx/Dockerfile :
FROM nginx:alpine
WORKDIR /var/www
CMD ["nginx"]
EXPOSE 80
nginx/nginx.conf :
user nginx;
worker_processes 4;
daemon off;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
keepalive_timeout 65;
upstream phpserver {
server php:9000;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /var/www/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass phpserver;
fastcgi_index index.php;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 600;
include fastcgi_params;
}
location ~ /\.ht {
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
deny all;
}
}
}
error.log :
2021/10/13 13:53:04 [error] 25#25: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.16.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://192.168.16.3:9000", host: "localhost:8081"
2021/10/13 13:53:04 [error] 25#25: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.16.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://192.168.16.3:9000", host: "localhost:8081", referrer: "http://localhost:8081/"
I'm new to Docker so the problem is probably obvious

Nginx on Docker serves only welcome pages

I'm using macOS. I'm working on a Debian image created through Dockerfile. Nginx, php-fpm was installed in Debian image. Then I copied server file to /etc/nginx/sites-available/server and created its symbolic link file in /etc/nginx/sites-enabled/. It also copied srcs/info.php files to /var/www/server/info.php.
After starting nginx service, I can access my private IP address 192.168.0.46 and see NGINX's welcome page. However, when accessing 192.168.0.46/info.php, page opening fails. This is the same when other html files are inserted.
I looked it up on Google and found out that it was an INCLUDE problem of /etc/nginx/nginx.conf, but there was no problem.
The first question I suspected was the firewall, but if it was the problem, shouldn't I not be able to see the welcome page of Nginx?
I've been thinking and searching all day, but I couldn't get an answer. Please help me!
Here is my files:
Dockerfile :
FROM debian:buster
ARG DEBIAN_FRONTEND=noninteractive
ENV USER=root
COPY srcs/server.sites-available /etc/nginx/sites-available/server
COPY srcs/info.php /var/www/server/info.php
COPY srcs/login_form.html /var/www/server/login_form.html
# Installed mariadb-server instead of mysql-server
RUN apt-get update && apt-get upgrade -y && \
apt-get install -y --no-install-recommends --no-install-suggests \
nginx \
openssl \
mariadb-server \
php-fpm \
php-mysql && \
ln -s /etc/nginx/sites-available/server /etc/nginx/sites-enabled/
CMD service mysql start; \
service nginx start; \
bash;
# EXPOSE 80 for HTTP and 443 for HTTPS
EXPOSE 80 443
server.sites-available :
server {
listen 80;
listen [::]:80;
root /var/www/server;
index index.php index.html index.htm;
server_name server;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}
info.php :
<?php
phpinfo();
?>
nginx.conf :
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Here is my commands:
$ docker build -t server_image .
$ docker run -it -P --rm --name server_container server_image
The server_name server directive means that only requests for http://server will be served.
You should add 192.168.0.46 server to your hosts file.
I'd also recommend changing the server_name to something else like myserver.local (and add that to the hosts file)
use docker run -it -p 80:80 --rm --name server_container server_image to fix it.
But I don't know why...

Using nginX and HTTPS downloads the index.php file instead of rendering it

I am using docker with nginx.
When I go to:
http://www.mypage.com
Everything is fine, however when I add HTTPS it literally just downloads index.php.
There is nothing written in the logs so I am not sure where to even start to fix this.
Here is my relevant configuration:
Docker-compose.yml:
php:
image: myImage
ports:
- "9000:9001"
volumes:
- home/me/my-site/:/var/www/symfony:cached
- ./logs/symfony:/var/www/symfony/var/log:cached
extra_hosts:
- "docker-host.localhost:127.0.0.1"
- "otherhost:10.5.221.132"
nginx:
build: ./nginx
ports:
- "80:80"
- "443:443"
links:
- php
volumes:
- ./logs/nginx:/var/log/nginx:cached
- /home/me/my-site/:/var/www/symfony:cached
- ./nginx/my-site.com.crt:/etc/nginx/my-site.com.crt
- ./nginx/my-site.com.key:/etc/nginx/my-site.com.key
My dockerfile:
FROM alpine:3.8
RUN apk add --update nginx
RUN rm -rf /var/cache/apk/* && rm -rf /tmp/*
ADD nginx.conf /etc/nginx/
ADD symfony.conf /etc/nginx/conf.d/
ADD fastcgi_params /etc/nginx/
ADD my-site.com.crt /etc/nginx/
ADD my-site.com.key /etc/nginx/
RUN echo "upstream php-upstream { server php:9000; }" > /etc/nginx/conf.d/upstream.conf
RUN adduser -D -g '' -G www-data www-data
CMD ["nginx"]
EXPOSE 80
EXPOSE 443
My nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
http {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
error_log off;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
open_file_cache max=100;
client_body_temp_path /tmp 1 2;
client_body_buffer_size 256k;
client_body_in_file_only off;
server {
listen 443 ssl;
server_name symfony.localhost;
ssl_certificate /etc/nginx/my-site.com.crt;
ssl_certificate_key /etc/nginx/my-site.com.key;
root /var/www/symfony/public/;
index index.php;
}
}
daemon off;
You dont have any php processing in your nginx config, you need something like below to listen to php files
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;
}
More info https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/#connecting-nginx-to-php-fpm

Can't make nginx and php-fpm dockers communicate

I have created two docker images to match my needs, here is the nginx one:
FROM alpine:3.3
RUN apk add --update nginx
EXPOSE 80 443
CMD nginx -c /www/dev/nginx/conf/nginx.conf -g 'daemon off;'
then the php-fpm
FROM php:7.0-fpm
RUN apt-get update
RUN apt-get install -y apt-transport-https ca-certificates dcmtk libgdcm-tools wkhtmltopdf libdbd-freetds libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
&& docker-php-ext-install -j$(nproc) iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
RUN apt-get install -y imagemagick --fix-missing
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
WORKDIR /www/public
CMD rm /etc/freetds.conf
ADD freetds.conf /etc/freetds.conf
CMD rm /var/cache/apk/*
Then the run commands in the following order
docker create -p 9000:9000 --name php -v /www:/www:rw php
docker start php
docker create --privileged=true -p 80:80 -p 443:443 --name nginx -v /www:/www --link php:php nginx
docker start nginx
So far all works good, containers are running and I can even get static content from nginx owever any php script fails with
[error] 6#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: platform.v2.vetology.net, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.17.0.2:9000", host: "localhost.localdomain"
doing
cgi-fcgi -bind -connect 127.0.0.1:9000
from the host machine works and php responds so I know both containers ar working and responsive they just don't communicate.
I read in a different thread that the issue is caused by php and nginx using a different root directory but here is not the case as both have the /www directory mounted and /www/public contains the index.php file I am trying to open.
And here is the nginx.conf
user nobody;
error_log /www/dev/nginx/log/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
worker_processes 1;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /www/dev/nginx/log/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
include /etc/nginx/conf.d/*.conf;
default_type application/octet-stream;
gzip on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
include /www/dev/nginx/conf/upstream.conf;
server {
listen 80;
listen 443 ssl;
listen [::]:443 default ipv6only=on;
listen [::]:80 default_server ipv6only=on;
ssl_certificate /www/dev/ssl/certificate.crt;
ssl_certificate_key /www/dev/ssl/key.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
root /www/public;
index index.php index.html index.htm;
server_name hostname.net;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
client_body_buffer_size 128k;
client_body_temp_path /www/dev/nginx/tmp/client_body_temp;
proxy_temp_path /www/dev/nginx/tmp/proxy_temp_path;
fastcgi_temp_path /www/dev/nginx/tmp/fastcgi_temp_path;
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param HTTP_PROXY "";
fastcgi_pass vetology;
fastcgi_index index.php;
include fastcgi_params;
}
}
}
Any ideas of what I maybe doing wrong?
First off, I'm not sure you want to start the nginx container with privileged. There's also no need to mount the php directive with the :rw switch. rw is the default mode.
Second, take a look at where you're telling nginx to send the php request -- specifically, which host, which is defined in nginx's fastcgi_pass directive. You have it sending the php request to a machine named vetology. But docker is linking the containers together with --link php:php. That means its taking the container named php and giving it the alias named php. That means it will be exposed to your nginx container as a machine named php.
You can try this out:
docker run -it --rm --link php:some_php nginx sh -c 'ping some_php'
Change
fastcgi_pass vetology;
to:
fastcgi_pass php:9000;
And your container should work.
For complex container assemblies like this one, have a look at docker-compose next.

Nginx and php-fpm docker compose

I'm trying to create a development environnement for PHP project with Docker-compose.
I just created a docker-compose file that contains 3 docker:
Application: that mounts my code volume
Php: that runs php5-fpm
Nginx: that runs the nginx web server.
I can get an html file through my web server but when I try with a php file I get a 502 Bad Request error.
Error.log:
2016/09/19 16:51:18 [error] 8#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: xxx, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.17.0.3:9000", host: "xxx"
I think that nginx can't connect to php.
Here is my docker-compose file:
# import the code and bind it on /var/www
application:
build: code
volumes:
- ./sites:/var/www/
tty: true
# php container
php:
build: php-fpm
volumes_from:
- application
expose:
- 9000:9000
volumes:
- ./logs/php5-fpm/:/var/log
# nginx container
nginx:
build: nginx
ports:
- 80:80
links:
- php
volumes_from:
- application
volumes:
- ./logs/nginx:/var/log/nginx
php-fpm/Dockerfile:
FROM debian:jessie
RUN apt-get update && apt-get install -y php5-common php5-cli php5-fpm php5-mcrypt php5-mysql php5-gd php5-imagick php5-curl php5-intl php5-memcache php5-imap
RUN usermod -u 1000 www-data
CMD ["php5-fpm", "-F"]
EXPOSE 9000
nginx/Dockerfile:
FROM debian:jessie
RUN apt-get update && apt-get install -y nginx
ADD nginx.conf /etc/nginx
ADD test.conf /etc/nginx/sites-available/
RUN ln -s /etc/nginx/sites-available/test.conf /etc/nginx/sites-enabled/test.conf
RUN rm /etc/nginx/sites-enabled/default
RUN echo "upstream php-upstream { server php:9000; }" > /etc/nginx/conf.d/upstream.conf
RUN usermod -u 1000 www-data
CMD ["nginx"]
EXPOSE 80
nginx/nginx.conf:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
error_log off;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
daemon off;
nginx/test.conf:
server {
listen 80;
server_name xxx;
root /var/www/test;
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_log /var/log/nginx/test_error.log;
access_log /var/log/nginx/test_access.log;
}
There is no error when launching the docker-compose.
Can someone help me ?
Thanks

Categories