what the problem with nginx configuration with php? - 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

Related

502 Bad Gateway - Google Cloud Run with php-fpm (socket not found) on cold starts

I am dealing with an issue where the first request towards my Google Cloud Run container results in a 502 Bad Gateway error. The following can be seen in the logs:
[crit] 23#23: *3 connect() to unix:/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 169.254.8.129, server: domain.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.4-fpm.sock:", host: "my.domain.com"
However, on the second request it works fine.
I am using supervisord together with php-fpm and nginx.
Dockerfile
FROM php:7.4-fpm
ENV COMPOSER_MEMORY_LIMIT='-1'
RUN apt-get update && \
apt-get install -y --force-yes --no-install-recommends \
nginx \
supervisor \
libmemcached-dev \
libzip-dev \
libz-dev \
libzip-dev \
libpq-dev \
libjpeg-dev \
libpng-dev \
libfreetype6-dev \
libssl-dev \
openssh-server \
libmagickwand-dev \
git \
cron \
nano \
libxml2-dev \
libreadline-dev \
libgmp-dev \
mariadb-client \
unzip
RUN docker-php-ext-install exif zip pdo_mysql intl
#####################################
# PHPRedis:
#####################################
RUN pecl install redis && docker-php-ext-enable redis
#####################################
# Imagick:
#####################################
RUN pecl install imagick && \
docker-php-ext-enable imagick
#####################################
# PHP Memcached:
#####################################
# Install the php memcached extension
RUN pecl install memcached && docker-php-ext-enable memcached
#####################################
# Composer:
#####################################
# Install composer and add its bin to the PATH.
RUN curl -s http://getcomposer.org/installer | php && \
echo "export PATH=${PATH}:/var/www/vendor/bin" >> ~/.bashrc && \
mv composer.phar /usr/local/bin/composer
# Source the bash
RUN . ~/.bashrc
#####################################
# Laravel Schedule Cron Job:
#####################################
# RUN echo "* * * * * www-data /usr/local/bin/php /var/www/artisan schedule:run >> /dev/null 2>&1" >> /etc/cron.d/laravel-scheduler
# RUN chmod 0644 /etc/cron.d/laravel-scheduler
#
#--------------------------------------------------------------------------
# NGINX
#--------------------------------------------------------------------------
#
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log
ADD infrastructure/nginx/nginx.conf /etc/nginx/sites-available/default
ADD infrastructure/supervisor/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
#
#--------------------------------------------------------------------------
# Move config files
#--------------------------------------------------------------------------
#
ADD ./infrastructure/php/php.ini /usr/local/etc/php/conf.d/php.ini
ADD ./infrastructure/php/php-fpm.conf /usr/local/etc/php-fpm.d/zz-docker.conf
#
#--------------------------------------------------------------------------
# Ensure fpm socket folder is present
#--------------------------------------------------------------------------
#
RUN mkdir -p /run/php/
RUN rm -r /var/lib/apt/lists/*
RUN usermod -u 1000 www-data
COPY ./infrastructure/entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh
RUN ln -s /usr/local/bin/entrypoint.sh /
ENTRYPOINT ["entrypoint.sh"]
WORKDIR /var/www
COPY . .
RUN chown -R www-data:www-data /var/www
RUN composer install
EXPOSE 8000
entrypoint.sh
#!/bin/bash
# Start the cron service.
service cron start
##
# Run artisan migrate
##
php artisan migrate
##
# Run a command or start supervisord
##
if [ $# -gt 0 ];then
# If we passed a command, run it
exec "$#"
else
# Otherwise start supervisord
/usr/bin/supervisord
fi
Nginx.conf
server {
listen 8000 default_server;
root /var/www/public;
index index.html index.htm index.php;
server_name my.domain.com;
charset utf-8;
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; }
# Remove index.php$
if ($request_uri ~* "^(.*/)index\.php/*(.*)") {
return 301 $1$2;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_buffering off;
fastcgi_index index.php;
include fastcgi_params;
}
error_page 404 /index.php;
}
phpfpm custom config
[global]
daemonize = no
[www]
listen = /run/php/php7.4-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
supervisord
[supervisord]
nodaemon=true
user=root
[program:nginx]
command=nginx -g "daemon off;"
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:php-fpm]
command=php-fpm
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
Cloud Run containers being cold started will need to perform several tasks before being able to serve requests according to the documentation. This could be the cause of the first request failing, while subsequent requests are served correctly:
The startup routine consists of:
starting the service,
starting the container,
running the entrypoint command to start your server,
checking for the open service port
The same documentation page offers several tips to avoid cold starts and improve container startup performance, specially, setting a number of minimum instances that are always idle. Since Cloud Run can scale down to 0 instances, this will create cold starts when there are no instances active.
As for the specific error message from the logs, it seems that a path from your nginx.conf file might be wrong according to this related thread. According to the thread, the path to fpm should be:
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; //with your specific fpm version

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.

Configuration PHP Dockerfile from ubuntu image

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.

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!

Categories