/docker-nginx-php-fpm

Docker Nginx + PHP-fpm image w/S6 overlay, logrotate

Primary LanguageDockerfileMIT LicenseMIT

github.com/tiredofit/docker-nginx-php-fpm

GitHub release Build Status Docker Stars Docker Pulls Become a sponsor Paypal Donate


About

This repository will build a Nginx w/PHP-FPM docker image, suitable for serving PHP scripts, or utilizing as a base image for installing additional software.

  • Tracking PHP 5.3-8.0
  • Easily enable / disable extensions based on your use case
  • Automatic Log rotation
  • Composer Included
  • XDebug capability
  • Caching via APC, opcache
  • Includes client libraries for MariaDB and Postgresql

Maintainer

Table of Contents

Prerequisites and Assumptions and Assumptions

  • Assumes you are using some sort of SSL terminating reverse proxy such as:

Installation

Build from Source

Clone this repository and build the image with docker build -t (imagename) .

Prebuilt Images

Builds of the image are available on Docker Hub and is the recommended method of installation.

docker pull tiredofit/nginx-php-fpm:(imagetag)

The following image tags are available along with their tagged release based on what's written in the Changelog:

PHP version Alpine Base Tag Debian Base Tag
latest edge :alpine-edge
8.0.x 3.13 :alpine-8.0 Buster :debian-8.0
7.4.x 3.13 :alpine-7.4 Buster :debian-7.3
7.3.x 3.12 :alpine-7.3 Buster :debian-7.3
7.2.x 3.9 :alpine-8.0
7.1.x 3.7 :alpine-7.4
7.0.x 3.5 :alpine-7.3
5.6.x 3.8 :alpine-5.6
5.5.x 3.4 :5.5-latest
5.3.x 3.4 :5.3-latest

Multi Archictecture

Images are built primarily for amd64 architecture, and may also include builds for arm/v6, arm/v7, arm64 and others. These variants are all unsupported. Consider sponsoring my work so that I can work with various hardware. To see if this image supports multiple architecures, type docker manifest (image):(tag)

Configuration

Quick Start

Persistent Storage

The container starts up and reads from /etc/nginx/nginx.conf for some basic configuration and to listen on port 73 internally for Nginx Status responses. /etc/nginx/conf.d contains a sample configuration file that can be used to customize a nginx server block.

The following directories are used for configuration and can be mapped for persistent storage.

Directory Description
/www/html Root Directory
/www/logs Nginx and php-fpm logfiles

Environment Variables

Base Images used

This image relies on an Alpine Linux or Debian Linux base image that relies on an init system for added capabilities. Outgoing SMTP capabilities are handlded via msmtp. Individual container performance monitoring is performed by zabbix-agent. Additional tools include: bash,curl,less,logrotate, nano,vim. Be sure to view the following repositories to understand all the customizable options:

Image Description
OS Base Customized Image based on Alpine Linux
Nginx Nginx webserver

Container Options

The container has an ability to work in 3 modes, nginx-php-fpm (default) is an All in One image with nginx and php-fpm working together, nginx will only utilize nginx however not the included php-fpm instance, allowing for connecting to multiple remote php-fpm backends, and finally php-fpm to operate PHP-FPM in standalone mode.

Parameter Description Default
CONTAINER_MODE Mode of running container nginx-php-fpm, nginx, php-fpm nginx-php-fpm

When CONTAINER_MODE set to nginx the PHP_FPM_LISTEN_PORT environment variable is ignored and the PHP_FPM_HOST variable defined below changes. You can add multiple PHP-FPM hosts to the backend in this syntax : seperated by commas e.g php-fpm-container1:9000,php-fpm-container2:9000

You can also pass arguments to each server as defined in the Nginx Upstream Documentation

Parameter Description Default
PHP_APC_SHM_SIZE APC Cache Memory size - 0 to disable 128M
PHP_FPM_HOST Default PHP-FPM Host 127.0.0.1 - See above Container options
PHP_FPM_LISTEN_PORT PHP-FPM Listening Port - Ignored with above container options 9000
PHP_FPM_MAX_CHILDREN Maximum Children 75
PHP_FPM_MAX_REQUESTS How many requests before spawning new server 500
PHP_FPM_MAX_SPARE_SERVERS Maximum Spare Servers available 3
PHP_FPM_MIN_SPARE_SERVERS Minium Spare Servers avaialble 1
PHP_FPM_PROCESS_MANAGER How to handle processes static, ondemand, dynamic dynamic
PHP_FPM_START_SERVERS How many FPM servers to start initially 2
PHP_LOG_ACCESS_FILE PHP Access Logfile Name access.log
PHP_LOG_ERROR_FILE Logfile name error.log
PHP_LOG_LEVEL PHP Log Level notice
PHP_LOG_ACCESS_FORMAT Log format - default or json default
PHP_LOG_LOCATION Log Location for PHP Logs /www/logs/php-fpm
PHP_MEMORY_LIMIT How much memory should PHP use 128M
PHP_OPCACHE_MEM_SIZE OPCache Memory Size - Set 0 to disable or via other env vars 128
PHP_POST_MAX_SIZE Maximum Input Size for POST 2G
PHP_TIMEOUT Maximum Script execution Time 180
PHP_UPLOAD_MAX_SIZE Maximum Input Size for Uploads 2G
PHP_WEBROOT Used with CONTAINER_MODE=php-fpm /www/html

Enabling / Disabling Specific Extensions

Enable extensions by using the PHP extension name ie redis as PHP_ENABLE_REDIS=TRUE. Core extensions are enabled by default are:

Parameter Default
PHP_ENABLE_APCU TRUE
PHP_ENABLE_BCMATH TRUE
PHP_ENABLE_BZ2 TRUE
PHP_ENABLE_CTYPE TRUE
PHP_ENABLE_CURL TRUE
PHP_ENABLE_DOM TRUE
PHP_ENABLE_EXIF TRUE
PHP_ENABLE_FILEINFO TRUE
PHP_ENABLE_GD TRUE
PHP_ENABLE_ICONV TRUE
PHP_ENABLE_IMAP TRUE
PHP_ENABLE_INTL TRUE
PHP_ENABLE_JSON TRUE
PHP_ENABLE_MBSTRING TRUE
PHP_ENABLE_MYSQLI TRUE
PHP_ENABLE_MYSQLND TRUE
PHP_ENABLE_OPCACHE TRUE
PHP_ENABLE_OPENSSL TRUE
PHP_ENABLE_PDO TRUE
PHP_ENABLE_PDO_MYSQL TRUE
PHP_ENABLE_PGSQL TRUE
PHP_ENABLE_PHAR TRUE
PHP_ENABLE_SESSION TRUE
PHP_ENABLE_SIMPLEXML TRUE
PHP_ENABLE_TOKENIZER TRUE
PHP_ENABLE_XML TRUE
PHP_ENABLE_XMLREADER TRUE
PHP_ENABLE_XMLWRITER TRUE

To enable all extensions in image use PHP_KITCHENSINK=TRUE. Head inside the image and see what extensions are available by typing php-ext list all

Debug Options

To enable XDebug set PHP_ENABLE_XDEBUG=TRUE. Visit the PHP XDebug Documentation to understand what these options mean.

Parameter Description
PHP_XDEBUG_PROFILER_DIR Where to store Profiler Logs
PHP_XDEBUG_PROFILER_ENABLE Enable Profiler
PHP_XDEBUG_PROFILER_ENABLE_TRIGGER Enable Profiler Trigger
PHP_XDEBUG_REMOTE_AUTOSTART Enable Autostarting as opposed to GET/POST
PHP_XDEBUG_REMOTE_CONNECT_BACK Enbable Connection Back
PHP_XDEBUG_REMOTE_ENABLE Enable Remote Debugging
PHP_XDEBUG_REMOTE_HANDLER XDebug Remote Handler
PHP_XDEBUG_REMOTE_HOST Set this to your IP Address
PHP_XDEBUG_REMOTE_PORT XDebug Remote Port

Networking

The following ports are exposed.

Port Description
9000 PHP-FPM

Maintenance

Inside the image are tools to perform modification on how the image runs.

Shell Access

For debugging and maintenance purposes you may want access the containers shell.

docker exec -it (whatever your container name is e.g. nginx-php-fpm) bash

PHP Extensions

If you want to enable or disable or list what PHP extensions are available, type php-ext help

Maintenance Mode

If you wish to turn the web server into maintenance mode showing a single page screen outlining that the service is being worked on, you can also enter into the container and type maintenance ARG, where ARG is either ON,OFF, or SLEEP (seconds) which will temporarily place the site in maintenance mode and then restore it back to normal after time has passed.

Contributions

Welcomed. Please fork the repository and submit a pull request for any bug fixes, features or additions you propose to be included in the image. If it does not impact my intended usage case, it will be merged into the tree, tagged as a release and credit to the contributor in the CHANGELOG.

Support

These images were built to serve a specific need in a production environment and gradually have had more functionality added based on requests from the community.

Usage

  • The Discussions board is a great place for working with the community on tips and tricks of using this image.
  • Consider sponsoring me personalized support.

Bugfixes

  • Please, submit a Bug Report if something isn't working as expected. I'll do my best to issue a fix in short order.

Feature Requests

  • Feel free to submit a feature request, however there is no guarantee that it will be added, or at what timeline.
  • Consider sponsoring me regarding development of features.

Updates

  • Best effort to track upstream changes, More priority if I am actively using the image in a production environment.
  • Consider sponsoring me for up to date releases.

License

MIT. See LICENSE for more details.

References