Compare commits

..

14 Commits

Author SHA1 Message Date
402874a099 update readme
Some checks failed
Test / integration (push) Has been cancelled
2025-07-27 07:21:50 +02:00
93ddde9b30 Merge pull request #40 from jkaninda/dev
Some checks failed
Build / docker (push) Has been cancelled
Test / integration (push) Has been cancelled
Create FUNDING.yml
2025-07-19 02:29:52 +02:00
f59bd88d2e Create FUNDING.yml 2025-07-19 02:29:30 +02:00
091ebc6b09 Optimization of entrypoint (#39)
Some checks failed
Test / integration (push) Has been cancelled
Build / docker (push) Has been cancelled
2025-05-11 09:39:25 +02:00
258580b107 Merge pull request #37 from jkaninda/develop
Some checks failed
Build / docker (push) Failing after 8s
Test / integration (push) Failing after 2s
Update core features
2024-12-27 19:00:04 +01:00
fa948d34c5 Update core features
Some checks failed
Test / integration (push) Failing after 3s
2024-12-27 18:59:07 +01:00
a6c5254e05 Merge pull request #36 from jkaninda/develop
Add php 8.4
2024-11-24 13:18:41 +01:00
745d85b999 Merge pull request #35 from jkaninda/develop
Update readme
2024-11-16 20:56:09 +01:00
a73dc861d3 Merge pull request #34 from jkaninda/develop
fix ci
2024-11-16 20:48:30 +01:00
2e77ee7a6d Merge pull request #33 from jkaninda/develop
add php 8.4 RC
2024-11-16 20:45:17 +01:00
566d21fb6d Merge pull request #32 from jkaninda/develop
Add LICENSE
2024-10-10 08:14:43 +02:00
193e19df92 Merge pull request #31 from jkaninda/develop
chore: upgrade Node version from 17 to 20
2024-09-18 20:54:08 +02:00
6c7be22d06 Merge pull request #30 from jkaninda/develop
Update PHP version
2024-06-08 08:37:14 +02:00
3895edac70 Merge pull request #29 from jkaninda/develop
chore: update php version
2024-03-05 07:37:12 +01:00
5 changed files with 262 additions and 203 deletions

1
.github/FUNDING.yml vendored Normal file
View File

@@ -0,0 +1 @@
ko_fi: jkaninda

View File

@@ -6,6 +6,13 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
# Build the Docker image
- name: Build Docker Image
run: |
docker buildx build -f src/docker/Dockerfile --build-arg phpVersion=8.4 -t ${{ vars.BUILDKIT_IMAGE }}:latest --load .
- name: Verify Docker images
run: |
docker images
- name: Create Laravel project - name: Create Laravel project
run: | run: |
composer create-project laravel/laravel laravel composer create-project laravel/laravel laravel

268
README.md
View File

@@ -1,164 +1,185 @@
### 🐳 **Docker Image: Nginx PHP-FPM**
A **ready-to-use container** designed for running PHP-based applications, including Laravel microservices. This Docker image combines **Nginx** and **PHP-FPM**, offering a robust foundation for your projects with built-in support for essential extensions and configurations.
[![Build](https://github.com/jkaninda/nginx-php-fpm/actions/workflows/build.yml/badge.svg)](https://github.com/jkaninda/nginx-php-fpm/actions/workflows/build.yml) [![Build](https://github.com/jkaninda/nginx-php-fpm/actions/workflows/build.yml/badge.svg)](https://github.com/jkaninda/nginx-php-fpm/actions/workflows/build.yml)
[![Tests](https://github.com/jkaninda/nginx-php-fpm/actions/workflows/tests.yml/badge.svg)](https://github.com/jkaninda/nginx-php-fpm/actions/workflows/tests.yml) [![Tests](https://github.com/jkaninda/nginx-php-fpm/actions/workflows/tests.yml/badge.svg)](https://github.com/jkaninda/nginx-php-fpm/actions/workflows/tests.yml)
![Docker Image Size (latest by date)](https://img.shields.io/docker/image-size/jkaninda/nginx-php-fpm?style=flat-square) ![Docker Image Size (latest by date)](https://img.shields.io/docker/image-size/jkaninda/nginx-php-fpm?style=flat-square)
![Docker Pulls](https://img.shields.io/docker/pulls/jkaninda/nginx-php-fpm?style=flat-square) ![Docker Pulls](https://img.shields.io/docker/pulls/jkaninda/nginx-php-fpm?style=flat-square)
# Nginx PHP-FPM Docker image
> 🐳 Full Docker image for Nginx PHP-FPM container created to run Laravel or any php based applications. #### **Features**
- **PHP Application Support**: Optimized to run Laravel or any PHP-based applications.
- **Integrated Extensions**:
- **Database**: MySQL and PostgreSQL.
- **Caching**: Redis and Memcached.
- **Messaging**: Kafka for event-driven architecture.
- **Task Scheduling**: Laravel Scheduler and Cron jobs support.
- **Custom Configuration**: Pre-configured with sensible defaults, allowing seamless customization.
- **Event Handling**: Support for advanced event-driven processes.
- **Optimized for Microservices**: Built with modern PHP microservices in mind.
- [Docker Hub](https://hub.docker.com/r/jkaninda/nginx-php-fpm) This image is ideal for developers looking for a streamlined, high-performance solution to deploy PHP applications with essential tools already integrated.
- [Github](https://github.com/jkaninda/nginx-php-fpm)
## PHP Versions: ---
- 8.4 ## **Links**
- 8.3 - [Docker Hub](https://hub.docker.com/r/jkaninda/nginx-php-fpm)
- 8.2 - [GitHub Repository](https://github.com/jkaninda/nginx-php-fpm)
- 8.1
- 8.0
- 7.4
- 7.2
## Specifications: ## **Supported PHP Versions**
- 8.4
- 8.3
- 8.2
- 8.1
- 8.0
- 7.4
- 7.2
* Composer ## **Specifications**
* OpenSSL PHP Extension This Docker image comes pre-installed with the following:
* XML PHP Extension - **PHP Extensions**:
* PDO PHP Extension - Composer
* Rdkafka PHP Extension - OpenSSL
* Redis PHP Extension - XML
* Mbstring PHP Extension - PDO
* PCNTL PHP Extension - Rdkafka
* ZIP PHP Extension - Redis
* GD PHP Extension - Mbstring
* BCMath PHP Extension - PCNTL
* Memcached - ZIP
* Laravel Cron Job - GD
* Laravel Schedule - BCMath
* Supervisord - Memcached
* Nodejs
* NPM
## Simple docker-compose usage: - **Additional Features**:
- Laravel Cron Jobs & Scheduler
- Supervisord
- Node.js & NPM
```yml ## **Basic Usage with Docker Compose**
### **Example `docker-compose.yml`**
```yaml
services: services:
app: app:
image: jkaninda/nginx-php-fpm:8.3 image: jkaninda/nginx-php-fpm:8.3
container_name: app container_name: app
restart: unless-stopped restart: unless-stopped
user: www-data # Optional - for production usage user: www-data # Optional for production
volumes: volumes:
#Project root # Project root
- ./src:/var/www/html - ./src:/var/www/html
ports: ports:
- "80:80" - "80:80"
networks: networks:
- default #if you're using networks between containers - default
```
``` ### **Commands**
## Docker:
### Run #### Start the service:
```sh ```sh
docker compose up -d docker compose up -d
``` ```
### Create Laravel project
#### Create a new Laravel project:
```sh ```sh
docker compose exec app composer create-project --prefer-dist laravel/laravel . docker compose exec app composer create-project --prefer-dist laravel/laravel .
``` ```
### Artisan generate key
#### Generate application key:
```sh ```sh
docker compose exec app php artisan key:generate docker compose exec app php artisan key:generate
``` ```
### Storage link
#### Create a storage symlink:
```sh ```sh
docker compose exec app php artisan storage:link docker compose exec app php artisan storage:link
``` ```
### Fix permissions
#### Fix storage and cache permissions:
```sh ```sh
docker compose exec app chmod -R 777 storage bootstrap/cache docker compose exec app chmod -R 777 storage bootstrap/cache
``` ```
### Laravel migration
#### Run Laravel migrations:
```sh ```sh
docker compose exec app php artisan migrate docker compose exec app php artisan migrate
``` ```
###
#### Access the container shell:
```sh ```sh
docker exec -it app bash docker exec -it app bash
``` ```
## Advanced Nignx-php-fpm: ---
### docker-compose.yml
```yml ## **Advanced Nginx PHP-FPM Setup**
### **Extended `docker-compose.yml` Example**
```yaml
version: '3' version: '3'
services: services:
app: app:
image: jkaninda/nginx-php-fpm image: jkaninda/nginx-php-fpm
container_name: app container_name: app
restart: unless-stopped restart: unless-stopped
ports: ports:
- "80:80" - "80:80"
volumes: volumes:
#Project root # Project root
- ./:/var/www/html - ./:/var/www/html
- ~/.ssh:/root/.ssh # If you use private CVS - ~/.ssh:/root/.ssh # Use private CVS if needed
#./php.ini:/usr/local/etc/php/conf.d/php.ini # Optional, your custom php init file # Optional custom PHP config
environment: # - ./php.ini:/usr/local/etc/php/conf.d/php.ini
- APP_ENV=development # Optional, or production environment:
- LARAVEL_PROCS_NUMBER=2 # Optional, Laravel queue:work process number - APP_ENV=development # or production
#- CLIENT_MAX_BODY_SIZE=20M # Optional - LARAVEL_PROCS_NUMBER=2 # Optional: Queue worker processes
#- DOMAIN=example.com # Optional # - CLIENT_MAX_BODY_SIZE=20M # Optional
- DOCUMENT_ROOT=/var/www/html #Optional # - DOMAIN=example.com # Optional
- DOCUMENT_ROOT=/var/www/html # Optional
``` ```
Default web root:
### **Default Web Root**
``` ```
/var/www/html /var/www/html
``` ```
---
## Docker run ## **Custom Build Example**
```sh
docker compose up -d
``` ### **Dockerfile**
## Build from base
Dockerfile
```Dockerfile ```Dockerfile
FROM jkaninda/nginx-php-fpm:8.3 FROM jkaninda/nginx-php-fpm:8.3
# Copy laravel project files # Copy Laravel project files
COPY . /var/www/html COPY . /var/www/html
# Storage Volume # Storage Volume
VOLUME /var/www/html/storage VOLUME /var/www/html/storage
WORKDIR /var/www/html WORKDIR /var/www/html
# Custom cache invalidation / optional
#ARG CACHEBUST=1
# composer install / Optional
#RUN composer install
# Fix permissions # Fix permissions
RUN chown -R www-data:www-data /var/www/html RUN chown -R www-data:www-data /var/www/html
USER www-data USER www-data
``` ```
---
## Nginx custom config: ## **Custom Nginx Configuration**
### Enable custom nginx config files
> /var/www/html/conf/nginx/nginx.conf
> /var/www/html/conf/nginx/nginx-site.conf To enable custom Nginx configurations, use the following files:
- `/var/www/html/conf/nginx/nginx.conf`
- `/var/www/html/conf/nginx/nginx-site.conf`
## Supervisord ---
### Add more supervisor process in
> /etc/supervisor/conf.d/
In case you want to execute and maintain a task or process with supervisor. ## **Supervisord Integration**
Find below an example with Apache Kafka, when you want to maintain a consumer process. Supervisord can be used to manage tasks or processes within the container.
### Example:
### **Example Configuration for Kafka Consumer**
```conf ```conf
[program:kafkaconsume-worker] [program:kafkaconsume-worker]
process_name=%(program_name)s_%(process_num)02d process_name=%(program_name)s_%(process_num)02d
@@ -171,14 +192,49 @@ redirect_stderr=true
stdout_logfile=/var/www/html/storage/logs/kafka.log stdout_logfile=/var/www/html/storage/logs/kafka.log
``` ```
### Storage permision issue ---
## **Fixing Storage Permissions**
If you encounter storage permission issues, run the following commands:
```sh ```sh
docker compose exec php-fpm /bin/bash docker compose exec php-fpm /bin/bash
```
```sh
chown -R www-data:www-data /var/www/html/
``` ```
> chmod -R 775 /var/www/html/storage Then inside the container:
```sh
chown -R www-data:www-data /var/www/html/
chmod -R 775 /var/www/html/storage
```
---
### Explore Another Project: Goma Gateway
Are you building a microservices architecture?
Do you need a powerful yet lightweight API Gateway or a high-performance reverse proxy to secure and manage your services effortlessly?
Check out my other project — **[Goma Gateway](https://github.com/jkaninda/goma-gateway)**.
**Goma Gateway** is a high-performance, declarative API Gateway built for modern microservices. It comes with a rich set of built-in middleware, including:
* Basic, JWT, OAuth2, LDAP, and ForwardAuth authentication
* Caching and rate limiting
* Bot detection
* Built-in load balancing
* Simple configuration with minimal overhead
* ...and more!
**Protocol support:** REST, GraphQL, gRPC, TCP, and UDP
**Security:** Automatic HTTPS via Lets Encrypt or use your own TLS certificates
Whether you're managing internal APIs or exposing public endpoints, **Goma Gateway** helps you do it efficiently, securely, and with minimal complexity.
---
## ⭐️ **Support the Project**
If this project helped you, do not skip on giving it a star. Thanks!
> P.S. please give a star if you like it :wink:

View File

@@ -1,104 +1,96 @@
#!/bin/sh #!/bin/sh
set -e
echo "" echo ""
echo "***********************************************************" echo "***********************************************************"
echo " Starting NGINX PHP-FPM Docker Container " echo " Starting NGINX PHP-FPM Docker Container "
echo "***********************************************************" echo "***********************************************************"
set -e # Logging functions
set -e info() { echo "[INFO] $*"; }
info() { warning() { echo "[WARNING] $*"; }
{ set +x; } 2> /dev/null fatal() { echo "[ERROR] $*" >&2; exit 1; }
echo '[INFO] ' "$@"
} ARTISAN_FILE="/var/www/html/artisan"
warning() { SUPERVISOR_CONF_DIR="/etc/supervisor/conf.d"
{ set +x; } 2> /dev/null NGINX_CONF_DIR="/etc/nginx/conf.d"
echo '[WARNING] ' "$@" CUSTOM_NGINX_CONF="/var/www/html/conf/nginx"
} CUSTOM_SUPERVISOR_CONF="/var/www/html/conf/worker/supervisor.conf"
fatal() { DOCUMENT_ROOT="/var/www/html/public"
{ set +x; } 2> /dev/null
echo '[ERROR] ' "$@" >&2 # Laravel Supervisor Setup
exit 1 setup_laravel_supervisor() {
} info "Artisan file found, creating Laravel supervisor config"
## Check if the artisan file exists export DOCUMENT_ROOT
if [ -f /var/www/html/artisan ]; then
info "Artisan file found, creating laravel supervisor config" cat > "$SUPERVISOR_CONF_DIR/laravel-worker.conf" <<EOF
# Set DocumentRoot to the Laravel project directory [program:Laravel-scheduler]
export DOCUMENT_ROOT=/var/www/html/public process_name=%(program_name)s_%(process_num)02d
##Create Laravel Scheduler process command=/bin/sh -c "while true; do (php /var/www/html/artisan schedule:run --verbose --no-interaction &); sleep 60; done"
TASK=/etc/supervisor/conf.d/laravel-worker.conf autostart=true
touch $TASK autorestart=true
cat > "$TASK" <<EOF numprocs=1
[program:Laravel-scheduler] user=$USER_NAME
process_name=%(program_name)s_%(process_num)02d stdout_logfile=/var/log/laravel_scheduler.out.log
command=/bin/sh -c "while [ true ]; do (php /var/www/html/artisan schedule:run --verbose --no-interaction &); sleep 60; done" redirect_stderr=true
autostart=true
autorestart=true [program:Laravel-worker]
numprocs=1 process_name=%(program_name)s_%(process_num)02d
user=$USER_NAME command=php /var/www/html/artisan queue:work --sleep=3 --tries=3
stdout_logfile=/var/log/laravel_scheduler.out.log autostart=true
redirect_stderr=true autorestart=true
numprocs=$LARAVEL_PROCS_NUMBER
[program:Laravel-worker] user=$USER_NAME
process_name=%(program_name)s_%(process_num)02d redirect_stderr=true
command=php /var/www/html/artisan queue:work --sleep=3 --tries=3 stdout_logfile=/var/log/laravel_worker.log
autostart=true
autorestart=true
numprocs=$LARAVEL_PROCS_NUMBER
user=$USER_NAME
redirect_stderr=true
stdout_logfile=/var/log/laravel_worker.log
EOF EOF
info "Laravel supervisor config created"
else
info "artisan file not found"
fi
# Enable custom nginx config files if they exist info "Laravel supervisor config created"
if [ -f /var/www/html/conf/nginx/nginx.conf ]; then }
cp /var/www/html/conf/nginx/nginx.conf /etc/nginx/nginx.conf
info "Using custom nginx.conf"
fi
if [ -f /var/www/html/conf/nginx/nginx-site.conf ]; then # Nginx Config Setup
info "Custom nginx site config found" setup_nginx_config() {
rm /etc/nginx/conf.d/default.conf if [ -f "$CUSTOM_NGINX_CONF/nginx.conf" ]; then
cp /var/www/html/conf/nginx/nginx-site.conf /etc/nginx/conf.d/default.conf cp "$CUSTOM_NGINX_CONF/nginx.conf" /etc/nginx/nginx.conf
info "Start nginx with custom server config..." info "Using custom nginx.conf"
fi
if [ -f "$CUSTOM_NGINX_CONF/nginx-site.conf" ]; then
info "Custom nginx site config found"
rm -f "$NGINX_CONF_DIR/default.conf"
cp "$CUSTOM_NGINX_CONF/nginx-site.conf" "$NGINX_CONF_DIR/default.conf"
info "Start nginx with custom server config..."
else else
info "nginx-site.conf not found" info "nginx-site.conf not found"
info "If you want to use custom configs, create config file in /var/www/html/conf/nginx/nginx-site.conf" info "If you want to use custom configs, create it at $CUSTOM_NGINX_CONF/nginx-site.conf"
info "Start nginx with default config..." info "Start nginx with default config..."
rm -f /etc/nginx/conf.d/default.conf
TASK=/etc/nginx/conf.d/default.conf cat > "$NGINX_CONF_DIR/default.conf" <<EOF
touch $TASK server {
cat > "$TASK" <<EOF
server {
listen 80 default_server; listen 80 default_server;
listen [::]:80 default_server; listen [::]:80 default_server;
server_name $DOMAIN; server_name $DOMAIN;
# Add index.php to setup Nginx, PHP & PHP-FPM config root $DOCUMENT_ROOT;
index index.php index.html index.htm index.nginx-debian.html; index index.php index.html index.htm;
error_log /var/log/nginx/error.log; error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log; access_log /var/log/nginx/access.log;
root $DOCUMENT_ROOT;
# pass PHP scripts on Nginx to FastCGI (PHP-FPM) server location ~ \.php\$ {
location ~ \.php$ {
try_files \$uri =404; try_files \$uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_split_path_info ^(.+\.php)(/.+)\$;
# Nginx php-fpm config:
fastcgi_pass 127.0.0.1:9000; fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php; fastcgi_index index.php;
include fastcgi_params; include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_param PATH_INFO \$fastcgi_path_info; fastcgi_param PATH_INFO \$fastcgi_path_info;
} }
client_max_body_size $CLIENT_MAX_BODY_SIZE; client_max_body_size $CLIENT_MAX_BODY_SIZE;
server_tokens off; server_tokens off;
# Hide PHP headers fastcgi_hide_header X-Powered-By;
fastcgi_hide_header X-Powered-By;
fastcgi_hide_header X-CF-Powered-By; fastcgi_hide_header X-CF-Powered-By;
fastcgi_hide_header X-Runtime; fastcgi_hide_header X-Runtime;
@@ -106,27 +98,30 @@ if [ -f /var/www/html/conf/nginx/nginx-site.conf ]; then
try_files \$uri \$uri/ /index.php\$is_args\$args; try_files \$uri \$uri/ /index.php\$is_args\$args;
gzip_static on; gzip_static on;
} }
location ~ \.css {
add_header Content-Type text/css; location ~ \.css\$ { add_header Content-Type text/css; }
} location ~ \.js\$ { add_header Content-Type application/x-javascript; }
location ~ \.js {
add_header Content-Type application/x-javascript; location ~ /\.ht { deny all; }
} location ~ /\.(svn|git|hg|bzr)/ { deny all; }
# deny access to Apache .htaccess on Nginx with PHP,
# if Apache and Nginx document roots concur
location ~ /\.ht {deny all;}
location ~ /\.svn/ {deny all;}
location ~ /\.git/ {deny all;}
location ~ /\.hg/ {deny all;}
location ~ /\.bzr/ {deny all;}
} }
EOF EOF
fi fi
## Check if the supervisor config file exists }
if [ -f /var/www/html/conf/worker/supervisor.conf ]; then
info "Custom supervisor config found"
cp /var/www/html/conf/worker/supervisor.conf /etc/supervisor/conf.d/supervisor.conf
fi
## Start Supervisord
supervisord -c /etc/supervisor/supervisord.conf
# Load custom supervisor config if exists
load_custom_supervisor_config() {
if [ -f "$CUSTOM_SUPERVISOR_CONF" ]; then
info "Custom supervisor config found"
cp "$CUSTOM_SUPERVISOR_CONF" "$SUPERVISOR_CONF_DIR/supervisor.conf"
fi
}
# Main Execution
[ -f "$ARTISAN_FILE" ] && setup_laravel_supervisor || info "artisan file not found"
setup_nginx_config
load_custom_supervisor_config
# Start Supervisor
supervisord -c /etc/supervisor/supervisord.conf

0
version Normal file
View File