Skip to content

[pull] master from TrafeX:master #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 67 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
1bf1f81
Introduce multi-arch builds with Docker buildx
TrafeX Oct 7, 2021
61f3188
Merge pull request #75 from TrafeX/multi-arch-builds
TrafeX Oct 7, 2021
68d8df8
Improving multi-platform support
TrafeX Oct 7, 2021
a41ad31
Automatically update the description on Docker Hub
TrafeX Oct 7, 2021
1c140e8
Update nginx version in badge
TrafeX Oct 7, 2021
6a440c8
Fix incorrect repository
TrafeX Oct 7, 2021
df1a00c
Replace old PHP7 example with PHP8
TrafeX Oct 7, 2021
5cd4283
unix socket instead of tcp sockets between nginx and php-fpm
vildand Dec 16, 2021
3594f51
Merge pull request #80 from vildand/unix_sockets_instead_of_tcp_sockets
TrafeX Dec 29, 2021
302cf16
Only login to Docker Hub when pushing on master branch
TrafeX Dec 29, 2021
77f9f02
Trigger Docker Hub login on tags as well
TrafeX Dec 30, 2021
2f2b574
Disable absolute_redirect
jlxip Jan 14, 2022
238f3db
Merge pull request #82 from jlxip/master
TrafeX Jan 18, 2022
8bf0386
updated Dockerfile
tiagodevweb Dec 18, 2021
8405c55
removed --update flag
tiagodevweb Jan 18, 2022
b3ee5d8
Reverted the switch to a Go version of supervisord; it doesn't suppor…
TrafeX Jan 29, 2022
f4db6ce
Dropped installing the json extension, is included in PHP's core.
TrafeX Mar 20, 2022
e9596ce
Add reference to my services
TrafeX Mar 20, 2022
b8e362c
Update Alpine Linux to 3.15
TrafeX Mar 21, 2022
cc99d82
Fix spell error
TrafeX Mar 24, 2022
27cf98b
Upgrade Alpine Linux to 3.16, PHP to 8.1 and Nginx to 1.22
TrafeX May 29, 2022
76a9204
Enable Docker buildkit by default
TrafeX May 29, 2022
866e9a3
Optimize performance
kroese Jun 5, 2022
dbfab1e
Update location of PHP.ini files
johncoles Aug 10, 2022
0be32d3
Remove the zlib PHP extension, it's included in PHP core
TrafeX Sep 6, 2022
15dba68
Hide server info for security hardening
jimsihk Aug 15, 2022
bf2732f
Chopped up nginx.conf, extracted the server block to /conf.d/default.…
doedje Nov 29, 2022
31e09d9
Describe a few things better with comments
TrafeX Jan 7, 2023
ef39565
Upgrade Alpine Linux to 3.17
TrafeX Jan 7, 2023
d6e6462
Only upload Trivy reports to GitHub on master branch to avoid duplicates
TrafeX Jan 7, 2023
8944c19
Upgrade GitHub workflow packages and configure dependabot
TrafeX Jan 7, 2023
47c413f
Bump peter-evans/dockerhub-description from 2 to 3
dependabot[bot] Jan 7, 2023
9d8c091
Bump github/codeql-action from 1 to 2
dependabot[bot] Jan 7, 2023
c397b92
Run workflow weekly to keep the image up to date
TrafeX Jan 7, 2023
d01c49f
Update image when triggered via schedules pipeline
TrafeX Jan 15, 2023
02d326e
Update supported versions
TrafeX Jan 15, 2023
f4d87ab
Update image when triggered via schedules pipeline
TrafeX Jan 15, 2023
37460bf
Bump actions/checkout from 2 to 3
dependabot[bot] Feb 24, 2023
8780e1b
Upgrade to Alpine Linux 3.18 and Nginx 1.24
TrafeX Jun 4, 2023
397b58a
Upgrade PHP to 8.2
TrafeX Aug 18, 2023
bb5751a
create symlinkg for php
C0mp4ct Aug 21, 2023
7fb96f7
Add PHP_INI_DIR to make it easier for future PHP updates
TrafeX Aug 27, 2023
9cda0d0
Add documentation and examples
TrafeX Aug 27, 2023
58f3e7e
Describe how to add e-mail support
TrafeX Aug 27, 2023
2de0c0d
Bump docker/setup-buildx-action from 2 to 3
dependabot[bot] Sep 12, 2023
428bd70
Bump actions/checkout from 3 to 4
dependabot[bot] Sep 4, 2023
5ae5c75
Bump docker/setup-qemu-action from 2 to 3
dependabot[bot] Sep 12, 2023
0005024
Bump docker/login-action from 2 to 3
dependabot[bot] Sep 12, 2023
d0692cb
Update Dockerfile with Laravel PHP extensions needed
insign Sep 14, 2023
232971a
Changed docs urls to absolute for Docker Hub
TrafeX Sep 17, 2023
4073339
added instructions to enable https/ssl
insign Nov 14, 2023
f9d28d1
Bump github/codeql-action from 2 to 3
dependabot[bot] Dec 13, 2023
b829e43
Remove duplicatie SCRIPT_NAME, already defined in fastcgi_params
TrafeX Jan 1, 2024
1545dc1
Upgrade to Alpine Linux 3.19 and PHP 8.3
TrafeX Jan 15, 2024
1784a0a
Add `|| exit 1` to `HEALTHCHECK` to map cURL exit codes and Docker po…
JoshuaLicense Jan 15, 2024
cf05bd7
Bump peter-evans/dockerhub-description from 3 to 4
dependabot[bot] Jan 25, 2024
93b0234
Add mimetypes to be gzipped based on CloudFlare
TrafeX Feb 23, 2024
519bec5
Explain versioning
TrafeX May 10, 2024
d764e5c
Upgrade Alpine Linux to 3.20 and Nginx to 1.26
TrafeX Jul 14, 2024
b6d2907
Update Nginx version description
TrafeX Jul 14, 2024
ff7ef50
Renamed docker-compose to docker compose
TrafeX Aug 13, 2024
e5c5ec7
Fix chown command to use colon for user and group separation
Everhard Aug 24, 2024
43aa4be
Get weekly updates from Dependabot
TrafeX Dec 4, 2024
7be04d9
Update to PHP 8.4
MarkCenturion Dec 19, 2024
a17d18a
Remove deprecated version and upgrade Alpine to 3.21
TrafeX Jan 4, 2025
d1772a6
Remove the extra q query param with the request URL
TrafeX Jan 4, 2025
dc6f38e
build: create an alias for `php`
f15u Feb 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "docker"
directory: "/"
schedule:
interval: "weekly"
groups:
docker-updates:
update-types:
- "patch"
- "minor"
84 changes: 84 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Test & build Docker image

on:
push:
branches: [master]
tags: ["*"]
pull_request:
schedule:
- cron: "0 2 * * 6"

env:
IMAGE_NAME: trafex/php-nginx
IMAGE_TAG: ${{ github.sha }}
DOCKER_BUILDKIT: 1

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3

- name: Build image
run: |-
docker build -t $IMAGE_NAME:$IMAGE_TAG .

- name: Smoke test image
run: |-
docker compose -f docker-compose.test.yml up -d app
sleep 2
docker compose -f docker-compose.test.yml run sut

- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: "${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}"
format: "template"
template: "@/contrib/sarif.tpl"
output: "trivy-results.sarif"

- name: Upload Trivy scan results to GitHub Security tab
if: github.ref == 'refs/heads/master' && (github.event_name == 'push' || github.event_name == 'schedule')
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: "trivy-results.sarif"

- name: Login to Docker Hub
if: (github.ref == 'refs/heads/master' && (github.event_name == 'push' || github.event_name == 'schedule' )) || contains(github.ref, 'refs/tags/')
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build multi-arch image and push latest tag
if: github.ref == 'refs/heads/master' && (github.event_name == 'push' || github.event_name == 'schedule')
run: |-
docker buildx build \
--cache-from=$IMAGE_NAME:latest \
--push \
-t $IMAGE_NAME:latest \
--platform linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6 \
.

- name: Set tag in environment
if: contains(github.ref, 'refs/tags/')
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

- name: Build multi-arch image and push release tag
if: contains(github.ref, 'refs/tags/')
run: |-
docker buildx build \
--cache-from=$IMAGE_NAME:latest \
--push \
-t $IMAGE_NAME:$RELEASE_VERSION \
--platform linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6 \
.
63 changes: 0 additions & 63 deletions .github/workflows/deploy.yaml

This file was deleted.

21 changes: 21 additions & 0 deletions .github/workflows/dockerhub-description.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Update Docker Hub Description
on:
push:
branches:
- master
paths:
- README.md
- .github/workflows/dockerhub-description.yml
jobs:
dockerHubDescription:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Docker Hub Description
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
repository: trafex/php-nginx
short-description: ${{ github.event.repository.description }}
67 changes: 33 additions & 34 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,57 +1,56 @@
FROM alpine:3.14
ARG ALPINE_VERSION=3.21
FROM alpine:${ALPINE_VERSION}
LABEL Maintainer="Tim de Pater <[email protected]>"
LABEL Description="Lightweight container with Nginx 1.20 & PHP 8.0 based on Alpine Linux."
LABEL Description="Lightweight container with Nginx 1.26 & PHP 8.4 based on Alpine Linux."
# Setup document root
WORKDIR /var/www/html

# Install packages and remove default server definition
RUN apk --no-cache add \
RUN apk add --no-cache \
curl \
nginx \
php8 \
php8-ctype \
php8-curl \
php8-dom \
php8-fpm \
php8-gd \
php8-intl \
php8-json \
php8-mbstring \
php8-mysqli \
php8-opcache \
php8-openssl \
php8-phar \
php8-session \
php8-xml \
php8-xmlreader \
php8-zlib \
php84 \
php84-ctype \
php84-curl \
php84-dom \
php84-fileinfo \
php84-fpm \
php84-gd \
php84-intl \
php84-mbstring \
php84-mysqli \
php84-opcache \
php84-openssl \
php84-phar \
php84-session \
php84-tokenizer \
php84-xml \
php84-xmlreader \
php84-xmlwriter \
supervisor

# Create symlink so programs depending on `php` still function
RUN ln -s /usr/bin/php8 /usr/bin/php
RUN ln -s /usr/bin/php84 /usr/bin/php

# Configure nginx
# Configure nginx - http
COPY config/nginx.conf /etc/nginx/nginx.conf
# Configure nginx - default server
COPY config/conf.d /etc/nginx/conf.d/

# Configure PHP-FPM
COPY config/fpm-pool.conf /etc/php8/php-fpm.d/www.conf
COPY config/php.ini /etc/php8/conf.d/custom.ini
ENV PHP_INI_DIR /etc/php84
COPY config/fpm-pool.conf ${PHP_INI_DIR}/php-fpm.d/www.conf
COPY config/php.ini ${PHP_INI_DIR}/conf.d/custom.ini

# Configure supervisord
COPY config/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Setup document root
RUN mkdir -p /var/www/html

# Make sure files/folders needed by the processes are accessable when they run under the nobody user
RUN chown -R nobody.nobody /var/www/html && \
chown -R nobody.nobody /run && \
chown -R nobody.nobody /var/lib/nginx && \
chown -R nobody.nobody /var/log/nginx
RUN chown -R nobody:nobody /var/www/html /run /var/lib/nginx /var/log/nginx

# Switch to use a non-root user from here on
USER nobody

# Add application
WORKDIR /var/www/html
COPY --chown=nobody src/ /var/www/html/

# Expose the port nginx is reachable on
Expand All @@ -61,4 +60,4 @@ EXPOSE 8080
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

# Configure a healthcheck to validate that everything is up&running
HEALTHCHECK --timeout=10s CMD curl --silent --fail http://127.0.0.1:8080/fpm-ping
HEALTHCHECK --timeout=10s CMD curl --silent --fail http://127.0.0.1:8080/fpm-ping || exit 1
69 changes: 21 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
# Docker PHP-FPM 8.0 & Nginx 1.20 on Alpine Linux
Example PHP-FPM 8.0 & Nginx 1.20 container image for Docker, build on [Alpine Linux](https://www.alpinelinux.org/).
# Docker PHP-FPM 8.4 & Nginx 1.26 on Alpine Linux
Example PHP-FPM 8.4 & Nginx 1.26 container image for Docker, built on [Alpine Linux](https://www.alpinelinux.org/).

Repository: https://github.com/TrafeX/docker-php-nginx


* Built on the lightweight and secure Alpine Linux distribution
* Multi-platform, supporting AMD4, ARMv6, ARMv7, ARM64
* Very small Docker image size (+/-40MB)
* Uses PHP 8.0 for better performance, lower CPU usage & memory footprint
* Uses PHP 8.4 for the best performance, low CPU usage & memory footprint
* Optimized for 100 concurrent users
* Optimized to only use resources when there's traffic (by using PHP-FPM's `on-demand` process manager)
* The services Nginx, PHP-FPM and supervisord run under a non-privileged user (nobody) to make it more secure
* The logs of all the services are redirected to the output of the Docker container (visible with `docker logs -f <container name>`)
* Follows the KISS principle (Keep It Simple, Stupid) to make it easy to understand and adjust the image to your needs

[![Docker Pulls](https://img.shields.io/docker/pulls/trafex/php-nginx.svg)](https://hub.docker.com/r/trafex/php-nginx/)
![nginx 1.18.0](https://img.shields.io/badge/nginx-1.20-brightgreen.svg)
![php 8.0](https://img.shields.io/badge/php-8.0-brightgreen.svg)
![nginx 1.26](https://img.shields.io/badge/nginx-1.26-brightgreen.svg)
![php 8.4](https://img.shields.io/badge/php-8.4-brightgreen.svg)
![License MIT](https://img.shields.io/badge/license-MIT-blue.svg)

## [![Trafex Consultancy](https://timdepater.com/logo/mini-logo.png)](https://timdepater.com?mtm_campaign=github)
I can help you with [Containerization, Kubernetes, Monitoring, Infrastructure as Code and other DevOps challenges](https://timdepater.com/?mtm_campaign=github).


## Goal of this project
The goal of this container image is to provide an example for running Nginx and PHP-FPM in a container which follows
the best practices and is easy to understand and modify to your needs.
Expand All @@ -34,11 +39,9 @@ Or mount your own code to be served by PHP-FPM & Nginx

docker run -p 80:8080 -v ~/my-codebase:/var/www/html trafex/php-nginx

### Docker Hub repository name change
Since we switched to PHP8 the repository name [trafex/alpine-nginx-php7](https://hub.docker.com/r/trafex/alpine-nginx-php7) didn't make sense anymore.
Because you can't change the name of the repository on Docker Hub I created a new one.

From now on this image can be pulled from Docker Hub under the name [trafex/php-nginx](https://hub.docker.com/r/trafex/php-nginx).
## Versioning
Major or minor changes are always published as a [release](https://github.com/TrafeX/docker-php-nginx/releases) with correspondending changelogs.
The `latest` tag is automatically updated weekly to include the latests patches from Alpine Linux.

## Configuration
In [config/](config/) you'll find the default configuration files for Nginx, PHP and PHP-FPM.
Expand All @@ -50,48 +53,18 @@ Nginx configuration:

PHP configuration:

docker run -v "`pwd`/php-setting.ini:/etc/php7/conf.d/settings.ini" trafex/php-nginx
docker run -v "`pwd`/php-setting.ini:/etc/php84/conf.d/settings.ini" trafex/php-nginx

PHP-FPM configuration:

docker run -v "`pwd`/php-fpm-settings.conf:/etc/php7/php-fpm.d/server.conf" trafex/php-nginx
docker run -v "`pwd`/php-fpm-settings.conf:/etc/php84/php-fpm.d/server.conf" trafex/php-nginx

_Note; Because `-v` requires an absolute path I've added `pwd` in the example to return the absolute path to the current directory_

## Documentation and examples
To modify this container to your specific needs please see the following examples;

## Adding composer

If you need [Composer](https://getcomposer.org/) in your project, here's an easy way to add it.

```Dockerfile
FROM trafex/php-nginx:latest

# Install composer from the official image
COPY --from=composer /usr/bin/composer /usr/bin/composer

# Run composer install to install the dependencies
RUN composer install --optimize-autoloader --no-interaction --no-progress
```

### Building with composer

If you are building an image with source code in it and dependencies managed by composer then the definition can be improved.
The dependencies should be retrieved by the composer but the composer itself (`/usr/bin/composer`) is not necessary to be included in the image.

```Dockerfile
FROM composer AS composer

# copying the source directory and install the dependencies with composer
COPY <your_directory>/ /app

# run composer install to install the dependencies
RUN composer install \
--optimize-autoloader \
--no-interaction \
--no-progress

# continue stage build with the desired image and copy the source including the
# dependencies downloaded by composer
FROM trafex/php-nginx
COPY --chown=nginx --from=composer /app /var/www/html
```
* [Adding xdebug support](https://github.com/TrafeX/docker-php-nginx/blob/master/docs/xdebug-support.md)
* [Adding composer](https://github.com/TrafeX/docker-php-nginx/blob/master/docs/composer-support.md)
* [Getting the real IP of the client behind a load balancer](https://github.com/TrafeX/docker-php-nginx/blob/master/docs/real-ip-behind-loadbalancer.md)
* [Sending e-mails](https://github.com/TrafeX/docker-php-nginx/blob/master/docs/sending-emails.md)
3 changes: 2 additions & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Only the latest version will be supported and receive security updates.

| Version | Supported |
| ------- | ------------------ |
| 2.x.x | :white_check_mark: |
| 3.x.x | :white_check_mark: |
| 2.x.x | :x: |
| 1.x.x | :x: |

## Reporting a Vulnerability
Expand Down
Loading