From dc46c9e48f421423dd97a3edd7ebf7b514dd92d3 Mon Sep 17 00:00:00 2001 From: Yannick7777 <87943421+Yannick7777@users.noreply.github.com> Date: Wed, 2 Apr 2025 15:21:48 +0200 Subject: [PATCH] Initial commit --- .editorconfig | 18 ++++++++++ .gitattributes | 11 ++++++ .gitignore | 11 ++++++ README.md | 41 ++++++++++++++++++++++ art | 10 ++++++ compose.yaml | 48 ++++++++++++++++++++++++++ docker_build/.dockerignore | 1 + docker_build/Dockerfile | 30 +++++++++++++++++ docker_build/README.md | 1 + docker_build/add_pkgs | 2 ++ docker_build/enable_ext | 2 ++ docker_build/install_ext | 2 ++ env_docker | 69 ++++++++++++++++++++++++++++++++++++++ exec | 10 ++++++ setup | 62 ++++++++++++++++++++++++++++++++++ start | 10 ++++++ stop | 10 ++++++ switch-branch-migrate | 22 ++++++++++++ 18 files changed, 360 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 README.md create mode 100755 art create mode 100644 compose.yaml create mode 100644 docker_build/.dockerignore create mode 100644 docker_build/Dockerfile create mode 100644 docker_build/README.md create mode 100644 docker_build/add_pkgs create mode 100644 docker_build/enable_ext create mode 100644 docker_build/install_ext create mode 100644 env_docker create mode 100755 exec create mode 100755 setup create mode 100755 start create mode 100755 stop create mode 100755 switch-branch-migrate diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8f0de65 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,18 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.{yml,yaml}] +indent_size = 2 + +[docker-compose.yml] +indent_size = 4 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..fcb21d3 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +* text=auto eol=lf + +*.blade.php diff=html +*.css diff=css +*.html diff=html +*.md diff=markdown +*.php diff=php + +/.github export-ignore +CHANGELOG.md export-ignore +.styleci.yml export-ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ec4e4d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +.env +/dbdata +/.vscode +/.fleet +/.idea +/.nova +/.zed +.idea + + +laravel/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..440d256 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +## Idea +This Project aims to simplify setting up a laravel project in a local development environment for new and experienced developers. + +It stores all data (excluding the built docker image) in a single directory to make it easy for a developer to remove existing projects again and keep their system clutter-free. + +The only **dependencies** for this project are **git** and **docker(-compose)**. + +Tested shells are: `bash`, `zsh` + +## Content +This project ships with a custom docker container based on **php:8.2-apache** with the php extensions opcache, pdo_mysql, intl, and zip. + +It also includes **mysql:8** and **phpmyadmin:latest**. + +## Other useful info +- To reset the Database, stop the Docker containers, go to the project root and execute `rm -rf dbdata`. Then execute `./setup`. +- The apps and PMAs port can be changed by editing the `SETUP_APP_PORT` and `SETUP_PMA_PORT` variables in `.env`. This resets after running `./setup`. + +## Setup +### Windows with VSCode +- If WSL isn't installed yet, install it by executing `wsl --install` in your preferred shell. +- Open WSL from startmenu or your preferred shell by executing `wsl`. +- Open VSCode by executing `code`. +- Wait until the VSCode server is installed. +- VSCode should open up. Press F1 and use `WSL: Connect to WSL` command. +- Press F1 and use `Git: Clone` to clone this repo. +- `cd` into this repo (the laravel-setup-script directory). +- Press F1 and use `Git: Clone` again. Make sure to clone it into this repo's folder and to NOT open the cloned repository and instead click `cancel`. +- Rename your project to `laravel` by either using the `mv` command in the VSCode terminal or by right-clicking on the directory in VSCode and clicking `rename`. +- Start Docker Desktop on Windows. +- Type `./setup` into the VSCode terminal. +- Type `./start` into the VSCode terminal. +- Done! +#### Tips & Tricks +- If you encounter any Docker-related issues, it might help to close Docker Engine and stop WSL by executing `wsl -t ubuntu` in your preferred shell. After that, start both again. +- If you want to commit to your laravel repo with the VSCode F1 commands, make sure to switch to the folder your repo is in first. This can be done via `File` => `Open Folder...` + +### Linux +- If you're a (sigma) Linux user, follow the Windows instructions starting from when the repo should be cloned. +- Make sure you have access to the `docker` and `docker compose` commands. +- Make sure you started the Docker Daemon; that's `rc-service docker start` for Open-RC based distros and `systemctl start docker` for Systemd based distros. If you're not sure what type of distro you're running, you probably run a Systemd based one. diff --git a/art b/art new file mode 100755 index 0000000..16efe6b --- /dev/null +++ b/art @@ -0,0 +1,10 @@ +#!/bin/bash + +if [[ "$(basename $(pwd))" == "laravel" ]]; then + cd .. + ./art "$@" + cd laravel + exit +fi + +docker compose exec -it app php artisan $@ diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..23f30b4 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,48 @@ +services: + # Application & web server + app: + build: + context: docker_build + working_dir: /var/www + volumes: + - ./laravel/:/var/www + depends_on: + - database + ports: + - ${SETUP_APP_PORT:-80}:80 + user: ${dockerUID}:${dockerGID} + # Database + database: + image: mysql:8 + volumes: + - ./dbdata:/var/lib/mysql + environment: + MYSQL_DATABASE: ${DB_DATABASE} + MYSQL_ROOT_PASSWORD: ${DB_PASSWORD} + MYSQL_PASSWORD: ${DB_PASSWORD} + MYSQL_USER: ${DB_USERNAME} + MYSQL_PORT: ${DB_PORT} + ports: + - "33061:3306" + user: ${dockerUID}:${dockerGID} + # Database management + pma: + image: phpmyadmin:latest + environment: + - PMA_ARBITRARY=1 + - PMA_HOST=${DB_HOST} + - PMA_USER=${DB_USERNAME} + - PMA_PASSWORD=${DB_PASSWORD} + - PMA_PORT=${DB_PORT} + depends_on: + - database + ports: + - ${SETUP_PMA_PORT:-8888}:80 + # Mailing Server +# mailhog: +# image: mailhog/mailhog +# logging: +# driver: 'none' +# ports: +# - 1025:1025 +# - 8025:8025 diff --git a/docker_build/.dockerignore b/docker_build/.dockerignore new file mode 100644 index 0000000..9414382 --- /dev/null +++ b/docker_build/.dockerignore @@ -0,0 +1 @@ +Dockerfile diff --git a/docker_build/Dockerfile b/docker_build/Dockerfile new file mode 100644 index 0000000..55bf5f4 --- /dev/null +++ b/docker_build/Dockerfile @@ -0,0 +1,30 @@ +FROM composer AS composer + + +FROM php:8.2-apache + +COPY --from=composer /usr/bin/composer /usr/bin/composer +COPY . . + +RUN apt-get update \ + && apt-get install -y \ + libicu-dev libzip-dev \ + && docker-php-ext-enable opcache \ + && docker-php-ext-install pdo_mysql intl zip -j$(nproc) \ + && apt-get autoclean -y \ + && rm -rf /var/lib/apt/lists/* + +RUN apt-get update \ + && apt-get install -y "$(cat add_pkgs | head -n 1)" || true \ + && docker-php-ext-enable "$(cat enable_ext | head -n 1)" || true \ + && docker-php-ext-enable "$(cat install_ext | head -n 1)" || true \ + && apt-get autoclean -y \ + && rm -rf /var/lib/apt/lists/* \ + +# Update apache config to point to the *public* directory +ENV APACHE_DOCUMENT_ROOT=/var/www/public +RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf +RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf + +# Enable headers module +RUN a2enmod rewrite headers diff --git a/docker_build/README.md b/docker_build/README.md new file mode 100644 index 0000000..073f3ee --- /dev/null +++ b/docker_build/README.md @@ -0,0 +1 @@ +If you edit the files in this directory to add contents to the container remember that the buildprocess will not throw an error if you entered an invalid packagename / PHP extension. diff --git a/docker_build/add_pkgs b/docker_build/add_pkgs new file mode 100644 index 0000000..e3bbd10 --- /dev/null +++ b/docker_build/add_pkgs @@ -0,0 +1,2 @@ +curl +#list additional packages to be installed inside the container on the first line only diff --git a/docker_build/enable_ext b/docker_build/enable_ext new file mode 100644 index 0000000..f7e7525 --- /dev/null +++ b/docker_build/enable_ext @@ -0,0 +1,2 @@ + +#list additional PHP extensions to be enabled inside the container on the first line only diff --git a/docker_build/install_ext b/docker_build/install_ext new file mode 100644 index 0000000..096d487 --- /dev/null +++ b/docker_build/install_ext @@ -0,0 +1,2 @@ + +#list additional PHP extensions to be installed inside the container on the first line only diff --git a/env_docker b/env_docker new file mode 100644 index 0000000..63376e4 --- /dev/null +++ b/env_docker @@ -0,0 +1,69 @@ +APP_NAME=Laravel +APP_ENV=local +APP_KEY= +APP_DEBUG=true +APP_TIMEZONE=UTC +APP_URL=http://localhost + +APP_LOCALE=en +APP_FALLBACK_LOCALE=en +APP_FAKER_LOCALE=en_US + +APP_MAINTENANCE_DRIVER=file +# APP_MAINTENANCE_STORE=database + +PHP_CLI_SERVER_WORKERS=4 + +BCRYPT_ROUNDS=12 + +LOG_CHANNEL=stack +LOG_STACK=single +LOG_DEPRECATIONS_CHANNEL=null +LOG_LEVEL=debug + +DB_CONNECTION=mysql +DB_HOST=database +DB_PORT=3306 +DB_DATABASE=laravel +DB_USERNAME=laravel-user +DB_PASSWORD=laravel-password + +SESSION_DRIVER=database +SESSION_LIFETIME=120 +SESSION_ENCRYPT=false +SESSION_PATH=/ +SESSION_DOMAIN=null + +BROADCAST_CONNECTION=log +FILESYSTEM_DISK=local +QUEUE_CONNECTION=database + +CACHE_STORE=database +CACHE_PREFIX= + +MEMCACHED_HOST=127.0.0.1 + +REDIS_CLIENT=phpredis +REDIS_HOST=127.0.0.1 +REDIS_PASSWORD=null +REDIS_PORT=6379 + +MAIL_MAILER=log +MAIL_HOST=mailhost +MAIL_PORT=1025 +MAIL_USERNAME=null +MAIL_PASSWORD=null +MAIL_ENCRYPTION=null +MAIL_FROM_ADDRESS="hello@example.com" +MAIL_FROM_NAME="${APP_NAME}" + +AWS_ACCESS_KEY_ID= +AWS_SECRET_ACCESS_KEY= +AWS_DEFAULT_REGION=us-east-1 +AWS_BUCKET= +AWS_USE_PATH_STYLE_ENDPOINT=false + +VITE_APP_NAME="${APP_NAME}" + +SETUP_APP_PORT=80 +SETUP_PMA_PORT=8888 diff --git a/exec b/exec new file mode 100755 index 0000000..4aff8e8 --- /dev/null +++ b/exec @@ -0,0 +1,10 @@ +#!/bin/bash + +if [[ "$(basename $(pwd))" == "laravel" ]]; then + cd .. + ./exec "$@" + cd laravel + exit +fi + +docker compose exec -it app $@ diff --git a/setup b/setup new file mode 100755 index 0000000..5d66912 --- /dev/null +++ b/setup @@ -0,0 +1,62 @@ +#!/bin/bash + +if [[ "$(basename $(pwd))" == "laravel" ]]; then + cd .. + ./setup + cd laravel + exit +fi + +docker compose down + +for executable in start stop setup exec art switch-branch-migrate; do + ln -sf ../$executable laravel/$executable +done + +dockerUID=$(id -u) +dockerGID=$(id -g) + +mkdir dbdata &> /dev/null + +echo "Container build in progress.." +composer_docker_hash=$(docker build -q docker_build) +docker run --user $dockerUID:$dockerGID --rm -v ./laravel:/var/www/html -it $composer_docker_hash composer install +cp env_docker laravel/.env +cp env_docker .env +echo "dockerUID=$dockerUID" >> .env +echo "dockerGID=$dockerGID" >> .env + +if [ -z "$(ls -A dbdata)" ]; then + echo "Initialising database.." + docker compose up database -d &> /dev/null + while ! docker compose logs database | grep -q 'mysqld: ready for connections'; do + sleep 0.5 + done + docker compose down database &> /dev/null +fi + +docker compose up database -d &> /dev/null +while ! docker compose logs database | grep -q 'mysqld: ready for connections'; do + sleep 0.5 +done +docker compose down database &> /dev/null + +docker compose up -d + +echo "Waiting for app to start up..." +while ! docker compose logs app | grep -q 'Command line: '; do + sleep 0.5 +done + +docker compose exec app php artisan key:generate +docker compose exec app php artisan config:cache + +echo "Waiting for database to start up..." +while ! docker compose logs database | grep -q 'mysqld: ready for connections'; do + echo "not ready.." + sleep 0.5 +done + +docker compose exec app php artisan migrate:fresh --seed +docker compose exec app php artisan test +docker compose down diff --git a/start b/start new file mode 100755 index 0000000..d0968ae --- /dev/null +++ b/start @@ -0,0 +1,10 @@ +#!/bin/bash + +if [[ "$(basename $(pwd))" == "laravel" ]]; then + cd .. + ./start + cd laravel + exit +fi + +docker compose up diff --git a/stop b/stop new file mode 100755 index 0000000..e2727ca --- /dev/null +++ b/stop @@ -0,0 +1,10 @@ +#!/bin/bash + +if [[ "$(basename $(pwd))" == "laravel" ]]; then + cd .. + ./stop + cd laravel + exit +fi + +docker compose down diff --git a/switch-branch-migrate b/switch-branch-migrate new file mode 100755 index 0000000..93ece22 --- /dev/null +++ b/switch-branch-migrate @@ -0,0 +1,22 @@ +#!/bin/bash + +if [[ "$(basename $(pwd))" == "laravel" ]]; then + cd .. + ./switch-branch-migrate "$@" + cd laravel + exit +fi + +branch_name=$1 + +cd laravel + +if git show-ref --verify --quiet refs/heads/"$branch_name"; then + git checkout "$branch_name" +else + git checkout -b "$branch_name" +fi + +cd .. + +docker compose exec app php artisan migrate:fresh --seed