From dd48154796fae901c47405ac0451cfa29d206817 Mon Sep 17 00:00:00 2001 From: NaysKutzu Date: Sun, 24 Nov 2024 00:14:52 +0100 Subject: [PATCH] PUSH -> Docker :) --- .gitignore | 1 + backend/.dockerignore | 1 + backend/Dockerfile | 43 ++++++++++ backend/mythicalclient | 15 ++++ backend/storage/.env | 7 +- docker-compose.yml | 104 +++++++++++++++++------- dockerfiles/frontend/Dockerfile | 9 -- dockerfiles/mariadb/.gitignore | 0 dockerfiles/php/Dockerfile | 7 -- framework.conf | 35 ++++++++ frontend/.dockerignore | 1 + frontend/Dockerfile | 13 +++ frontend/src/mythicalclient/Settings.ts | 17 ++-- frontend/vite.config.ts | 18 ++-- 14 files changed, 204 insertions(+), 67 deletions(-) create mode 100755 .gitignore create mode 100755 backend/.dockerignore create mode 100755 backend/Dockerfile create mode 100755 backend/mythicalclient delete mode 100755 dockerfiles/frontend/Dockerfile delete mode 100755 dockerfiles/mariadb/.gitignore delete mode 100755 dockerfiles/php/Dockerfile create mode 100755 framework.conf create mode 100755 frontend/.dockerignore create mode 100755 frontend/Dockerfile diff --git a/.gitignore b/.gitignore new file mode 100755 index 00000000..b929d822 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +dockerfiles/mariadb/data \ No newline at end of file diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100755 index 00000000..6e925e22 --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1 @@ +/storage/packages \ No newline at end of file diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100755 index 00000000..78f70456 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,43 @@ +FROM php:8.3-fpm + +WORKDIR /var/www/html + +# Install system dependencies and PHP extensions +RUN apt-get update && apt-get install -y \ + libpng-dev \ + libjpeg-dev \ + libfreetype6-dev \ + libxml2-dev \ + libzip-dev \ + libonig-dev \ + libcurl4-openssl-dev \ + zip \ + unzip \ + git \ + && docker-php-ext-configure gd --with-freetype --with-jpeg \ + && docker-php-ext-install \ + gd \ + pdo \ + pdo_mysql \ + mysqli \ + bcmath \ + xml \ + mbstring \ + curl \ + zip + +# Install Composer +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer + +# Copy composer files first to leverage Docker cache +COPY composer.json composer.lock ./ + +# Install dependencies +RUN cd /var/www/html && COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev --optimize-autoloader + +# Copy rest of the application +COPY . . + +# Set permissions +RUN chown -R www-data:www-data /var/www/html \ + && chmod -R 755 /var/www/html \ No newline at end of file diff --git a/backend/mythicalclient b/backend/mythicalclient new file mode 100755 index 00000000..fa8f54e1 --- /dev/null +++ b/backend/mythicalclient @@ -0,0 +1,15 @@ +getMessage(); +} diff --git a/backend/storage/.env b/backend/storage/.env index 132e60e7..92445fe5 100755 --- a/backend/storage/.env +++ b/backend/storage/.env @@ -1,7 +1,8 @@ -DATABASE_HOST=127.0.0.1 +DATABASE_HOST=mythical_database DATABASE_PORT=3306 DATABASE_USER=mythical -DATABASE_PASSWORD=efwefwefwe +DATABASE_PASSWORD="Pizza2007!" DATABASE_DATABASE=mythicalclient DATABASE_ENCRYPTION=xchacha20 -DATABASE_ENCRYPTION_KEY="zKZ7//AocUD2wCuzjW5rdTbb8FvGBgKTZ8iCATslZ/8=" \ No newline at end of file +DATABASE_ENCRYPTION_KEY="zKZ7//AocUD2wCuzjW5rdTbb8FvGBgKTZ8iCATslZ/8=" +REDIS_PASSWORD="eufefwefwefw" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index c0c58faa..c1e4b9f6 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,46 +1,90 @@ +version: '3.8' + services: - php: + # Frontend Vue service + mythical_frontend: build: - context: ./dockerfiles/php - restart: unless-stopped + context: ./frontend + dockerfile: Dockerfile + container_name: mythical_frontend volumes: - - .:/var/www/ - expose: - - 9000 - depends_on: - database: - condition: service_healthy - env_file: ./backend/storage/.env - extra_hosts: - host.docker.internal: host-gateway + - ./frontend:/app + - /app/node_modules + networks: + - mythical_network - frontend: - build: - context: . - dockerfile: ./dockerfiles/frontend + # PHP Backend service with PHP-FPM + mythical_backend: + build: + context: ./backend + dockerfile: Dockerfile + container_name: mythical_backend volumes: - - ./:/var/www/ - restart: unless-stopped + - ./backend:/var/www/html + - ./backend/vendor:/var/www/html/vendor + depends_on: + - mythical_database + networks: + - mythical_network - composer: - image: composer:latest - working_dir: /var/www/ + # Main Nginx service + mythical_nginx: + image: nginx:alpine + container_name: mythical_nginx + ports: + - "8080:80" volumes: - - .:/var/www - entrypoint: ["composer", "--ignore-platform-reqs", "--no-progress", "--no-dev", "--optimize-autoloader"] - command: ["install"] - - database: - image: mariadb:10.5 + - ./backend:/var/www/html + - ./framework.conf:/etc/nginx/conf.d/default.conf + depends_on: + - mythical_frontend + - mythical_backend + networks: + - mythical_network + + mythical_database: + image: mariadb:latest restart: unless-stopped + container_name: mythical_database volumes: - - ./dockerfiles/mariadb/data:/var/lib/mysql + - ./dockerfiles/mariadb/data:/var/lib/mysql/ ports: - 3306:3306 + - 33060:33060 env_file: - ./backend/storage/.env environment: - MYSQL_RANDOM_ROOT_PASSWORD: "true" + MARIADB_ROOT_PASSWORD: "superlongquiery!!password" MARIADB_DATABASE: ${DATABASE_DATABASE} MARIADB_USER: ${DATABASE_USER} - MARIADB_PASSWORD: ${DATABASE_PASSWORD} \ No newline at end of file + MARIADB_PASSWORD: ${DATABASE_PASSWORD} + healthcheck: + test: mysqladmin ping -u$$MARIADB_USER -p$$MARIADB_PASSWORD + interval: 5s + timeout: 5s + retries: 20 + networks: + - mythical_network + + mythical_redis: + image: redis:7.0-alpine + container_name: mythical_redis + restart: unless-stopped + command: redis-server --save 60 1 --loglevel notice --requirepass '${REDIS_PASSWORD}' + env_file: + - ./backend/storage/.env + ports: + - 6379:6379 + environment: + REDIS_PASSWORD: ${REDIS_PASSWORD} + healthcheck: + test: redis-cli -a $$REDIS_PASSWORD ping | grep PONG + interval: 5s + timeout: 5s + retries: 20 + networks: + - mythical_network + +networks: + mythical_network: + driver: bridge diff --git a/dockerfiles/frontend/Dockerfile b/dockerfiles/frontend/Dockerfile deleted file mode 100755 index dc322372..00000000 --- a/dockerfiles/frontend/Dockerfile +++ /dev/null @@ -1,9 +0,0 @@ -FROM node:20-alpine - -WORKDIR /var/www/ - -RUN ls /var/www/ - -RUN yarn install -RUN yarn build - diff --git a/dockerfiles/mariadb/.gitignore b/dockerfiles/mariadb/.gitignore deleted file mode 100755 index e69de29b..00000000 diff --git a/dockerfiles/php/Dockerfile b/dockerfiles/php/Dockerfile deleted file mode 100755 index 42fc3cfc..00000000 --- a/dockerfiles/php/Dockerfile +++ /dev/null @@ -1,7 +0,0 @@ -FROM php:8.3-fpm - -RUN apt -y install php8.3-{common,cli,gd,mysql,mbstring,bcmath,xml,fpm,curl,zip,pdo} - -WORKDIR /var/www/backend/ - -CMD ["php-fpm"] \ No newline at end of file diff --git a/framework.conf b/framework.conf new file mode 100755 index 00000000..ab8decd2 --- /dev/null +++ b/framework.conf @@ -0,0 +1,35 @@ +server { + listen 80; + server_name framework.mythical.systems; + + # Root directory for PHP backend + root /var/www/html/public; + index index.php index.html index.htm; + + # Location for the PHP backend (handles all /api requests and PHP files) + location /api { + try_files $uri $uri/ /index.php; + } + + # General PHP script handling (via PHP-FPM) + location ~ \.php$ { + include fastcgi_params; + fastcgi_pass mythical_backend:9000; # PHP-FPM is running here + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + } + + # Location for the frontend + location / { + proxy_pass http://mythical_frontend:80; # Replace 'frontend:80' with your frontend service + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + # Deny access to sensitive files + location ~ /\.(ht|git|svn) { + deny all; + } +} diff --git a/frontend/.dockerignore b/frontend/.dockerignore new file mode 100755 index 00000000..30bc1627 --- /dev/null +++ b/frontend/.dockerignore @@ -0,0 +1 @@ +/node_modules \ No newline at end of file diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100755 index 00000000..3dbbcda7 --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,13 @@ +# Build stage +FROM node:20 as build-stage +WORKDIR /app +COPY package*.json ./ +RUN yarn install +COPY . . +RUN yarn run build + +# Production stage +FROM nginx:alpine +COPY --from=build-stage /app/dist /usr/share/nginx/html +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/frontend/src/mythicalclient/Settings.ts b/frontend/src/mythicalclient/Settings.ts index cf99e954..4ad9db36 100755 --- a/frontend/src/mythicalclient/Settings.ts +++ b/frontend/src/mythicalclient/Settings.ts @@ -1,5 +1,3 @@ -import Swal from 'sweetalert2'; - class Settings { static settings = {}; @@ -28,13 +26,14 @@ class Settings { } } catch (error) { console.error('Failed to initialize settings:', error); - Swal.fire({ - icon: 'error', - title: 'Failed to initialize session!', - text: 'It looks like something went really wrong. Please try again later or contact the webmaster.', - }).then(() => { - document.body.innerHTML = '

We are so sorry but our backend is down at this moment :(

'; - }); + document.body.innerHTML = ` +
+
+

We are so sorry

+

Our backend is down at this moment :(

+
+
+ `; } } diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 634b495c..b63bc5c1 100755 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -17,13 +17,13 @@ export default defineConfig({ '@': fileURLToPath(new URL('./src', import.meta.url)) }, }, - server: { - proxy: { - '/api': { - target: 'http://localhost:9000', - changeOrigin: true, - rewrite: (path) => path - } - } - } + // server: { + // proxy: { + // '/api': { + // target: 'http://localhost:9000', + // changeOrigin: true, + // rewrite: (path) => path + // } + // } + // } })