From 689ce8e32e5f793633ebd0b0b2add1ddaa8c8798 Mon Sep 17 00:00:00 2001 From: Higor de Deus Date: Sun, 14 Jul 2024 14:20:41 -0300 Subject: [PATCH 1/2] Added Docker setup files and configuration --- Dockerfile | 68 ++++++++++++++++++++++++++++ docker-compose.yml | 73 ++++++++++++++++++++++++++++++ nginx/default.conf | 25 +++++++++++ setup_gymie_docker.sh | 100 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 266 insertions(+) create mode 100755 Dockerfile create mode 100755 docker-compose.yml create mode 100755 nginx/default.conf create mode 100755 setup_gymie_docker.sh diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 00000000..6721a4c8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,68 @@ +# syntax=docker/dockerfile:1 +FROM php:7.1-fpm + +# Set the working directory +WORKDIR /var/www + +# Install necessary dependencies and diagnostic tools +RUN apt-get update && apt-get install -y \ + build-essential \ + libpng-dev \ + libjpeg62-turbo-dev \ + libfreetype6-dev \ + locales \ + zip \ + jpegoptim optipng pngquant gifsicle \ + vim \ + unzip \ + git \ + curl \ + libmagickwand-dev --no-install-recommends \ + libonig-dev \ + nano \ + iputils-ping \ + procps \ + net-tools \ + iproute2 \ + lsof \ + strace \ + htop + +# Clean up the apt cache to reduce the image size +RUN apt-get clean && rm -rf /var/lib/apt/lists/* + +# Install necessary PHP extensions +RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd tokenizer ctype json xml + +# Install and enable the Imagick extension +RUN pecl install imagick && docker-php-ext-enable imagick + +# Install Composer +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer + +# Copy the content of the current directory to the working directory in the container +COPY . /var/www + +# Set ownership and permissions for the copied files +COPY --chown=www-data:www-data . /var/www + +# Create PHP-FPM log directory and set permissions +RUN mkdir -p /var/log/php-fpm && chown -R www-data:www-data /var/log/php-fpm + +# Add custom PHP-FPM configurations +RUN echo "catch_workers_output = yes" >> /usr/local/etc/php-fpm.d/www.conf +RUN echo "pm = ondemand" >> /usr/local/etc/php-fpm.d/www.conf +RUN echo "pm.max_requests = 500" >> /usr/local/etc/php-fpm.d/www.conf +RUN echo "request_slowlog_timeout = 5s" >> /usr/local/etc/php-fpm.d/www.conf +RUN echo "slowlog = /var/log/php-fpm/www-slow.log" >> /usr/local/etc/php-fpm.d/www.conf +RUN echo "rlimit_files = 65535" >> /usr/local/etc/php-fpm.d/www.conf + +# Set permissions for Laravel directories +RUN chown -R www-data:www-data /var/www && chmod -R 755 /var/www/storage /var/www/bootstrap/cache + +# Switch to the www-data user +USER www-data + +# Expose port 9000 and start the PHP-FPM server +EXPOSE 9000 +CMD ["php-fpm"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100755 index 00000000..1b0eac29 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,73 @@ +version: '3.9' +services: + app: + build: + context: . + dockerfile: Dockerfile + image: laravel-gymie + container_name: laravel_gymie_app + restart: unless-stopped + working_dir: /var/www + volumes: + - ./:/var/www + depends_on: + - mysql + networks: + - laravel_gymie + deploy: + resources: + limits: + cpus: '0.50' + memory: 512M + + mysql: + image: mysql:5.7 + container_name: laravel_gymie_db + restart: unless-stopped + environment: + MYSQL_DATABASE: gymie + MYSQL_ROOT_PASSWORD: root + MYSQL_USER: gymie + MYSQL_PASSWORD: password + volumes: + - mysql_data:/var/lib/mysql + ports: + - "3306:3306" + networks: + - laravel_gymie + deploy: + resources: + limits: + cpus: '0.50' + memory: 512M + ulimits: + nofile: + soft: 1048576 + hard: 1048576 + + nginx: + image: nginx:latest + container_name: laravel_gymie_nginx + restart: unless-stopped + ports: + - "8081:80" + volumes: + - ./nginx/default.conf:/etc/nginx/conf.d/default.conf + - .:/var/www + depends_on: + - app + networks: + - laravel_gymie + deploy: + resources: + limits: + cpus: '0.25' + memory: 256M + +networks: + laravel_gymie: + driver: bridge + +volumes: + mysql_data: + driver: local diff --git a/nginx/default.conf b/nginx/default.conf new file mode 100755 index 00000000..364a0326 --- /dev/null +++ b/nginx/default.conf @@ -0,0 +1,25 @@ +server { + listen 80; + + server_name localhost; + root /var/www/public; + + index index.php index.html; + + location / { + try_files $uri $uri/ /index.php?$query_string; + } + + location ~ \.php$ { + try_files $uri =404; + fastcgi_pass app:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } + + location ~ /\.ht { + deny all; + } +} diff --git a/setup_gymie_docker.sh b/setup_gymie_docker.sh new file mode 100755 index 00000000..80b20c19 --- /dev/null +++ b/setup_gymie_docker.sh @@ -0,0 +1,100 @@ +#! /bin/bash + +# Define environment variables +APP_ENV=local +APP_DEBUG=true +DB_CONNECTION=mysql +DB_HOST=mysql +DB_PORT=3306 +DB_DATABASE=gymie +DB_USERNAME=gymie +DB_PASSWORD=password + +# Define project path +PROJECT_PATH=$(pwd) + +# Set permissions for the local project directory +echo "Setting permissions for the project directory..." +sudo chmod -R 777 $PROJECT_PATH + +# Stop and remove containers, networks, volumes, and images created by `up` +echo "Stopping and removing existing containers, networks, and volumes..." +docker-compose down -v + +# Build and start Docker containers +echo "Building and starting Docker containers..." +docker-compose up --build -d + +# Wait for MySQL to be ready +echo "Waiting for MySQL to be ready..." +MYSQL_READY=false +for i in {1..30}; do + if docker-compose exec mysql mysqladmin ping -h"$DB_HOST" --silent; then + MYSQL_READY=true + break + fi + echo "MySQL is unavailable - sleeping ($i/30)" + sleep 5 +done + +if [ "$MYSQL_READY" = false ]; then + echo "MySQL failed to start." + docker-compose logs mysql + exit 1 +fi + +# Create necessary directories and set permissions +echo "Creating necessary directories and setting permissions..." +docker-compose exec app sh -c "mkdir -p /var/www/vendor && chown -R www-data:www-data /var/www/vendor" +docker-compose exec app sh -c "mkdir -p /var/www/storage && chown -R www-data:www-data /var/www/storage" +docker-compose exec app sh -c "mkdir -p /var/www/bootstrap/cache && chown -R www-data:www-data /var/www/bootstrap/cache" +docker-compose exec app sh -c "mkdir -p /var/www/html && chown -R www-data:www-data /var/www/html" + +# Install composer dependencies +echo "Installing composer dependencies..." +docker-compose exec app composer install + +# Copy .env.example to .env +echo "Copying .env.example to .env..." +cp .env.example .env + +# Update .env with database details +echo "Updating .env with database details..." +sed -i "s/DB_CONNECTION=.*/DB_CONNECTION=$DB_CONNECTION/" .env +sed -i "s/DB_HOST=.*/DB_HOST=$DB_HOST/" .env +sed -i "s/DB_PORT=.*/DB_PORT=$DB_PORT/" .env +sed -i "s/DB_DATABASE=.*/DB_DATABASE=$DB_DATABASE/" .env +sed -i "s/DB_USERNAME=.*/DB_USERNAME=$DB_USERNAME/" .env +sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=$DB_PASSWORD/" .env + +# Generate application key +echo "Generating application key..." +docker-compose exec app php artisan key:generate + +# Run migrations and seed the database +echo "Running migrations and seeding the database..." +docker-compose exec app php artisan migrate --seed + +# Set permissions for storage and cache inside container +echo "Setting permissions for storage and cache inside the container..." +docker-compose exec app sh -c "chown -R www-data:www-data storage bootstrap/cache && chmod -R ug+rwx storage bootstrap/cache" + +# Modify php-fpm listen directive +echo "Modifying PHP-FPM listen directive..." +docker-compose exec app sh -c "sed -i 's/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/' /usr/local/etc/php-fpm.d/www.conf" + +# Restart php-fpm to apply changes +echo "Restarting PHP-FPM to apply changes..." +docker-compose exec app sh -c "pkill -o -USR2 php-fpm" + +# Create test PHP file +echo "Creating test PHP file..." +docker-compose exec app sh -c "echo '' > /var/www/html/test.php" + +# Add cron job for scheduled tasks +echo "Adding cron job for scheduled tasks..." +(crontab -l 2>/dev/null; echo "* * * * * cd $PROJECT_PATH && docker-compose exec app php artisan schedule:run >> /dev/null 2>&1") | crontab - + +echo "Laravel Gymie setup complete. Use the following credentials to log in:" +echo "Email: admin@gymie.in" +echo "Password: password" From 31a38962fee392f05edf8868b0abe5ccf28fb083 Mon Sep 17 00:00:00 2001 From: Higor <54960146+Higor-Matos@users.noreply.github.com> Date: Sun, 14 Jul 2024 14:22:00 -0300 Subject: [PATCH 2/2] Update README.md - DockerConfigs --- README.md | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/README.md b/README.md index 9394bf5b..3b265c82 100755 --- a/README.md +++ b/README.md @@ -65,6 +65,103 @@ email: admin@gymie.in password: password ``` +### Docker Method + +1. Clone to your server root + ```sh + git clone -b master git@github.com:lubusIN/laravel-gymie.git + ``` + > For faster updates and bleeding edge features, or if you want to help test the next version, use the `develop` branch instead of the `master` branch. + +2. Ensure Docker and Docker Compose are installed on your system. + +3. Build and start Docker containers: + ```sh + docker-compose up --build -d + ``` + +4. Wait for MySQL to be ready (can take a few moments). Ensure MySQL is ready before proceeding. + +5. Create necessary directories and set permissions: + ```sh + docker-compose exec app sh -c "mkdir -p /var/www/vendor && chown -R www-data:www-data /var/www/vendor" + docker-compose exec app sh -c "mkdir -p /var/www/storage && chown -R www-data:www-data /var/www/storage" + docker-compose exec app sh -c "mkdir -p /var/www/bootstrap/cache && chown -R www-data:www-data /var/www/bootstrap/cache" + docker-compose exec app sh -c "mkdir -p /var/www/html && chown -R www-data:www-data /var/www/html" + ``` + +6. Install composer dependencies: + ```sh + docker-compose exec app composer install + ``` + +7. Copy `.env.example` to `.env` and update it: + ```sh + cp .env.example .env + sed -i "s/DB_CONNECTION=.*/DB_CONNECTION=mysql/" .env + sed -i "s/DB_HOST=.*/DB_HOST=mysql/" .env + sed -i "s/DB_PORT=.*/DB_PORT=3306/" .env + sed -i "s/DB_DATABASE=.*/DB_DATABASE=gymie/" .env + sed -i "s/DB_USERNAME=.*/DB_USERNAME=gymie/" .env + sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=password/" .env + ``` + +8. Generate application key: + ```sh + docker-compose exec app php artisan key:generate + ``` + +9. Run migrations and seed the database: + ```sh + docker-compose exec app php artisan migrate --seed + ``` + +10. Set permissions for storage and cache inside container: + ```sh + docker-compose exec app sh -c "chown -R www-data:www-data storage bootstrap/cache && chmod -R ug+rwx storage bootstrap/cache" + ``` + +11. Modify php-fpm listen directive: + ```sh + docker-compose exec app sh -c "sed -i 's/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/' /usr/local/etc/php-fpm.d/www.conf" + ``` + +12. Restart php-fpm to apply changes: + ```sh + docker-compose exec app sh -c "pkill -o -USR2 php-fpm" + ``` + +13. Create test PHP file: + ```sh + docker-compose exec app sh -c "echo '' > /var/www/html/test.php" + ``` + +14. Add cron job for scheduled tasks: + ```sh + (crontab -l 2>/dev/null; echo "* * * * * cd $(pwd) && docker-compose exec app php artisan schedule:run >> /dev/null 2>&1") | crontab - + ``` + +15. All set! Use the following credentials to log in: + ``` + email: admin@gymie.in + password: password + ``` + +#### Particularities for Docker + +- We are using Nginx as a reverse proxy to access the PHP application due to compatibility issues with the PHP version. + +#### Using the setup script + +If you prefer to use a script to automate the setup process, you can use `setup_gymie_docker.sh` located in the project directory: + +```sh +./setup_gymie_docker.sh +``` +#### Particularities for Docker + +- We are using Nginx as a reverse proxy to access the PHP application due to compatibility issues with the PHP version. + ## Troubleshooting **APP_KEY not getting added to .env**