├── .gitignore ├── Makefile ├── README.md ├── docker-compose.yml.dist └── docker ├── Dockerfile ├── default.conf └── xdebug.ini /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .vscode 3 | docker-compose.yml -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | UID = $(shell id -u) 4 | DOCKER_BE = symfony-app 5 | 6 | help: ## Show this help message 7 | @echo 'usage: make [target]' 8 | @echo 9 | @echo 'targets:' 10 | @egrep '^(.+)\:\ ##\ (.+)' ${MAKEFILE_LIST} | while read -r l; do printf "\033[1;32m$$(echo $$l | cut -f 1 -d':')\033[00m:$$(echo $$l | cut -f 2- -d'#')\n"; done | column -t -c 2 -s ':#' 11 | 12 | start: ## Start the containers 13 | cp -n docker-compose.yml.dist docker-compose.yml || true 14 | U_ID=${UID} docker-compose up -d 15 | 16 | stop: ## Stop the containers 17 | U_ID=${UID} docker-compose stop 18 | 19 | restart: ## Restart the containers 20 | $(MAKE) stop && $(MAKE) start 21 | 22 | build: ## Rebuilds all the containers 23 | cp -n docker-compose.yml.dist docker-compose.yml || true 24 | U_ID=${UID} docker-compose build 25 | 26 | prepare: ## Runs backend commands 27 | $(MAKE) composer-install 28 | 29 | run: ## starts the Symfony development server in detached mode 30 | U_ID=${UID} docker exec -it --user ${UID} ${DOCKER_BE} symfony serve -d 31 | 32 | logs: ## Show Symfony logs in real time 33 | U_ID=${UID} docker exec -it --user ${UID} ${DOCKER_BE} symfony server:log 34 | 35 | # Backend commands 36 | composer-install: ## Installs composer dependencies 37 | U_ID=${UID} docker exec --user ${UID} ${DOCKER_BE} composer install --no-interaction 38 | # End backend commands 39 | 40 | ssh: ## bash into the be container 41 | U_ID=${UID} docker exec -it --user ${UID} ${DOCKER_BE} bash 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Symfony Base Repository 2 | 3 | This repository contains the basic configuration to run Symfony applications with MySQL database 4 | 5 | ## Content 6 | - PHP-APACHE container running version 8.2 7 | - MySQL container running version 8.2.0 8 | 9 | ## Instructions 10 | - `make build` to build the containers 11 | - `make start` to start the containers 12 | - `make stop` to stop the containers 13 | - `make restart` to restart the containers 14 | - `make prepare` to install dependencies with composer (once the project has been created) 15 | - `make logs` to see application logs 16 | - `make ssh` to SSH into the application container 17 | 18 | ## Create and Run the application 19 | > [!TIP] 20 | > Replace all the occurrences of `symfony-app` in the project with a more meaningful name. 21 | > You can use your IDE's find and replace option to complete this task. 22 | 23 | 24 | 1. Build and start the containers: 25 | ```shell 26 | make start 27 | ``` 28 | 2. SSH into the container: 29 | ```shell 30 | make ssh 31 | ``` 32 | 3. Create a Symfony project using the CLI: 33 | ```shell 34 | symfony new --no-git --dir project 35 | ``` 36 | 4. Move all the content in the `project` folder to the root of the repository: 37 | ```shell 38 | mv project/{*,.*} . && rm -r project/ 39 | ``` 40 | 5. Add the content of `.gitignore` file to the root one, it should look like this: 41 | ```text 42 | .idea 43 | .vscode 44 | docker-compose.yml 45 | 46 | ###> symfony/framework-bundle ### 47 | /.env.local 48 | /.env.local.php 49 | /.env.*.local 50 | /config/secrets/prod/prod.decrypt.private.php 51 | /public/bundles/ 52 | /var/ 53 | /vendor/ 54 | ###< symfony/framework-bundle ### 55 | ``` 56 | 6. Once you have installed you Symfony application go to http://localhost:1000 57 | -------------------------------------------------------------------------------- /docker-compose.yml.dist: -------------------------------------------------------------------------------- 1 | version: '3.5' 2 | 3 | services: 4 | symfony-app: 5 | container_name: symfony-app 6 | build: 7 | context: ./docker 8 | args: 9 | UID: $U_ID 10 | volumes: 11 | - ./:/var/www/html 12 | ###> XDEBUG 3 ### 13 | # Use your client IP here 14 | # Linux: run "ip a | grep docker0" 15 | # Windows (with WSL2) and Mac: host.docker.internal 16 | environment: 17 | XDEBUG_CLIENT_HOST: 172.17.0.1 18 | XDEBUG_CLIENT_PORT: 9003 19 | PHP_IDE_CONFIG: serverName=symfony-server 20 | ports: 21 | - '1000:80' 22 | networks: 23 | - symfony-app-network 24 | depends_on: 25 | - symfony-app-mysql 26 | 27 | symfony-app-mysql: 28 | container_name: symfony-app-mysql 29 | image: mysql:8.2.0 30 | ports: 31 | - '3336:3306' 32 | environment: 33 | MYSQL_DATABASE: symfony-app 34 | MYSQL_ROOT_PASSWORD: root 35 | volumes: 36 | - symfony-app-mysql-data:/var/lib/mysql 37 | networks: 38 | - symfony-app-network 39 | command: [ 'mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci' ] 40 | 41 | networks: 42 | symfony-app-network: 43 | name: symfony-app-network 44 | 45 | volumes: 46 | symfony-app-mysql-data: 47 | -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:8.3-apache 2 | 3 | ARG UID 4 | 5 | # Create user with same permissions as host and some useful stuff 6 | RUN adduser -u ${UID} --disabled-password --gecos "" appuser 7 | RUN mkdir /home/appuser/.ssh 8 | RUN chown -R appuser:appuser /home/appuser/ 9 | RUN echo "StrictHostKeyChecking no" >> /home/appuser/.ssh/config 10 | RUN echo "alias sf=/var/www/html/bin/console" >> /home/appuser/.bashrc 11 | 12 | # Install packages and PHP extensions 13 | RUN apt update \ 14 | # common libraries and extensions 15 | && apt install -y git acl openssl openssh-client wget zip \ 16 | && apt install -y libpng-dev zlib1g-dev libzip-dev libxml2-dev libicu-dev \ 17 | && docker-php-ext-install intl pdo zip \ 18 | # for MySQL 19 | && docker-php-ext-install pdo_mysql \ 20 | # XDEBUG and APCu 21 | && pecl install xdebug apcu \ 22 | # enable Docker extensions 23 | && docker-php-ext-enable --ini-name 05-opcache.ini opcache xdebug apcu 24 | 25 | # Install and update composer 26 | RUN curl https://getcomposer.org/composer.phar -o /usr/bin/composer && chmod +x /usr/bin/composer 27 | RUN composer self-update 28 | 29 | ## Install Symfony binary 30 | RUN curl -1sLf 'https://dl.cloudsmith.io/public/symfony/stable/setup.deb.sh' | bash 31 | RUN apt install symfony-cli 32 | 33 | RUN mkdir -p /var/www/html 34 | 35 | # Config XDEBUG 36 | COPY xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini 37 | 38 | # Update Apache config 39 | COPY ./default.conf /etc/apache2/sites-available/default.conf 40 | RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf \ 41 | && a2enmod rewrite \ 42 | && a2dissite 000-default \ 43 | && a2ensite default \ 44 | && service apache2 restart 45 | 46 | # Modify upload file size 47 | RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" 48 | 49 | WORKDIR /var/www/html 50 | -------------------------------------------------------------------------------- /docker/default.conf: -------------------------------------------------------------------------------- 1 | 2 | ServerName localhost 3 | ServerAlias localhost 4 | 5 | DocumentRoot /var/www/html/public 6 | DirectoryIndex /index.php 7 | 8 | 9 | AllowOverride None 10 | Order Allow,Deny 11 | Allow from All 12 | 13 | FallbackResource /index.php 14 | DirectoryIndex index.php 15 | 16 | 17 | Options -MultiViews 18 | 19 | 20 | 21 | RewriteEngine On 22 | RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$ 23 | RewriteRule .* - [E=BASE:%1] 24 | RewriteCond %{HTTP:Authorization} .+ 25 | RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0] 26 | RewriteCond %{ENV:REDIRECT_STATUS} ="" 27 | RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L] 28 | RewriteCond %{REQUEST_FILENAME} !-f 29 | RewriteRule ^ %{ENV:BASE}/index.php [L] 30 | 31 | 32 | 33 | 34 | RedirectMatch 307 ^/$ /index.php/ 35 | 36 | 37 | 38 | 39 | 40 | DirectoryIndex disabled 41 | FallbackResource disabled 42 | 43 | ErrorLog /var/log/apache2/symfony-app_error.log 44 | CustomLog /var/log/apache2/symfony-app_access.log combined 45 | 46 | -------------------------------------------------------------------------------- /docker/xdebug.ini: -------------------------------------------------------------------------------- 1 | xdebug.mode=debug 2 | xdebug.start_with_request=yes 3 | xdebug.client_host=${XDEBUG_CLIENT_HOST} 4 | xdebug.client_port=${XDEBUG_CLIENT_PORT} 5 | xdebug.log_level=0 --------------------------------------------------------------------------------