Laravel 第一版 整合在一起 20250609

This commit is contained in:
allen.yan 2025-06-09 17:14:04 +08:00
parent b982b8cf34
commit 6ab0fc1770
8 changed files with 81 additions and 151 deletions

View File

@ -1,29 +1,32 @@
FROM php:8.3-fpm FROM php:8.3-fpm
# ---- system & PHP extensions -------------------------------------------------
RUN apt-get update && apt-get install -y \ RUN apt-get update && apt-get install -y \
git unzip zip curl libpng-dev libjpeg62-turbo-dev libfreetype6-dev \ git unzip zip curl ca-certificates supervisor cron nano \
libpng-dev libjpeg62-turbo-dev libfreetype6-dev \
libonig-dev libxml2-dev libzip-dev libpq-dev libicu-dev libxslt-dev \ libonig-dev libxml2-dev libzip-dev libpq-dev libicu-dev libxslt-dev \
libsqlite3-dev sqlite3 cron supervisor bash nano default-mysql-client \ libsqlite3-dev sqlite3 default-mysql-client \
&& curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \ && curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
&& apt-get install -y nodejs \ && apt-get install -y nodejs \
&& npm install -g npm \ && npm install -g npm \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \ && docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd pdo_mysql zip bcmath intl xsl pcntl sockets \ && docker-php-ext-install -j$(nproc) gd pdo_mysql zip bcmath intl xsl pcntl sockets \
&& docker-php-ext-install opcache \ && docker-php-ext-install opcache \
&& pecl install redis && docker-php-ext-enable redis && pecl install redis && docker-php-ext-enable redis \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
# ---- composer ---------------------------------------------------------------
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY ./docker/php/php.ini /usr/local/etc/php/conf.d/custom-php.ini
COPY ./docker/php/opcache.ini /usr/local/etc/php/conf.d/opcache.ini
COPY ./data/html/ /var/www/html/
RUN chown -R www-data:www-data /var/www/html # ---- config files -----------------------------------------------------------
COPY docker/php/php.ini /usr/local/etc/php/conf.d/custom-php.ini
COPY docker/php/opcache.ini /usr/local/etc/php/conf.d/opcache.ini
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY docker/entrypoint.sh /entrypoint.sh
COPY docker/entrypoint.git.sh /entrypoint.git.sh
WORKDIR /var/www
WORKDIR /var/www/html RUN chmod +x /entrypoint.sh /entrypoint.git.sh
COPY ./docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"] ENTRYPOINT ["/entrypoint.sh"]
CMD ["docker-php-entrypoint", "php-fpm"] CMD ["docker-php-entrypoint", "php-fpm"]

View File

@ -1,21 +0,0 @@
FROM debian:bullseye-slim
# 安裝必要工具
RUN apt-get update && apt-get install -y \
git supervisor bash curl ca-certificates \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
# 建立目錄
RUN mkdir -p /app /var/log /etc/supervisor
# 複製 supervisord 設定與啟動腳本
COPY ./docker/supervisord.git.conf /etc/supervisor/supervisord.conf
COPY ./docker/entrypoint.git.sh /entrypoint.sh
# 權限設定
RUN chmod +x /entrypoint.sh
WORKDIR /app
# 使用 supervisord 啟動
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]

View File

@ -1,17 +0,0 @@
FROM php:8.3-fpm
# 安裝必要套件與 PHP 擴充
RUN apt-get update && apt-get install -y \
git unzip zip curl libzip-dev libonig-dev libxml2-dev supervisor \
&& docker-php-ext-install pdo_mysql zip bcmath \
&& pecl install redis && docker-php-ext-enable redis
COPY ./data/html/ /var/www/html/
RUN chown -R www-data:www-data /var/www/html
WORKDIR /var/www/html
COPY ./docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
CMD ["supervisord", "-n", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

View File

@ -1,16 +1,4 @@
services: services:
git_worker:
build:
context: .
dockerfile: Dockerfile.git
container_name: ${APP_NAME}_git_worker
volumes:
- ./data:/app
- ./data/git/logs/:/var/log/
environment:
- REPO_URL=${REPO_URL}
- BRANCH=${REPO_BRANCH}
restart: unless-stopped
mariadb: mariadb:
image: mariadb:10.6 image: mariadb:10.6
container_name: ${APP_NAME}_db container_name: ${APP_NAME}_db
@ -29,19 +17,19 @@ services:
- "3306:3306" - "3306:3306"
networks: networks:
- app_network - app_network
app: app:
build: build:
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
container_name: ${APP_NAME}_app container_name: ${APP_NAME}_app
volumes: volumes:
- ./data/html:/var/www/html - ./data:/var/www
- ./data/php/logs:/var/www/logs
- ./docker/php/php.ini:/usr/local/etc/php/conf.d/custom-php.ini
- ./docker/php/opcache.ini:/usr/local/etc/php/conf.d/opcache.ini
depends_on: depends_on:
- mariadb - mariadb
environment: environment:
REPO_URL: ${REPO_URL}
BRANCH: ${REPO_BRANCH:-main}
APP_NAME: ${APP_NAME} APP_NAME: ${APP_NAME}
APP_URL: ${APP_URL} APP_URL: ${APP_URL}
DB_HOST: ${DB_HOST} DB_HOST: ${DB_HOST}
@ -51,6 +39,7 @@ services:
DB_PASSWORD: ${DB_PASSWORD} DB_PASSWORD: ${DB_PASSWORD}
networks: networks:
- app_network - app_network
nginx: nginx:
image: nginx:stable-alpine image: nginx:stable-alpine
container_name: ${APP_NAME}_nginx container_name: ${APP_NAME}_nginx
@ -68,20 +57,6 @@ services:
NGINX_PORT: ${APP_PORT} NGINX_PORT: ${APP_PORT}
networks: networks:
- app_network - app_network
queue:
build:
context: .
dockerfile: Dockerfile.queue
container_name: ${APP_NAME}_queue
volumes:
- ./data/html:/var/www/html
- ./data/queue/logs:/var/www/logs
- ./docker/supervisord.conf:/etc/supervisor/conf.d/supervisord.conf
depends_on:
- nginx
networks:
- app_network
networks: networks:
app_network: app_network:
name: ${APP_NAME}_network
external: true

View File

@ -1,22 +1,24 @@
#!/bin/bash #!/usr/bin/env bash
set -e
REPO_URL=${REPO_URL} BRANCH=${BRANCH:-main}
BRANCH=${BRANCH} TARGET_DIR=/var/www/html
TARGET_DIR=/app/html
# 若資料夾為空則 clone echo "[git-worker] Loop started for branch $BRANCH"
if [ -z "$(ls -A "$TARGET_DIR")" ]; then
echo "[git-worker] Cloning repository into $TARGET_DIR..."
git clone --branch "$BRANCH" "$REPO_URL" "$TARGET_DIR"
else
echo "[git-worker] Repository exists in $TARGET_DIR, skipping clone."
fi
# 定時 git pull
echo "[git-worker] Starting periodic git pull..."
while true; do while true; do
cd "$TARGET_DIR" || exit cd "$TARGET_DIR"
echo "[git-worker] Pulling latest changes from $BRANCH..." git remote update
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse "origin/$BRANCH")
if [ "$LOCAL" != "$REMOTE" ]; then
echo "[git-worker] Detected new commits, pulling..."
git pull origin "$BRANCH" git pull origin "$BRANCH"
sleep 300
echo "[git-worker] Restarting queue-worker..."
supervisorctl restart queue-worker
else
echo "[git-worker] No changes."
fi
sleep 300
done done

View File

@ -1,51 +1,44 @@
#!/bin/bash #!/usr/bin/env bash
set -e
cd /var/www/html REPO_URL=${REPO_URL}
BRANCH=${BRANCH:-main}
TARGET_DIR=/var/www/html
APP_URL="${APP_URL%/}" # 1) 初次 clone 或拉取
if [ -z "$(ls -A "$TARGET_DIR")" ]; then
echo "[entrypoint] Cloning $BRANCH from $REPO_URL ..."
git clone --branch "$BRANCH" "$REPO_URL" "$TARGET_DIR"
echo "Running composer install..." else
composer install --no-interaction --prefer-dist echo "[entrypoint] Repository already present, skipping first clone."
if [ ! -f ".env" ]; then
echo "Copying .env.example to .env"
cp .env.example .env
fi fi
echo "Updating .env configurations..." cd "$TARGET_DIR"
update_env_var() { # 2) Laravel 基礎安裝
local key=$1 composer install --no-interaction --prefer-dist
local value=$2
grep -q "^${key}=" .env && \
sed -i "s|^${key}=.*|${key}=${value}|" .env || \
echo "${key}=${value}" >> .env
}
update_env_var "APP_NAME" "$APP_NAME" [ -f .env ] || cp .env.example .env
update_env_var "APP_URL" "$APP_URL"
update_env_var "DB_HOST" "$DB_HOST"
update_env_var "DB_PORT" "$DB_PORT"
update_env_var "DB_DATABASE" "$DB_DATABASE"
update_env_var "DB_USERNAME" "$DB_USERNAME"
update_env_var "DB_PASSWORD" "$DB_PASSWORD"
echo "Generating Laravel app key..." update_env() { local k=$1 v=$2; grep -q "^$k=" .env && sed -i "s|^$k=.*|$k=$v|" .env || echo "$k=$v" >> .env; }
php artisan key:generate update_env APP_NAME "$APP_NAME"
update_env APP_URL "${APP_URL%/}"
update_env DB_HOST "$DB_HOST"
update_env DB_PORT "$DB_PORT"
update_env DB_DATABASE "$DB_DATABASE"
update_env DB_USERNAME "$DB_USERNAME"
update_env DB_PASSWORD "$DB_PASSWORD"
echo "Running migrations..." php artisan key:generate --force
php artisan migrate --force php artisan migrate --force
echo "Installing npm packages..." [ -d node_modules ] || npm install
[ ! -d "node_modules" ] && npm install
echo "Building front-end assets..."
npm run build npm run build
echo "Setting permissions for storage and bootstrap/cache..." chown -R www-data:www-data storage bootstrap/cache
chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache chmod -R 775 storage bootstrap/cache
chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache
echo "[entrypoint] Starting supervisord ..."
supervisord -c /etc/supervisor/conf.d/supervisord.conf &
exec "$@" exec "$@"

View File

@ -8,15 +8,22 @@ directory=/var/www/html
command=php artisan queue:work --daemon --timeout=3600 --tries=1 --queue=default command=php artisan queue:work --daemon --timeout=3600 --tries=1 --queue=default
autostart=true autostart=true
autorestart=true autorestart=true
priority=1
stdout_logfile=/var/www/logs/queue.log stdout_logfile=/var/www/logs/queue.log
stderr_logfile=/var/www/logs/queue_error.log stderr_logfile=/var/www/logs/queue_error.log
stopsignal=INT
[program:scheduler] [program:scheduler]
directory=/var/www/html directory=/var/www/html
command=sh -c "while true; do php artisan schedule:run; sleep 60; done" command=/bin/sh -c "while true; do php artisan schedule:run; sleep 60; done"
autostart=true autostart=true
autorestart=true autorestart=true
priority=2
stdout_logfile=/var/www/logs/schedule.log stdout_logfile=/var/www/logs/schedule.log
stderr_logfile=/var/www/logs/schedule_error.log stderr_logfile=/var/www/logs/schedule_error.log
[program:git-worker]
command=/bin/sh /entrypoint.git.sh
autostart=true
autorestart=true
stdout_logfile=/var/www/logs/git-worker.log
stderr_logfile=/var/www/logs/git-worker_error.log
startsecs=0

View File

@ -1,12 +0,0 @@
[supervisord]
nodaemon=true
logfile=/var/log/supervisord.log
pidfile=/var/run/supervisord.pid
[program:git-worker]
command=/entrypoint.sh
autostart=true
autorestart=true
startsecs=0
stdout_logfile=/var/log/git-worker.log
stderr_logfile=/var/log/git-worker.err.log