Refactoring

This commit is contained in:
2022-08-14 14:43:00 +02:00
parent d45a6dea78
commit 43fe96b0cb
5 changed files with 52 additions and 63 deletions

View File

@@ -1,8 +1,7 @@
FROM php:8.1-fpm FROM php:8.1.7-fpm
ENV WORKDIR=/var/www/html ARG WORKDIR=/var/www/html
ENV STORAGE_DIR=${WORKDIR}/storage
ENV DOCUMENT_ROOT=${WORKDIR} ENV DOCUMENT_ROOT=${WORKDIR}
ENV LARAVEL_PROCS_NUMBER=2 ENV LARAVEL_PROCS_NUMBER=1
ENV DOMAIN=_ ENV DOMAIN=_
ENV CLIENT_MAX_BODY_SIZE=15M ENV CLIENT_MAX_BODY_SIZE=15M
# Install system dependencies # Install system dependencies
@@ -71,13 +70,13 @@ RUN composer global require "laravel/envoy=~1.0"
# Set working directory # Set working directory
WORKDIR $WORKDIR WORKDIR $WORKDIR
# nginx site conf
RUN rm -Rf /var/www/* && \ RUN rm -Rf /var/www/* && \
mkdir /var/www/html/ mkdir -p /var/www/html
ADD src/index.php $WORKDIR/index.php ADD src/index.php $WORKDIR/index.php
ADD src/conf/nginx/default.conf /etc/nginx/sites-available/default ADD src/conf/nginx/default.conf /etc/nginx/sites-available/default
ADD src/php.ini $PHP_INI_DIR/conf.d/ ADD src/php.ini $PHP_INI_DIR/conf.d/
ADD src/supervisor/supervisord.conf /etc/supervisor/supervisord.conf
COPY ./entrypoint.sh /usr/local/bin/ COPY ./entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh RUN chmod +x /usr/local/bin/entrypoint.sh
@@ -93,4 +92,6 @@ RUN groupmod -g 1000 www-data
RUN chmod -R 755 $WORKDIR RUN chmod -R 755 $WORKDIR
RUN chown -R www-data:www-data $WORKDIR RUN chown -R www-data:www-data $WORKDIR
EXPOSE 9000 EXPOSE 9000
EXPOSE 80
CMD [ "entrypoint" ] CMD [ "entrypoint" ]

View File

@@ -50,13 +50,11 @@ services:
## Laravel `artisan` command usage: ## Laravel `artisan` command usage:
### CLI ### CLI
```sh ```sh
docker-compose exec app bash docker-compose exec app bash
``` ```
### Laravel migration
```sh ```sh
php atisan migrate docker exec -it app bash
``` ```
@@ -67,8 +65,7 @@ version: '3'
services: services:
app: app:
image: jkaninda/nginx-php-fpm image: jkaninda/nginx-php-fpm
container_name: my-app container_name: nginx-fpm
working_dir: /var/www/html #Optional, If you want to use a custom directory
restart: unless-stopped restart: unless-stopped
ports: ports:
- "80:80" - "80:80"
@@ -76,15 +73,14 @@ services:
#Project root #Project root
- ./:/var/www/html - ./:/var/www/html
- ~/.ssh:/root/.ssh # If you use private CVS - ~/.ssh:/root/.ssh # If you use private CVS
- #./php.ini:/usr/local/etc/php/conf.d/php.ini # Optional, your custom php init 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: environment:
- APP_ENV=development # Optional, or production - APP_ENV=development # Optional, or production
- LARAVEL_PROCS_NUMBER=3 # Optional, Laravel queue:work process number - LARAVEL_PROCS_NUMBER=2 # Optional, Laravel queue:work process number
- CLIENT_MAX_BODY_SIZE=20M # Optional #- CLIENT_MAX_BODY_SIZE=20M # Optional
- DOMAIN=example.com # Optional #- DOMAIN=example.com # Optional
volumes: #- DOCUMENT_ROOT=/var/www/html Optional
storage-data:
``` ```
## Docker run ## Docker run

View File

@@ -8,25 +8,11 @@ echo "***********************************************************"
set -e set -e
## Create PHP-FPM worker process
TASK=/etc/supervisor/conf.d/php-fpm.conf
touch $TASK
cat > "$TASK" <<EOF
[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 ## Check if the artisan file exists
if [ -f $WORKDIR/artisan ]; then if [ -f /var/www/html/artisan ]; then
echo "${Green} artisan file found, creating laravel supervisor config" echo "${Green} artisan file found, creating laravel supervisor config"
# Set DocumentRoot to the Laravel project directory # Set DocumentRoot to the Laravel project directory
export DOCUMENT_ROOT=$WORKDIR/public export DOCUMENT_ROOT=/var/www/html/public
##Create Laravel Scheduler process ##Create Laravel Scheduler process
TASK=/etc/supervisor/conf.d/laravel-worker.conf TASK=/etc/supervisor/conf.d/laravel-worker.conf
touch $TASK touch $TASK
@@ -36,7 +22,7 @@ if [ -f $WORKDIR/artisan ]; then
user=root user=root
[program:Laravel-scheduler] [program:Laravel-scheduler]
process_name=%(program_name)s_%(process_num)02d 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" command=/bin/sh -c "while [ true ]; do (php /var/www/html/artisan schedule:run --verbose --no-interaction &); sleep 60; done"
autostart=true autostart=true
autorestart=true autorestart=true
numprocs=1 numprocs=1
@@ -46,7 +32,7 @@ if [ -f $WORKDIR/artisan ]; then
[program:Laravel-worker] [program:Laravel-worker]
process_name=%(program_name)s_%(process_num)02d process_name=%(program_name)s_%(process_num)02d
command=php $WORKDIR/artisan queue:work --sleep=3 --tries=3 command=php /var/www/html/artisan queue:work --sleep=3 --tries=3
autostart=true autostart=true
autorestart=true autorestart=true
numprocs=$LARAVEL_PROCS_NUMBER numprocs=$LARAVEL_PROCS_NUMBER
@@ -58,17 +44,6 @@ echo "${Green} Laravel supervisor config created"
else else
echo "${Red} artisan file not found" echo "${Red} artisan file not found"
fi 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 # Enable custom nginx config files if they exist
if [ -f /var/www/html/conf/nginx/nginx.conf ]; then if [ -f /var/www/html/conf/nginx/nginx.conf ]; then
@@ -94,7 +69,8 @@ if [ -f /var/www/html/conf/nginx/nginx-site.conf ]; then
listen [::]:80 default_server; listen [::]:80 default_server;
server_name $DOMAIN; server_name $DOMAIN;
# Add index.php to setup Nginx, PHP & PHP-FPM config # 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; 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; access_log /var/log/nginx/access.log;
root $DOCUMENT_ROOT; root $DOCUMENT_ROOT;
# pass PHP scripts on Nginx to FastCGI (PHP-FPM) server # pass PHP scripts on Nginx to FastCGI (PHP-FPM) server
@@ -146,19 +122,8 @@ if [ -f /var/www/html/conf/worker/supervisor.conf ]; then
echo "${Green} If you want to add more supervisor configs, create config file in /var/www/html/conf/worker/supervisor.conf" echo "${Green} If you want to add more supervisor configs, create config file in /var/www/html/conf/worker/supervisor.conf"
echo "${Green} Start supervisor with default config..." echo "${Green} Start supervisor with default config..."
fi fi
## Add nginx process to supervisor
TASK=/etc/supervisor/conf.d/nginx.conf
touch $TASK
cat > "$TASK" <<EOF
[program:nginx]
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
user=root
EOF
echo "" echo ""
echo "**********************************" echo "**********************************"

View File

@@ -6,7 +6,7 @@ server {
# Add index.php to setup Nginx, PHP & PHP-FPM config # 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; 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; access_log /var/log/nginx/access.log;
root /var/www/html/public; root /var/www/html;
# pass PHP scripts on Nginx to FastCGI (PHP-FPM) server # pass PHP scripts on Nginx to FastCGI (PHP-FPM) server
location ~ \.php$ { location ~ \.php$ {
try_files $uri =404; try_files $uri =404;

View File

@@ -0,0 +1,27 @@
[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
[program:nginx]
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
user=root
priority=100
[include]
files = /etc/supervisor/conf.d/*.conf