Giter Club home page Giter Club logo

docker-php-fpm's Introduction

PHP-FPM Docker Images

Docker container to install and run PHP-FPM.

Project Goal

Out of the box, multi-version, fully loaded PHP-FPM docker images, that can support all my PHP projects. I work with WordPress & Laravel. The images are no light weight. The aim is to support maximum number of features out of the box, that could be easily turn ON/OFF with environment settings.

Supported branches and respective Dockerfile links

What is PHP-FPM ?

PHP-FPM (FastCGI Process Manager) is an alternative FastCGI implementation for PHP.

Environment variables

Use following environment variables to configure docker container php process manager during container boot up:

PHP_UID=1000
PHP_GID=1000
PHP_HOME=/app
PHP_USER=php-fpm

will run create a system user called php-fpm with UID:GUID 1000:1000 and home directory /app, which then can be referenced in your php-fpm manager pool configuration file.

PHP_INI_PATH=/path/to/php.ini

will include specified php.ini configuration during php-fpm manager start. It allows to use a wildcard in case you would like to include several .ini configuration files.

PHP_POOL_PATH=/path/to/pool.conf

will include specified pool.conf configuration during php-fpm manager start. It allows to use a wildcard in case you would like to include several .conf configuration files. ATTENTION: default www.conf pool configuration will be loaded, unless you specify path to your custom www.conf.

boot scripts

PHP_BOOT_SCRIPTS=/path/to/*.sh

will run scripts or a single script from specified path during container boot, before php-fpm manager starts up. Useful in cases when you want to include several pools configurations, where each pool uses a different system user (shared hosting). In those cases you would need to create each system user before php-fom manager starts up. PHP_BOOT_SCRIPTS could be use to point to a bash script that will create those system users.

PHP_CRONTABS_PATH=/path/to/cronttab_scripts

will install a crontab defined in /path/to/cronttab_scripts and start crontab daemon inside container.

example Laravel crontab

#
# Laravel task scheduler
#
# ATTENTION:
# crontab sh shell requires:
# - a full path to php cli interpreter
# - current dir change to laravel artisan
# - an empty line is required at the end of this file for a valid cron file

* * * * * php-cli   cd /app && /usr/local/bin/php artisan schedule:run
NEWRELIC_LICENSE=license_string

will turn on NewRelic extension to monitor PHP application performance.

starting from latest 7.3 container the Sendgrid login & password credentials are deprecated in favor of API key.

deprecated:

SMTP_LOGIN=sendgrid_login
SMTP_PASSWORD=sendgrid_password

in favor of API key:

SENDGRID_API_KEY=api_key_string

will update default email routing via SendGrid. Google Cloud blocks SMTP port 25 by default, so this could be useful solution to set up an alternative email routing before php-fpm manager starts up.

if set, on container boot the test script will send an email using PHP mail function to given recipient address.

to support Redis or Memcached PHP session handler.

PHP_SESSION_HANDLER=php_session_handler
PHP_SESSION_PATH=php_session_path

will update default PHP session handler. Useful in cluster environments, to allow shared PHP sessions between cluster instances.

[Example Redis session](https://www.digitalocean.com/community/tutorials/how-to-set-up-a-redis-server-as-a-session-handler-for-php-on-ubuntu-14-04)

PHP_SESSION_HANDLER=redis
PHP_SESSION_PATH=tcp://redis.host:6379

This will set php.ini global session handler to use Redis server accessible at redis.host DNS endpoint name and port 6379.

[Example Memcached session](https://www.digitalocean.com/community/tutorials/how-to-share-php-sessions-on-multiple-memcached-servers-on-ubuntu-14-04)

PHP_SESSION_HANDLER=memcached
PHP_SESSION_PATH=memcached.host:11211

This will set php.ini global session handler to use Memcached server accessible at memcached.host DNS endpoint name and port 11211.

SUPERVISORD_PATH=/path/to/supervisord.conf

Allows to control and monitor multiple processes running inside the container. Example use case: ensure that there are minimum 8 simultaniously run Laravel Queues available at any time to process scheduled tasks.

Note that if you use supervisord the container boot script will create a /healthcheck file to monitor supervisord main process, which can be used to monitor container health. This example configuration for docker-compose.yaml will ensure that container does not exit after boot and redirect supervisord logs into stdout.

    command: [ "tail", '-f', '/var/log/supervisor/supervisord.log' ]
    healthcheck:
      test: /healthcheck
      retries: 3
      timeout: 5s
      interval: 5s

php access log (on|off)

PHP_ACCESS_LOG=off

turns on|off php access log to docker container stdout.

php error log (on|off)

PHP_ERROR_LOG=on

turns on|off php error log to docker container stdout.

Installed extensions

  • apc
  • apcu
  • bcmath
  • bz2
  • calendar
  • Core
  • ctype
  • curl
  • date
  • dba
  • dom
  • ds
  • enchant
  • exif
  • fileinfo
  • filter
  • ftp
  • gd
  • gettext
  • gmp
  • hash
  • iconv
  • igbinary
  • imagick
  • imap
  • interbase
  • intl
  • json
  • ldap
  • libxml
  • mbstring
  • memcache
  • memcached
  • mongodb
  • msgpack
  • mysqli
  • mysqlnd
  • newrelic
  • openssl
  • pcntl
  • pcre
  • PDO
  • pdo_dblib
  • pdo_mysql
  • pdo_pgsql
  • pdo_sqlite
  • pdo_sqlsrv
  • pgsql
  • Phar
  • posix
  • pspell
  • readline
  • recode
  • redis
  • Reflection
  • session
  • shmop
  • SimpleXML
  • soap
  • sockets
  • sodium
  • SPL
  • sqlite3
  • ssh2
  • standard
  • sysvmsg
  • sysvsem
  • sysvshm
  • test
  • tidy
  • tokenizer
  • wddx
  • xdebug
  • xml
  • xmlreader
  • xmlrpc
  • xmlwriter
  • xsl
  • Zend OPcache
  • zip
  • zlib

Installed Zend Modules

  • Xdebug
  • Zend OPcache

Pull latest image

docker pull crunchgeek/php-fpm:7.2

Running PHP apps

Running image

Run the PHP-FPM image, mounting a directory from your host.

docker run -it --name php-fpm -v /path/to/your/app:/app crunchgeek/php-fpm:7.2 php script.php

or using Docker Compose:

version: '3'
services:
  php-fpm:
    container_name: php-fpm
    image: crunchgeek/php-fpm:7.3
    entrypoint: php index.php
    volumes:
      - /path/to/your/app:/app

Running as server

docker run --rm --name php-fpm -v /path/to/your/app:/app -p 8000:8000 crunchgeek/php-fpm:7.2 php -S 0.0.0.0:8000 /app/index.php

Logging

docker logs php-fpm

Listing installed extensions

docker run --rm -it crunchgeek/php-fpm:7.2 php -m

Release Notes

PHP-FPM 7.4

Extensions that failed to build from 7.3 to 7.4:

  • mhash (Implemented RFC: The hash extension is now an integral part of PHP and cannot be disabled)
  • interbase (Unbundled the InterBase extension and moved it to PECL)
  • recode (Unbundled the recode extension)
  • wddx (Deprecated and unbundled the WDDX extension)
  • docker-php-ext-configure gd --with-png only PNG

docker-php-fpm's People

Contributors

eom avatar markhilton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

docker-php-fpm's Issues

Connection refused

nginx failed (111: Connection refused) while connecting to upstream, when it was running in k8s.
here is php-svc

apiVersion: v1
kind: Service
metadata:
  name: php-svc
spec:
  selector:
    name: php
  ports:
  - port: 9000
    name: http-php
    targetPort: 9000
    protocol: TCP
 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-php-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      name: php
  template:
    metadata:
      labels:
        name: php
    spec:
      containers:
      - name: php
        image: crunchgeek/php-fpm:7.2
        env:
        - name: PHP_CRONTABS_PATH
          value: /app/cronttab_scripts
        ports:
        - name: http-php
          containerPort: 9000
        volumeMounts:
        - name: php-code
          mountPath: /var/www/html/
        - name: php-app
          mountPath: /app
      volumes:
      - name: php-code
        nfs:
          path: /home/data
          server: 192.168.83.143
      - name: php-app
        nfs:
          path: /home/data/phpCron
          server: 192.168.83.143

and crontab was invalid, this file /app/cronttab_scripts content below

* * * * * /usr/local/bin/php /var/www/html/lumen57/artisan schedule:run >> /dev/null 2>&1 &

Issues with php-fpm user

The documentation specifies that a user may be created through environment variables as follows:

system user

PHP_UID=1000
PHP_GID=1000
PHP_HOME=/app
PHP_USER=php-fpm

will run create a system user called php-fpm with UID:GUID 1000:1000 and home directory /app, which then can be referenced in your php-fpm manager pool configuration file.

However, php-fpm is always running under user www-data [33:33], which apparently comes with the image:

.
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-timesync:x:100:103:systemd Time Synchronization,,,:/run/systemd:/bin/false
systemd-network:x:101:104:systemd Network Management,,,:/run/systemd/netif:/bin/false
systemd-resolve:x:102:105:systemd Resolver,,,:/run/systemd/resolve:/bin/false
systemd-bus-proxy:x:103:106:systemd Bus Proxy,,,:/run/systemd:/bin/false
messagebus:x:104:107::/var/run/dbus:/bin/false
Debian-exim:x:105:110::/var/spool/exim4:/bin/false
newrelic:x:999:999:New Relic daemons:/opt/newrelic:/bin/false
postfix:x:106:112::/var/spool/postfix:/bin/false
php-fpm:x:82:82::/app:/bin/false

This also presents a problem, because some apache images default to www-data [82:82]. I added a user through the environment variables, php-fpm [82:82], and though it was created, php still continues to use www-data.

image

Is there any way that the image could overwrite the uid and gid of www-data, in order to synchronize with whatever user apache/nginx are using?

PHP container not refreshing script updates in Docker for Windows (WSL2/NTFS regression)

This is my second attempt at setting up a Docker environment for myself so I'm still figuring things out.

Took some inspiration from your https://github.com/markhilton/docker-nginx-php-fpm-example repo, however I needed to setup MySQL and while debugging my connection errors, I noticed that PHP isn't updating edits I'm making until the service is physically restarted, which is tedious and defeats the purpose of this exercise.

My assumption is this issue exists solely in the php-fpm container I setup, since restarting the service refreshes the file system and my changes show up, but since I'm using volumes to map my working directory to the container I would expect my changes to show up instantly. I'm not sure if I have a fundamental misunderstanding with how this all works.

Doing some research on this behavior, opcache.revalidate_freq in the php.ini settings seemed like a promising resolution, but overwriting this value from the default 10 seconds to 1 in my .docker/local.ini filedoes not appear to resolve the issue.

# .docker/local.ini
[opcache]
opcache.revalidate_freq=1
# docker-compose.yml
version: '3'

services:
    mysql:
        container_name: mysql
        image: mysql:5.7
        restart: unless-stopped
        tty: true
        networks:
            - backend
        ports:
            - ${DB_PORT}:3306
        volumes:
            - data:/var/lib/mysql
            - ./.docker/my.cnf:/etc/mysql/my.cnf
        environment:
            MYSQL_DATABASE: ${DB_NAME}
            MYSQL_PASSORD: ${DB_PASS}
            MYSQL_USER: ${DB_USER}
            MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
            SERVICE_TAGS: dev
            SERVICE_NAME: mysql

    nginx:
        container_name: nginx
        image: crunchgeek/nginx-pagespeed:1.17.3-r1
        working_dir: /app
        hostname: nginx
        tty: true
        ports:
            - "80:80"
            - "8080:8080"
        networks:
            - backend
        volumes:
            - .:/app
            - ./.docker:/config
        env_file: .env

    php-fpm:
        image: crunchgeek/php-fpm:7.3
        working_dir: /app/public
        hostname: php-fpm
        tty: true
        depends_on:
            - mysql
        container_name: php-fpm
        networks:
            - backend
        volumes:
            - .:/app
            - ./.docker:/config
        env_file: .env

networks:
    backend:
        driver: bridge

volumes:
    data:
        driver: local

supervisor not working

I set environment SUPERVISORD_PATH to my supervisor config dir and start the container, but nothing happened.
Thanks for helping!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.