Files
nginx-php-fpm/README.md

241 lines
6.1 KiB
Markdown
Raw Permalink Normal View History

2024-12-27 18:59:07 +01:00
### 🐳 **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.
2023-01-04 18:04:25 +02:00
[![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)
2024-11-16 20:55:33 +01:00
[![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)
2022-07-01 18:21:34 +02:00
![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)
2024-12-27 18:59:07 +01:00
#### **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.
This image is ideal for developers looking for a streamlined, high-performance solution to deploy PHP applications with essential tools already integrated.
---
## **Links**
- [Docker Hub](https://hub.docker.com/r/jkaninda/nginx-php-fpm)
- [GitHub Repository](https://github.com/jkaninda/nginx-php-fpm)
## **Supported PHP Versions**
- 8.4
- 8.3
- 8.2
- 8.1
- 8.0
- 7.4
- 7.2
## **Specifications**
This Docker image comes pre-installed with the following:
- **PHP Extensions**:
- Composer
- OpenSSL
- XML
- PDO
- Rdkafka
- Redis
- Mbstring
- PCNTL
- ZIP
- GD
- BCMath
- Memcached
- **Additional Features**:
- Laravel Cron Jobs & Scheduler
- Supervisord
- Node.js & NPM
## **Basic Usage with Docker Compose**
### **Example `docker-compose.yml`**
```yaml
services:
app:
image: jkaninda/nginx-php-fpm:8.3
container_name: app
restart: unless-stopped
user: www-data # Optional for production
volumes:
# Project root
- ./src:/var/www/html
ports:
- "80:80"
networks:
- default
2022-07-01 18:21:34 +02:00
```
2024-12-27 18:59:07 +01:00
### **Commands**
#### Start the service:
2022-07-01 18:21:34 +02:00
```sh
2024-12-27 18:59:07 +01:00
docker compose up -d
```
2024-12-27 18:59:07 +01:00
#### Create a new Laravel project:
```sh
docker compose exec app composer create-project --prefer-dist laravel/laravel .
```
2024-12-27 18:59:07 +01:00
#### Generate application key:
```sh
docker compose exec app php artisan key:generate
```
2024-12-27 18:59:07 +01:00
#### Create a storage symlink:
```sh
docker compose exec app php artisan storage:link
```
2024-12-27 18:59:07 +01:00
#### Fix storage and cache permissions:
```sh
docker compose exec app chmod -R 777 storage bootstrap/cache
```
2024-12-27 18:59:07 +01:00
#### Run Laravel migrations:
```sh
2024-12-27 18:59:07 +01:00
docker compose exec app php artisan migrate
2022-07-01 18:21:34 +02:00
```
2024-12-27 18:59:07 +01:00
#### Access the container shell:
2022-07-01 18:21:34 +02:00
```sh
2022-08-14 14:43:00 +02:00
docker exec -it app bash
2022-07-01 18:21:34 +02:00
```
2024-12-27 18:59:07 +01:00
---
## **Advanced Nginx PHP-FPM Setup**
### **Extended `docker-compose.yml` Example**
```yaml
2022-07-01 18:21:34 +02:00
version: '3'
services:
2024-12-27 18:59:07 +01:00
app:
image: jkaninda/nginx-php-fpm
container_name: app
restart: unless-stopped
ports:
- "80:80"
volumes:
# Project root
- ./:/var/www/html
- ~/.ssh:/root/.ssh # Use private CVS if needed
# Optional custom PHP config
# - ./php.ini:/usr/local/etc/php/conf.d/php.ini
environment:
- APP_ENV=development # or production
- LARAVEL_PROCS_NUMBER=2 # Optional: Queue worker processes
# - CLIENT_MAX_BODY_SIZE=20M # Optional
# - DOMAIN=example.com # Optional
- DOCUMENT_ROOT=/var/www/html # Optional
2022-07-01 18:21:34 +02:00
```
2024-12-27 18:59:07 +01:00
### **Default Web Root**
2023-05-29 01:32:26 +02:00
```
/var/www/html
```
2024-12-27 18:59:07 +01:00
---
2022-07-01 18:21:34 +02:00
2024-12-27 18:59:07 +01:00
## **Custom Build Example**
2022-07-01 18:21:34 +02:00
2024-12-27 18:59:07 +01:00
### **Dockerfile**
2023-01-04 18:04:25 +02:00
```Dockerfile
2023-12-16 07:25:17 +01:00
FROM jkaninda/nginx-php-fpm:8.3
2024-12-27 18:59:07 +01:00
# Copy Laravel project files
2023-01-04 18:04:25 +02:00
COPY . /var/www/html
# Storage Volume
VOLUME /var/www/html/storage
WORKDIR /var/www/html
2023-12-16 07:25:17 +01:00
# Fix permissions
RUN chown -R www-data:www-data /var/www/html
USER www-data
2023-01-04 18:04:25 +02:00
```
2024-12-27 18:59:07 +01:00
---
## **Custom Nginx Configuration**
2023-01-04 18:04:25 +02:00
2024-12-27 18:59:07 +01:00
To enable custom Nginx configurations, use the following files:
- `/var/www/html/conf/nginx/nginx.conf`
- `/var/www/html/conf/nginx/nginx-site.conf`
2022-07-01 18:26:06 +02:00
2024-12-27 18:59:07 +01:00
---
2022-07-01 18:21:34 +02:00
2024-12-27 18:59:07 +01:00
## **Supervisord Integration**
2022-07-02 11:42:38 +02:00
2024-12-27 18:59:07 +01:00
Supervisord can be used to manage tasks or processes within the container.
2023-11-13 15:15:48 +01:00
2024-12-27 18:59:07 +01:00
### **Example Configuration for Kafka Consumer**
2023-11-13 15:14:43 +01:00
```conf
[program:kafkaconsume-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan kafka:consumer
autostart=true
autorestart=true
numprocs=1
user=www-data
redirect_stderr=true
stdout_logfile=/var/www/html/storage/logs/kafka.log
```
2024-12-27 18:59:07 +01:00
---
## **Fixing Storage Permissions**
If you encounter storage permission issues, run the following commands:
2023-12-16 07:25:17 +01:00
```sh
2024-12-27 18:59:07 +01:00
docker compose exec php-fpm /bin/bash
```
Then inside the container:
2023-12-16 07:25:17 +01:00
```sh
chown -R www-data:www-data /var/www/html/
2024-12-27 18:59:07 +01:00
chmod -R 775 /var/www/html/storage
2023-12-16 07:25:17 +01:00
```
2022-09-21 10:23:03 +02:00
2024-12-27 18:59:07 +01:00
---
2025-07-27 07:21:50 +02:00
### 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!
2022-09-21 10:17:47 +02:00