Configuration PHP Dockerfile from ubuntu image - php

I tried to execute docker-compose up with php7-fpm image from docker hub and it wor'ks, so it's my docker which is bad configured, can somehone help me ?
I got a problem using docker-compose with Dockerfile image. I explain you what I need, I need my nginx server which comunicates with php. php should communicates with Mysql database.
I just use a Dockerfile to add modules to php.
Here is my php dockerfile :
FROM ubuntu:18.04
# BLOCK : Make PHP works
RUN apt-get update
RUN apt-get -y install software-properties-common
RUN add-apt-repository ppa:ondrej/php
# FIN BLOCK
RUN apt-get -y update && apt-get install -y \
php7.0 \
php7.0-fpm \
php7.0-mysql \
php7.0-json \
php7.0-curl \
php7.0-sqlite3 \
php7.0-xml \
php7.0-bcmath \
php7.0-zip \
php7.0-mbstring \
php-xdebug \
php-ast
WORKDIR /var/www/html
CMD ["php-fpm"]
EXPOSE 9000
NGINX config
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/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;
}
}
When I execute docker-compose --verbose up I got an exit code
Here is --verbose and architecture file

I am using NGINX Unit as an app server for PHP since a little bit more then 2 years. It makes it easier to use PHP applications running on PHP with NGINX without installing / creating a complex docker container image by yourself.
Here is an example of an UNIT build for Wordpress. But it will work with any other PHP Application as well.
https://github.com/tippexs/unitwp
I would give it a try. The official documentation:
https://unit.nginx.org/configuration/#php
But back to your problem:
I would not using Ubuntu 14.04 anymore if not absolutley needed. Use a newer image as your base image.
What package are you using to install PHP7.0? I just used a fresh ubuntu:14.04 docker image and tried to install php7.0 without adding any special repo or ppa.
root#31bb99d337d9:/# apt search "php7"
Sorting... Done
Full Text Search... Done
Can't find any PHP7 package. I know there are some ppa's for PHP7 on Ubuntu14.04.
To make it work with my fresh Ubunut 14.04:
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
Your PHP-Image does not include an FCGI-proccesor.
Your NGINX configuration needs an fcgi-processor listen on port 9000 in the php container. You are not installing it in your Dockerfile.
root#31bb99d337d9:/# apt search php5-fpm
Sorting... Done
Full Text Search... Done
php5-fpm/trusty-updates,trusty-security 5.5.9+dfsg-1ubuntu4.29 amd64
server-side, HTML-embedded scripting language (FPM-CGI binary)
You should try to find the fpm for php7 in your container.
The fpm needs some additional configuration in your php container as well as it need to be started on container startup.
If have the chance go into your nginx and / or php container instance and check if the fpm is running and listening on port 9000 to accept connections from the nginx container.
I did that many times in the past and migrated anything to NGINX Unit because its much easier to use it. Give it a try.

Related

what the problem with nginx configuration with php?

I try to make Docker image with nginx and php on centos7 base image
this is my base image
FROM centos:7
RUN yum -y install openssh-server
RUN useradd remote_user && \
echo "1234" | passwd remote_user --stdin && \
mkdir /home/remote_user/.ssh && \
chmod 700 /home/remote_user/.ssh
COPY remote-key.pub /home/remote_user/.ssh/authorized_keys
RUN chown remote_user:remote_user -R /home/remote_user && \
chmod 600 /home/remote_user/.ssh/authorized_keys
RUN /usr/sbin/sshd-keygen
RUN yum install -y python3
RUN yum -y install mysql
CMD /usr/sbin/sshd -D
this is my nginx and php Docker image
FROM remote-host
COPY ./conf/nginx.repo /etc/yum.repos.d/nginx.repo
RUN yum -y update
RUN yum -y install nginx-1.12.2 openssl --enablerepo=nginx
RUN yum -y install vim net-tools
RUN yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y
RUN yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
RUN yum install yum-utils -y
RUN yum-config-manager --disable 'remi-php*' && \
yum-config-manager --enable remi-php71
RUN yum install php-cli -y
RUN yum install -y install \
php-fpm \
php-mysqlnd \
php-soap \
php-xml \
php-zip \
php-json \
php-mcrypt \
php-mbstring \
php-zip \
php-gd
RUN yum clean all
EXPOSE 80
VOLUME /var/www/html /var/log/nginx /var/log/php-fpm /var/lib/php-fpm /sock
COPY ./conf/nginx.conf /etc/nginx/conf.d/default.conf
COPY index.php /var/www/html/
RUN chown nginx:nginx -R /var/www/html/
COPY ./bin/start.sh /start.sh
RUN chmod +x /start.sh
CMD /start.sh
this is my nginx config
server {
listen 80;
server_name jenkins.local.com;
root /var/www/html;
index index.php index.html index.htm;
access_log /var/log/nginx/localhost-access.log;
error_log /var/log/nginx/localhost-error.log;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/sock/docker.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
in the index.php is the simple php function
<?php
phpinfo();
?>
when try to up the containers , in the web page after open jenkins.local.com , I faced the 502 Bad Gateway
I know its relates to the fastcgi_pass , but i dont know what should i do
could you please help me
this related to the nginx configuration and php-fpm

Cannot find php-fpm www.sock file on Docker with nginx and php-fpm

I am working to start the laravel app on Docker
I am having connect() to unix:/run/php-fpm/www.sock failed (2: No such file or directory) error and there is no php-fpm folder in run folder.
Why is not there a www.sock file created?
server.conf
server {
listen 8080;
root /home/app/public_html/public;
index index.php;
charset utf-8;
gzip on;
gzip_types text/css application/javascript text/javascript application/x-javascript image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php {
include fastcgi.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php-fpm/www.sock; //this crashes
}
location ~ /\.ht {
deny all;
}
}
Dockerfile
FROM centos:8
RUN dnf install nginx -y
RUN yum -y install yum-utils
RUN dnf install -y dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm
RUN dnf module reset php
RUN dnf -y module enable php:remi-7.4
RUN dnf install -y php php-cli php-mysqlnd php-zip php-devel php-pdo php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json php-fileinfo php-intl
RUN yum -y install make
RUN yum -y install libmcrypt-devel
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN rpm -Uvh http://www.city-fan.org/ftp/contrib/yum-repo/rhel7/x86_64/city-fan.org-release-2-1.rhel7.noarch.rpm
RUN mkdir -p /usr/local/lib/php/pecl
RUN pecl install mongodb
RUN pecl install mcrypt-1.0.4
RUN rm -v /etc/nginx/nginx.conf
ADD nginx.conf /etc/nginx/
ADD server.conf /etc/nginx/conf.d/
COPY . /home/app/public_html/
COPY mongodb.ini /etc/php.d/
RUN cd /home/app/public_html && composer update
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]
In your DockerFile you need add php-fpm:
RUN dnf install -y php php-fpm php-cli php-mysqlnd php-zip php-devel php-pdo php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json php-fileinfo php-intl
Hope help you.

Dockerfile - ERROR: for php Cannot start service php: OCI runtime create failed: container_linux.go:348: starting container process caused "exec

I'm quite newbie with using Docker and I'm trying to create a local development environment for running either my custom php or laravel projects.
This is my folder structure
root-dir
- src // this is where my php / laravel code lives
-- info.php // file having phpinfo(); just to ensure that everything has set properly
- docker // this is where all containers and config settings live
-- php etc..
- docker-compose.yml
This is my docker/php/Dockerfile
FROM ubuntu:18.04
ENV TERM=linux
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install -y curl zip unzip --no-install-recommends apt-utils ca-certificates
RUN apt-get install -y --no-install-recommends php7.2-fpm \
php7.2-cli \
php7.2-mysql \
php7.2-xml \
php7.2-curl \
php7.2-bcmath \
php7.2-bz2 \
php7.2-curl \
php7.2-zip \
php7.2-gd \
php7.2-gettext \
php7.2-zip \
php7.2-soap \
php7.2-odbc \
php7.2-json \
php7.2-geoip \
php7.2-igbinary \
php7.2-imagick \
php7.2-mbstring \
php7.2-msgpack \
php7.2-ssh2 \
php7.2-memcached \
php7.2-xdebug \
php7.2-intl \
php7.2-opcache \
php7.2-readline
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/* ~/.composer
COPY php.ini /etc/php/$PHPVER/cli/php.ini
COPY php.ini /etc/php/$PHPVER/fpm/php.ini
COPY 20-xdebug.ini /etc/php/$PHPVER/cli/conf.d/20-xdebug.ini
COPY 20-xdebug.ini /etc/php/$PHPVER/fpm/conf.d/20-xdebug.ini
COPY php-fpm-startup /usr/bin/php
CMD /usr/bin/php
EXPOSE 9000
ENTRYPOINT ["/usr/bin/php"]
CMD ["--version"]
When I'm running docker-compose up -d I get this error
ERROR: for php Cannot start service php: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"/usr/bin/php\": permission denied": unknown
This is my nginx/default.conf file
server {
listen 80 default_server;
root /var/www/html;
index index.html index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
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_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
add user to your dockerfile which will execute the php binary for you something like:
....
RUN ["chmod", "+x", "/usr/bin/php"]
RUN ["chmod", "+x", "-R", "/var/log"]
EXPOSE 9000
RUN useradd -ms /bin/bash admin
USER admin
ENTRYPOINT ["/usr/bin/php"]
CMD ["--version"]
RE:chat convo
However in your concern as to why the container stopped running.
If your command finishes executing the container will terminate
If your command fails and the tty dies, the container will terminate.
In other words if your command never runs or runs and completes, as in your example
php -verson
is the code you are trying to run
when that completes the container will terminate
If you are not catching stdout you will never see this execute
and the container will terminate as if nothing happened.

Docker impossible to link nginx container with php-fpm container

Hello for my work I am doing a nginx server and php fpm server with docker, but
I do not know how to link nginx and php with fast cgi
Nginx - Docker file
FROM debian:jessie
MAINTAINER Thomas Vidal <thomas-vidal#hotmail.com>
RUN apt-get update && apt-get upgrade
RUN apt-get install -y wget
RUN wget http://nginx.org/keys/nginx_signing.key && apt-key add nginx_signing.key
RUN apt-get update && apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
RUN ln -sf /etc/nginx/conf.d /site-conf
RUN ln -sf /var/www/html /www
VOLUME ["/site-conf", "/www"]
EXPOSE 80 443
CMD nginx
Nginx - default.conf
server {
listen 80;
index index.php index.html;
server_name 192.168.99.100;
root /www;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 192.168.99.100:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Nginx - index.php
<?php phpinfo(); ?>
Php-fpm - Dockerfile
FROM debian:jessie
MAINTAINER Thomas Vidal <thomas-vidal#hotmail.com>
RUN apt-get update && apt-get upgrade
RUN apt-get install -y php5-fpm php5-cli php5-mysql php5-curl php5-mcrypt php5-gd php5-redis
RUN sed -e 's#;daemonize = yes#daemonize = no#' -i /etc/php5/fpm/php-fpm.conf
RUN sed -e 's#listen = /var/run/php5-fpm.sock#listen = [::]:9000#g' -i /etc/php5/fpm/pool.d/www.conf
EXPOSE 9000
CMD php5-fpm
What is being returned:
File not found.
Thanks for your help!

How to use nginx with PHP?

What is a good way of using PHP with nginx? From the finding I got, maybe using PHP-FPM might be a good way of handing PHP behind nginx.
The problem we have is that the free web based API we serve gets a lot of request (about 500K a day), the requests are mostly very short and small in size but Apache is consuming a lot of memory. I want to try nginx to see if it can handle it better.
Thanks.
Ubuntu Lucid 64-bit
apt-get install nginx
apt-get update
apt-get install php5-cli php5-common php5-suhosin
apt-get install python-software-properties
add-apt-repository ppa:brianmercer/php
apt-get update && apt-get install php5-fpm php5-cgi
/etc/init.d/nginx restart
/etc/init.d/php5-fpm restart
Edit (might need this in your site conf) :
location ~ \.php$ {
fastcgi_read_timeout 60000;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/site$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}

Categories