How to set up Apache2 and PHP-FPM via unix socket? - php

I try to set up Apache2 and PHP-FPM via unix socket but result is
(111)Connection refused: AH02454: FCGI: attempt to connect to Unix domain socket /run/php/php7.2-fpm.sock (*) failed
docker-compose.yml
version: "2"
services:
php:
build: "php:7.2-rc-alpine"
container_name: "php"
volumes:
- "./code:/usr/local/apache2/htdocs"
- "./php7.2-fpm.sock:/run/php/php7.2-fpm.sock"
apache2:
build: "httpd:2.4-alpine"
container_name: "apache2"
volumes:
- "./code:/usr/local/apache2/htdocs"
- "./php7.2-fpm.sock:/run/php/php7.2-fpm.sock"
ports:
- 80:80
links:
- php
www.conf
listen = /run/php/php7.2-fpm.sock
httpd-vhosts.conf
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost/"
</FilesMatch>
But it's work when connect via TCP.
www.conf
listen = 127.0.0.1:9000
httpd-vhosts.conf
<FilesMatch \.php$>
SetHandler "proxy:fcgi://php:9000"
</FilesMatch>

Okie, so have the repo helped to fix the issue.
Issue #1 - www.conf being copied in apache container
You had below statement in your apache container Dockerfile
COPY ./www.conf /usr/local/etc/php-fpm.d/www.conf
This is actually intended for the php container which will be running php-fpm and not the apache container
Issue #2 - Socket was never being created
Your volume bind - "./php7.2-fpm.sock:/run/php/php7.2-fpm.sock" was creating the socket and they were not being created by php-fpm as such. So you created a blank file and trying to connect to it won't do anything
Issue #3 - No config in php to create socket
The docker container by default create listen to 0.0.0.0:9000 inside the fpm container. You needed to override the zz-docker.conf file inside the container to fix the issue.
zz-docker.conf
[global]
daemonize = no
[www]
listen = /run/php/php7.2-fpm.sock
listen.mode = 0666
Updated docker file
FROM php:7.2-rc-fpm-alpine
LABEL maintainer="Eakkapat Pattarathamrong (overbid#gmail.com)"
RUN docker-php-ext-install \
sockets
RUN set -x \
&& deluser www-data \
&& addgroup -g 500 -S www-data \
&& adduser -u 500 -D -S -G www-data www-data
COPY php-fpm.d /usr/local/etc/php-fpm.d/
Issue #4 - Sockets being shared as volumes to host
You should be sharing sockets using a named volume, so the socket should not be on host at all.
Updated docker-compose.yml
version: "2"
services:
php:
build: "./php"
container_name: "php"
volumes:
- "./code:/usr/local/apache2/htdocs"
- "phpsocket:/run/php"
apache2:
build: "./apache2"
container_name: "apache2"
volumes:
- "./code:/usr/local/apache2/htdocs"
- "phpsocket:/run/php"
ports:
- 7080:80
links:
- php
volumes:
phpsocket:
After fixing all the issues I was able to get the php page working

Related

Access index.php from docker

I've created a docker with a database and a php server but I'm failing accessing the php file from the server.
For testing purpose I'm currently having 2 index.php in my test app ./index.php and ./app/index.php
This is my docker-compose.yml
version: '3'
services:
symfony:
build:
context: .
dockerfile: docker/Dockerfile
image: project-manager
ports:
- 80:80
db:
image: mysql
ports:
- 3306:3306
volumes:
- "./.data/db:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: root
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8080:80"
links:
- db
This is the php dockerfile
FROM php:7.4-fpm
# Install Composer
COPY --from=composer /usr/bin/composer /usr/bin/composer
# Copy all our files in the docker root
COPY . /
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f80a16af8336 project-manager "docker-php-entrypoi…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, 9000/tcp project-manager_symfony_1
d97688010adf phpmyadmin/phpmyadmin "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 0.0.0.0:8080->80/tcp project-manager_phpmyadmin_1
55781c004031 mysql "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp project-manager_db_1
In my /etc/hosts
#Project Manager
127.0.0.1 project-manager.local
I can successfully access to the phpmyadmin using project-manager.local:8080
But if I try the simple project-manager.local/ or project-manager.local/index
I've got an empty response.
Root cause:
For symfony, you bind 80:80, this means you suppose there is a port 80 open in the php container. But, you use php:7.4-fpm which will just open port 9000.
(If you install net-tools in the container & use netstat -oanltp to check, there won't be 80 port open.)
Solutions:
Option 1:
If you insist to use php-fpm, then you need another web server container to pass the 80 request to php container's 9000 port. Maybe could add a more service with nginx container, and refers to connecting-nginx-to-php-fpm to set your configure for nginx container:
fastcgi_pass symfony:9000;
Option 2:
Switch to use php:7.4-apache, which defaults has a web server in the image open the 80 port, like next:
Dockerfile:
FROM php:7.4-apache
COPY . /var/www/html
index.php:
<?php
phpinfo();
NOTE: you should copy files to /var/www/html.
In a word, you should assure the container which you expose 80:80 really have a port 80 open in the container, otherwise, your expose is useless...

How to change Document Root with Docker

I'm new to Docker. I'm trying to change Document Root for my app.
my-app directory has my php codes.
Tree
Project
- docker-compose.yml
- html
- my-app
- php
- 000-default.conf
- Dockerfile
docker-compose.yml
version: "3"
services:
php:
build: ./php/
# image: php:7.2-apache
volumes:
- ./html:/var/www/html
ports:
- 8080:80
container_name: php7.2
restart: always
Dockerfile
FROM php:7.2-apache
RUN apt-get -y update && apt-get -y install vim
COPY 000-default.conf /etc/apache2/sites-available/
000-default.conf
<VirtualHost *:8080>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/my-app
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
EnableSendfile off
</VirtualHost>
I command docker-compose up -d --build, however document root never change to my-app where I expect. I confirmed 000-default.conf has changed by COPY inside of container.
Please give me same advice.
Thanks.
You appear to have your ports back-to-front. In your docker-compose.yml file, you've set host port 8080 and container port 80 (👍) but your v-host is listening on container port 8080.
I'm not actually seeing any reason for you to override the default site config nor for you to have a Dockerfile at all. The default site serves content from /var/www/html over container port 80 so you don't need to change that.
Try this config instead
version: "3"
services:
php:
image: php:7.2-apache
volumes:
- "./html:/var/www/html"
ports:
- "8080:80"
Run the stack using
docker-compose up -d
then open http://localhost:8080/my-app/ in your browser.

docker httpd php .htacces load but php files do not

I am trying to use docker-compose to load a php application which boot with a .htaccess.
If i use a proxypassmatch it works, but only for one route, as soon as the url change, the file is unfound (i read that proxypass/reverseproxy/proxypassmatch) can't be use with .htaccess ...?
If i use the traditional setting, with a virtualhost, the .htaccess is working but the php file do not load, unstead its content appear on the screen (whatever the route).
I have spent so many hours looking everywhere without any answer... Or maybe changing the image for a single container php-httpd ? but i like the idea to separate them.
If someone know how to fix it, it would be great thank you.
See the docker-compose file
version: "3.2"
services:
php:
build: './php/'
volumes:
- ./MediterPourGrandir/:/var/www/html/
apache:
build: './apache/'
depends_on:
- php
- mysql
ports:
- "80:80"
volumes:
- ./MediterPourGrandir/:/var/www/html/
mysql:
image: mysql:5.6.40
#restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=monsupersite
- MYSQL_PASSWORD=root
ports:
- "3306:3306"
the apache Dockerfile
FROM httpd:2.4.33-alpine
RUN apk update; \
apk upgrade;
# Copy apache vhost file to proxy php requests to php-fpm container
RUN sed -i '/LoadModule rewrite_module/s/^#//g' /usr/local/apache2/conf/httpd.conf && \
sed -i 's#AllowOverride [Nn]one#AllowOverride All#' /usr/local/apache2/conf/httpd.conf
COPY demo.apache.conf /usr/local/apache2/conf/demo.apache.conf
RUN echo "Include /usr/local/apache2/conf/demo.apache.conf" \
>> /usr/local/apache2/conf/httpd.conf
the apache config file
serverName localhost
<VirtualHost *:80>
DocumentRoot /var/www/html/
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# Send apache logs to stdout and stderr
CustomLog /proc/self/fd/1 common
ErrorLog /proc/self/fd/2
</VirtualHost>
and the php Dockerfile
FROM php:7.2.7-fpm-alpine3.7
RUN apk update; \
apk upgrade;
RUN apk update --no-cache \
&& apk add --no-cache $PHPIZE_DEPS \
&& apk add --no-cache mysql-dev \
&& docker-php-ext-install pdo pdo_mysql
I finally change the image (i liked not but it looks like nobody knows how to do with this one, even on Docker forum... ), i use php:7.2.1-apache then it works great. See the config of the docker file: running-virtual-hosts-in-apache-docker-container
I connect it to the mysql's image (like it was previously) using docker-compose.
version: "3.2"
services:
php-apache:
build:
context: ./apache-php
ports:
- 80:80
volumes:
- ./MediterPourGrandir/:/var/www/html/
mysql:
image: mysql:5.6.40
#restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=monsupersite
- MYSQL_PASSWORD=root
ports:
- "3306:3306"
#volumes:
#- ./var/www/html/'monsupersite (1).sql':/docker-entrypoint-initdb.d/'monsupersite (1).sql'

Connect to MariaDB with localhost from Docker container

First of I did read thoses links
Connect to Docker MySQL container from localhost?
Connect to Mysql on localhost from docker container
From inside of a Docker container, how do I connect to the localhost of the machine?
But as a beginner with docker. It did not help me.
What you need to know:
Yes, I need localhost. I'm working on an app that interact
directly with the database. It create/remove user privileges and
allow some user to access with limited privileges from a remote
access. When initialized, the app will drop the default remote access to root and forge user and grant them full privilege on localhost.
I'm using a docker-compose.yml generated by https://phpdocker.io
Ubuntu 18.10
Docker version 18.09.3, build 774a1f4
docker-compose version 1.21.0, build unknown
I'm using docker only for development purpose. On production I use forge
./docker-compose.yml
###############################################################################
# Generated on phpdocker.io #
###############################################################################
version: "3.1"
services:
mailhog:
image: mailhog/mailhog:latest
container_name: myapp-mailhog
ports:
- "8081:8025"
redis:
image: redis:alpine
container_name: myapp-redis
mariadb:
image: mariadb:10.4
container_name: myapp-mariadb
working_dir: /application
volumes:
- .:/application
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=myapp
- MYSQL_USER=forge
- MYSQL_PASSWORD=forge
ports:
- "8083:3306"
webserver:
image: nginx:alpine
container_name: myapp-webserver
working_dir: /application
volumes:
- .:/application
- ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8080:80"
php-fpm:
build: phpdocker/php-fpm
container_name: myapp-php-fpm
working_dir: /application
volumes:
- .:/application
- ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.3/fpm/conf.d/99-overrides.ini
./phpdocker/nginx/nginx.conf
server {
listen 80 default;
client_max_body_size 108M;
access_log /var/log/nginx/application.access.log;
root /application/public;
index index.php;
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
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;
}
}
./phpdocker/php-fpm/Dockerfile (slightly modified to add mysql_client and not installing git in a second RUN command)
FROM phpdockerio/php73-fpm:latest
WORKDIR "/application"
# Fix debconf warnings upon build
ARG DEBIAN_FRONTEND=noninteractive
# Install selected extensions and other stuff
RUN apt-get update \
&& apt-get -y --no-install-recommends install \
php7.3-mysql php-redis php7.3-sqlite3 php-xdebug php7.3-bcmath php7.3-bz2 php7.3-gd \
git \
mysql-client \
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
./php-ini-overrides.ini
upload_max_filesize = 100M
post_max_size = 108M
I tried to use network_mode: host but it makes the webserver stopping with Exit 1
Ok but as remember it, localhost in mysql/mariadb means access thru the local unix socket. There are ways of sharing these between containers.
Have a look here Connection between docker containers via UNIX sockets
#F.Maden gave me the right direction. I accepted his answer but here's how I made it in details.
Basically has he said, I need to share mysqld.sock between my services mariadb and php-fpm
The first step is to share a folder between both services. Since I already
have /application that contains the docker config /application/phpdocker, I will reuse this one.
I had to create a custom my.cnf file to edit the default mariadb config configuration and add a custom socket path:
./phpdocker/mariadb/my.cnf
[mysql]
socket = /application/phpdocker/shared/mysqld.sock
[mysqld]
socket = /application/phpdocker/shared/mysqld.sock
Then I had to share the config file with my mariadb container
./docker-compose.yml
mariadb:
image: mariadb:10.4
container_name: myapp-mariadb
working_dir: /application
volumes:
- .:/application
- ./phpdocker/mariadb/my.cnf:/etc/mysql/my.cnf # notice this line
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=myapp
- MYSQL_USER=forge
- MYSQL_PASSWORD=forge
ports:
- "8083:3306"
I created a folder ./phpdocker/shared with privileges 777 where mariadb will be able to create mysqld.sock (I couldn't start mariadb with 755. In my case this is only used on local not on production so it's fine)
From the terminal
$ mkdir ./phpdocker/shared && chmod 777 ./phpdocker/shared
And now test it!
From the terminal
$ docker-compose up -d --force-recreate --build
$ docker exec -it -u $(id -u):$(id -g) myapp-php-fpm /bin/bash
Once in the container
$ mysql -u root -p -h localhost --socket=/application/phpdocker/shared/mysqld.sock
$ mysql > select user();
+----------------+
| user() |
+----------------+
| root#localhost |
+----------------+
If the problem with connection to DB persists:
We can interogate what IP DB container has:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name>
We'll receive container IP (eg: 172.21.0.3);
After this, we can place this IP into connection "host" section.
Enjoy!
Ref How can I access my docker maria db?

Why can't apache communicate with php-fpm in separate containers using Docker Desktop for Windows?

I'm running the latest stable Docker Desktop for Windows on Windows 10.
I've written a docker-compose file (see below) that builds separate containers, based on Centos 6 with SCL, one for httpd24-httpd (Apache) and one for rh-php56-php-fpm (PHP-FPM). The containers start up fine with the services reporting an OK status. If I ping a container from the other it resolves it fine. If I visit the index.html page Apache is happy and brings it up.
Apache is currently set to use proxy:fcgi:phpfpm:9000 but if I try to load a php file Apache returns a 503 error. I have tried a number of different connection options e.g proxy:fcgi:127.0.0.1:9000 -- 0.0.0.0:9000 -- 172.20.0.3:9000 (this being the phpfpm ip from docker) however Apache is just logging:
[proxy:error] [pid 60:tid ...] (...)Connection refused: AH00957: FCGI:
attempt to connect to 172.20.0.3:9000 (*) failed [proxy_fcgi:error]
[pid 60:tid ...] [client 172.20.0.1:44546] AH01079: failed to make
connection to backend: ...
I've also tried it with a proxypassmatch and still no joy.
<IfModule proxy_module>
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
</IfModule>
mod_proxy, mod_proxy_http and mod_proxy_fcgi are being loaded by Apache.
PHP-FPM is obviously also being configured. I've generally had this just set to listen on port 9000 but did also try :::9000
During the build in the development.php.dockerfile I've added:
EXPOSE 9000
and, as you can see in the compose file, httpd is linked to phpfpm so by my reckoning they should have total knowledge of each other and apache should be able to reach phpfpm:9000
Can anyone please help me work out why this isn't working?? It's driving me round the twist. Many many thanks.
docker-compose:
version: '2'
services:
httpd:
build:
context: .
dockerfile: ./docker/development.httpd.Dockerfile
environment:
- APACHE_RUN_USER=apache
- APACHE_RUN_GROUP=apache
- APACHE_LOG_DIR=/var/log/httpd24
- APACHE_RUN_DIR=/opt/rh/httpd24/root/var/run/httpd
- APACHE_LOCK_DIR=/opt/rh/httpd24/root/var/lock
- APACHE_SERVERADMIN=admin#mtf8.bar
- APACHE_SERVERNAME=foo.bar
- APACHE_SERVERALIAS="foo.foo.bar www.foo.bar"
- APACHE_DOCUMENTROOT=/var/www/html
volumes:
- ./data/www/html:/var/www/html
ports:
- "10180:80"
tty: true
networks:
- front-tier
phpfpm:
build:
context: .
dockerfile: docker/development.php.Dockerfile
environment:
- PHPFPM_RUN_USER=apache
- PHPFPM_RUN_GROUP=apache
- PHPFPM_LISTEN=9000
- PHPFPM_PM=dynamic
- PHPFPM_PM_MAX_CHILDREN=50
- PHPFPM_PM_START_SERVERS=5
- PHPFPM_PM_MIN_SPARE_SERVERS=5
- PHPFPM_PM_MAX_SPARE_SERVERS=35
- PHPFPM_LOG_DIR=/var/log/rh-php56-php-fpm
volumes:
- ./data/www/html:/var/www/html
tty: true
networks:
- front-tier
- back-tier
mysql:
build:
context: .
dockerfile: docker/development.mysql.Dockerfile
volumes:
- ./data/db:/usr/tmp/db/
- mysql:/var/lib/mysql
expose:
- 3306
ports:
- "10133:3306"
tty: true
networks:
- back-tier
volumes:
mysql:
networks:
front-tier:
driver: bridge
back-tier:
driver: bridge
www.conf
[...]
user = apache
group = apache
listen = 9000
listen.allowed_clients = httpd
[...]
00-custom.conf
[...]
<VirtualHost *:80>
[...]
<Directory "/var/www/html">
Options -Indexes +FollowSymlinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:fcgi://php:9000"
</FilesMatch>
[...]
</VirtualHost>
UPDATE
Issue was with PHP-FPM configuration file. I didn't realise it but rh-php56-php-fpm from SCL was creating 2 directories of conf files. I was modifying /opt/rh/rh-php56/register.content/etc/opt/rh/rh-php56/php-fpm.d/www.conf but the service was using /etc/opt/rh/rh-php56/php-fpm.d/www.conf
I should have made clear the full filepaths in the question!
I also removed listen.allowed_clients = httpd as it was causing a Broken pipe AH01074 Apache error. Using the container IP was fine but I don't want to head down that route. Either way not important.
Issue resolved.
The configurations were all correct but I was modifying the wrong www.conf file. rh-php56-php-fpm created 2 directories of conf files. I was modifying /opt/rh/rh-php56/register.content/etc/opt/rh/rh-php56/php-fpm.d/www.conf instead of /etc/opt/rh/rh-php56/php-fpm.d/www.conf which was used by the service.

Categories