first commit

This commit is contained in:
2022-07-01 18:21:34 +02:00
commit 0f140da34e
8 changed files with 425 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/.history
laravel
docker-compose.yml

97
Dockerfile Normal file
View File

@@ -0,0 +1,97 @@
FROM php:8.1-fpm
ENV WORKDIR=/var/www/html
ENV STORAGE_DIR=${WORKDIR}/storage
ENV DOCUMENT_ROOT=${WORKDIR}
ENV LARAVEL_PROCS_NUMBER=2
ENV DOMAIN=_
ENV CLIENT_MAX_BODY_SIZE=15M
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmemcached-dev \
libzip-dev \
libpng-dev \
libonig-dev \
libxml2-dev \
librdkafka-dev \
libpq-dev \
openssh-server \
zip \
unzip \
supervisor \
sqlite3 \
nano \
cron
# Install nginx
RUN apt-get update && apt-get install -y nginx
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install Kafka
RUN git clone https://github.com/arnaud-lb/php-rdkafka.git\
&& cd php-rdkafka \
&& phpize \
&& ./configure \
&& make all -j 5 \
&& make install
# Install Rdkafka and enable it
RUN docker-php-ext-enable rdkafka \
&& cd .. \
&& rm -rf /php-rdkafka
# Install PHP extensions zip, mbstring, exif, bcmath, intl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install zip mbstring exif pcntl bcmath -j$(nproc) gd intl
# Install Redis and enable it
RUN pecl install redis && docker-php-ext-enable redis
# Install the php memcached extension
RUN pecl install memcached && docker-php-ext-enable memcached
# Install the PHP pdo_mysql extention
RUN docker-php-ext-install pdo_mysql
# Install the PHP pdo_pgsql extention
RUN docker-php-ext-install pdo_pgsql
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Install Laravel Envoy
RUN composer global require "laravel/envoy=~1.0"
# Set working directory
WORKDIR $WORKDIR
# nginx site conf
RUN rm -Rf /var/www/* && \
mkdir /var/www/html/
ADD src/index.php $WORKDIR/index.php
ADD src/conf/nginx/default.conf /etc/nginx/sites-available/default
ADD src/php.ini $PHP_INI_DIR/conf.d/
COPY ./entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh
RUN ln -s /usr/local/bin/entrypoint.sh /
ENTRYPOINT ["entrypoint.sh"]
RUN usermod -u 1000 www-data
RUN groupmod -g 1000 www-data
RUN chmod -R 755 $WORKDIR
RUN chown -R www-data:www-data $WORKDIR
EXPOSE 9000
CMD [ "entrypoint" ]

105
README.md Normal file
View File

@@ -0,0 +1,105 @@
![Docker Cloud Build Status](https://img.shields.io/docker/cloud/build/jkaninda/nginx-php-fpm?style=flat-square)
![Docker Cloud Automated build](https://img.shields.io/docker/cloud/automated/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)
# Nginx PHP-FPM Docker image
> 🐳 Docker image for Nginx PHP-FPM container crafted to run Laravel or any php based applications.
- [Docker Hub](https://hub.docker.com/r/jkaninda/nginx-php-fpm)
## Specifications:
* PHP 8.1 / 8.0 / 7.4 / 7.2
* Composer
* OpenSSL PHP Extension
* XML PHP Extension
* PDO PHP Extension
* Rdkafka PHP Extension
* Redis PHP Extension
* Mbstring PHP Extension
* PCNTL PHP Extension
* ZIP PHP Extension
* GD PHP Extension
* BCMath PHP Extension
* Memcached
* Laravel Cron Job
* Laravel Schedule
* Laravel Envoy
* Supervisord
## Simple docker-compose usage:
```yml
version: '3'
services:
php-fpm:
image: jkaninda/nginx-php-fpm:latest
container_name: php-fpm
restart: unless-stopped
volumes:
#Project root
- ./:/var/www/html
ports:
- "80:80"
networks:
- default #if you're using networks between containers
```
## Laravel `artisan` command usage:
### Open php-fpm
```sh
docker-compose exec php-fpm /bin/bash
```
### Laravel migration
```sh
php atisan migrate
```
## Advanced Nignx-php-fpm:
### docker-compose.yml
```yml
version: '3'
services:
php-fpm:
image: jkaninda/nginx-php-fpm
container_name: php-fpm
working_dir: /var/www/html #Optional, If you want to use a custom directory
restart: unless-stopped
ports:
- "80:80"
volumes:
#Project root
- ./:/var/www/html
- ~/.ssh:/root/.ssh # If you use private CVS
- ./supervisord:/etc/supervisor/conf.d/ # Supervisor directory, if you want to add more supervisor process config file
- ./php.ini:/usr/local/etc/php/conf.d/php.ini # Optional, your custom php init file
- storage-data:/var/www/html/storage/app #Optional, your custom storage data
environment:
- APP_ENV=development # Optional, or production
- WORKDIR=/var/www/html #Optional, If you want to use a custom directory
- LARAVEL_PROCS_NUMBER=3 # Optional, Laravel queue:work process number
- CLIENT_MAX_BODY_SIZE=20M
- DOMAIN=example.com
volumes:
storage-data:
```
## Docker run
```sh
docker-compose up -d
```
## Nginx custom config:
### Enable custom nginx config files if they exist
> /var/www/html/conf/nginx/nginx.conf
> /var/www/html/conf/nginx/nginx-site.conf
> P.S. please give a star if you like it :wink:

9
build.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/usr/bin/env bash
if [ $# -eq 0 ]
then
tag='latest'
else
tag=$1
fi
docker build -t jkaninda/nginx-php-fpm:$tag .

160
entrypoint.sh Normal file
View File

@@ -0,0 +1,160 @@
#!/bin/sh
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
echo ""
echo "***********************************************************"
echo " Starting LARAVEL NGINX PHP-FPM Docker Container "
echo "***********************************************************"
set -e
## Create PHP-FPM worker process
TASK=/etc/supervisor/conf.d/php-fpm.conf
touch $TASK
cat > "$TASK" <<EOF
[supervisord]
nodaemon=true
user=root
[program:php-fpm]
command=/usr/local/sbin/php-fpm
numprocs=1
autostart=true
autorestart=true
stderr_logfile=/var/log/php-fpm_consumer.err.log
stdout_logfile=/var/log/php-fpm_consumer.out.log
user=root
priority=100
EOF
## Check if the artisan file exists
if [ -f $WORKDIR/artisan ]; then
echo "${Green} artisan file found, creating laravel supervisor config"
# Set DocumentRoot to the Laravel project directory
export DOCUMENT_ROOT=$WORKDIR/public
##Create Laravel Scheduler process
TASK=/etc/supervisor/conf.d/laravel-worker.conf
touch $TASK
cat > "$TASK" <<EOF
[supervisord]
nodaemon=true
user=root
[program:Laravel-scheduler]
process_name=%(program_name)s_%(process_num)02d
command=/bin/sh -c "while [ true ]; do (php $WORKDIR/artisan schedule:run --verbose --no-interaction &); sleep 60; done"
autostart=true
autorestart=true
numprocs=1
user=root
stdout_logfile=/var/log/laravel_scheduler.out.log
redirect_stderr=true
[program:Laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php $WORKDIR/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
numprocs=$LARAVEL_PROCS_NUMBER
user=root
redirect_stderr=true
stdout_logfile=/var/log/laravel_worker.log
EOF
echo "${Green} Laravel supervisor config created"
else
echo "${Red} artisan file not found"
fi
#check if storage directory exists
echo "Checking if storage directory exists"
if [ -d "$STORAGE_DIR" ]; then
echo "Directory $STORAGE_DIR exist. Fixing permissions..."
chown -R www-data:www-data $STORAGE_DIR
chmod -R 775 $STORAGE_DIR
echo "${Green}Permissions fixed"
else
echo "${Red} Directory $STORAGE_DIR does not exist"
fi
# Enable custom nginx config files if they exist
if [ -f /var/www/html/conf/nginx/nginx.conf ]; then
cp /var/www/html/conf/nginx/nginx.conf /etc/nginx/nginx.conf
echo "Using custom nginx.conf"
fi
if [ -f /var/www/html/conf/nginx/nginx-site.conf ]; then
echo "Custom nginx site config found"
rm /etc/nginx/sites-enabled/default
cp /var/www/html/conf/nginx/nginx-site.conf /etc/nginx/sites-enabled/default
else
echo "${Red} nginx-site.conf not found"
echo "${Green} If you want to use custom configs, create config file in /var/www/html/conf/nginx/nginx-site.conf"
echo "${Green} Start nginx with default config..."
rm -f /etc/nginx/sites-enabled/default
TASK=/etc/nginx/sites-enabled/default
touch $TASK
cat > "$TASK" <<EOF
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name $DOMAIN;
# Add index.php to setup Nginx, PHP & PHP-FPM config
index index.php index.html index.htm index.nginx-debian.html; error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root $DOCUMENT_ROOT;
# pass PHP scripts on Nginx to FastCGI (PHP-FPM) server
location ~ \.php$ {
try_files \$uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Nginx php-fpm config:
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_param PATH_INFO \$fastcgi_path_info;
}
client_max_body_size $CLIENT_MAX_BODY_SIZE;
server_tokens off;
# Hide PHP headers
fastcgi_hide_header X-Powered-By;
fastcgi_hide_header X-CF-Powered-By;
fastcgi_hide_header X-Runtime;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
gzip_static on;
}
# deny access to Apache .htaccess on Nginx with PHP,
# if Apache and Nginx document roots concur
location ~ /\.ht {
deny all;
}
location ~ .*\.(?!htaccess).* {
deny all;
}
}
EOF
fi
TASK=/etc/supervisor/conf.d/nginx.conf
touch $TASK
cat > "$TASK" <<EOF
[supervisord]
nodaemon=true
user=root
[program:ginx]
command=/usr/sbin/nginx -g "daemon off;"
numprocs=1
autostart=true
autorestart=true
stderr_logfile=/var/log/ngnix.err.log
stdout_logfile=/var/log/ngnix.out.log
EOF
echo ""
echo "**********************************"
echo " Starting Supervisord... "
echo "***********************************"
supervisord -c /etc/supervisor/supervisord.conf

View File

@@ -0,0 +1,42 @@
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# Add index.php to setup Nginx, PHP & PHP-FPM config
index index.php index.html index.htm index.nginx-debian.html; error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
# pass PHP scripts on Nginx to FastCGI (PHP-FPM) server
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Nginx php-fpm config:
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
client_max_body_size 15M;
server_tokens off;
# Hide PHP headers
fastcgi_hide_header X-Powered-By;
fastcgi_hide_header X-CF-Powered-By;
fastcgi_hide_header X-Runtime;
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
# deny access to Apache .htaccess on Nginx with PHP,
# if Apache and Nginx document roots concur
location ~ /\.ht {
deny all;
}
location ~ .*\.(?!htaccess).* {
deny all;
}
}

3
src/index.php Normal file
View File

@@ -0,0 +1,3 @@
<?php
phpinfo();
?>

6
src/php.ini Normal file
View File

@@ -0,0 +1,6 @@
date.timezone=UTC
display_errors=Off
log_errors=On
upload_max_filesize= 80M
post_max_size= 80M
memory_limit = 256M