├── .docker ├── docker-compose.yml ├── nginx │ ├── Dockerfile │ ├── certs │ │ └── template.csr.conf │ └── default.conf └── php │ ├── Dockerfile │ ├── overrides.ini │ └── xdebug.ini ├── .editorconfig ├── .env.example ├── .gitignore ├── .vscode └── launch.json ├── LICENSE ├── Makefile ├── README.md ├── composer.json └── public └── index.php /.docker/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.9" 2 | name: default 3 | services: 4 | nginx: 5 | container_name: nginx_${PROJECT_NAME} 6 | build: ./nginx 7 | ports: 8 | - "80:80" 9 | - "443:443" 10 | volumes: 11 | - ./nginx/default.conf:/etc/nginx/conf.d/default.conf 12 | - ../:/var/www 13 | - ../logs/nginx:/var/log/nginx 14 | links: 15 | - php 16 | - db 17 | 18 | php: 19 | container_name: php_${PROJECT_NAME} 20 | build: ./php 21 | working_dir: /var/www 22 | volumes: 23 | - ../:/var/www 24 | - ../logs/php:/var/log 25 | links: 26 | - db 27 | 28 | db: 29 | container_name: mysql_${PROJECT_NAME} 30 | image: mysql:latest 31 | command: --default-authentication-plugin=mysql_native_password 32 | environment: 33 | MYSQL_ROOT_USER: ${DB_ROOT_USER} 34 | MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD} 35 | MYSQL_DATABASE: ${DB_NAME} 36 | MYSQL_USER: ${DB_USER} 37 | MYSQL_PASSWORD: ${DB_PASSWORD} 38 | volumes: 39 | - db_default:/var/lib/mysql:rw 40 | ports: 41 | - 3306:3306 42 | 43 | volumes: 44 | db_default: 45 | -------------------------------------------------------------------------------- /.docker/nginx/Dockerfile: -------------------------------------------------------------------------------- 1 | # Image and version 2 | FROM nginx:1.25.2-alpine 3 | 4 | # Install dependencies 5 | RUN apk --no-cache add openssl 6 | 7 | # Create dir ssl/ 8 | RUN mkdir -p /etc/nginx/ssl 9 | 10 | # Copy template.csr.conf 11 | COPY certs/template.csr.conf /etc/nginx/ssl 12 | 13 | # Generating the certificate's private key - localhost.key 14 | RUN openssl genpkey -algorithm RSA -out /etc/nginx/ssl/localhost.key 15 | 16 | # Generating the Certificate Signing Request - localhost.csr 17 | RUN openssl req -new -key /etc/nginx/ssl/localhost.key -out /etc/nginx/ssl/localhost.csr -subj "/C=BR/ST=State/L=City/O=Organization/CN=localhost" -config /etc/nginx/ssl/template.csr.conf 18 | 19 | # Generating the Self-Signed Certificate - localhost.crt 20 | RUN openssl x509 -req -days 365 -in /etc/nginx/ssl/localhost.csr -signkey /etc/nginx/ssl/localhost.key -out /etc/nginx/ssl/localhost.crt 21 | -------------------------------------------------------------------------------- /.docker/nginx/certs/template.csr.conf: -------------------------------------------------------------------------------- 1 | [req] 2 | default_bits = 2048 3 | default_keyfile = localhost.key 4 | distinguished_name = req_distinguished_name 5 | req_extensions = req_ext 6 | x509_extensions = v3_ca 7 | req_extensions = v3_req 8 | 9 | [req_distinguished_name] 10 | countryName = Country Name (2 letter code) 11 | countryName_default = BR 12 | stateOrProvinceName = State or Province Name (full name) 13 | stateOrProvinceName_default = Sao Paulo 14 | localityName = Locality Name (eg, city) 15 | localityName_default = Sao Paulo 16 | organizationName = Organization Name (eg, company) 17 | organizationName_default = Localhost Ltda 18 | organizationalUnitName = organizationalunit 19 | organizationalUnitName_default = Development 20 | commonName = Common Name (e.g. server FQDN or YOUR name) 21 | commonName_default = localhost 22 | commonName_max = 64 23 | 24 | [req_ext] 25 | subjectAltName = @alt_names 26 | 27 | [v3_ca] 28 | subjectAltName = @alt_names 29 | 30 | [v3_req] 31 | basicConstraints = CA:FALSE 32 | keyUsage = nonRepudiation, digitalSignature, keyEncipherment 33 | subjectAltName = @alt_names 34 | 35 | [alt_names] 36 | DNS.1 = 127.0.0.1 37 | DNS.2 = localhost 38 | -------------------------------------------------------------------------------- /.docker/nginx/default.conf: -------------------------------------------------------------------------------- 1 | server { 2 | # Port 80 3 | listen 80; 4 | # listen for ipv6 5 | listen [::]:80 ipv6only=on; 6 | # disable any limits to avoid HTTP 413 for large image uploads 7 | client_max_body_size 0; 8 | # Active HTTP2 9 | http2 on; 10 | # Server name 11 | server_name default.localhost www.default.localhost; 12 | # Redirect for https 13 | location / { 14 | return 301 https://$host$request_uri; 15 | } 16 | } 17 | 18 | server { 19 | # Port 443 20 | listen 443 ssl; 21 | # listen for ipv6 22 | listen [::]:443 ssl ipv6only=on; 23 | # Active HTTP2 24 | http2 on; 25 | # Server name 26 | server_name default.localhost www.default.localhost; 27 | # disable any limits to avoid HTTP 413 for large image uploads 28 | client_max_body_size 0; 29 | # log to stdout for docker 30 | error_log /var/log/nginx/error.default.log; 31 | access_log /var/log/nginx/access.default.log; 32 | # folder public 33 | root /var/www/public; 34 | # type of index file 35 | index index.html index.htm index.php; 36 | # charset 37 | charset utf-8; 38 | 39 | # SSL Configuration 40 | ssl_certificate /etc/nginx/ssl/localhost.crt; 41 | ssl_certificate_key /etc/nginx/ssl/localhost.key; 42 | ssl_session_timeout 5m; 43 | ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; 44 | ssl_ciphers HIGH:!aNULL:!MD5; 45 | ssl_prefer_server_ciphers on; 46 | 47 | location / { 48 | # try to get file directly, try it as a directory or fall back to modx 49 | try_files $uri $uri/ @mod_rewrite; 50 | } 51 | 52 | # block access to sensitive information about git 53 | location /.git { 54 | deny all; 55 | return 403; 56 | } 57 | 58 | location @mod_rewrite { 59 | # including ? in second rewrite argument causes nginx to drop GET params, so append them again 60 | rewrite ^/(.*)$ /index.php?route=/$1; 61 | } 62 | 63 | # You may need this to prevent return 404 recursion. 64 | location = /404.html { 65 | internal; 66 | } 67 | 68 | # Pass all .php files onto a php-fpm/php-fcgi server. 69 | location ~ \.php$ { 70 | try_files $uri =404; 71 | fastcgi_split_path_info ^(.+\.php)(/.+)$; 72 | fastcgi_pass php_default:9000; 73 | fastcgi_read_timeout 6000; 74 | fastcgi_index index.php; 75 | include fastcgi_params; 76 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 77 | fastcgi_param PATH_INFO $fastcgi_path_info; 78 | } 79 | 80 | # Cache static files 81 | location ~* \.(jpg|jpeg|gif|png|css|js|ico|webp|tiff|ttf|svg)$ { 82 | expires 5d; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /.docker/php/Dockerfile: -------------------------------------------------------------------------------- 1 | # Image and version 2 | FROM php:8.3-fpm 3 | 4 | # Install dependencies 5 | RUN apt update && \ 6 | apt install --no-install-recommends -y \ 7 | zlib1g-dev \ 8 | libpng-dev \ 9 | libzip-dev \ 10 | unzip 11 | 12 | # Call PHP images script `docker-php-ext-install` and install language extensions 13 | RUN docker-php-ext-install \ 14 | mysqli \ 15 | pdo \ 16 | pdo_mysql \ 17 | zip 18 | 19 | # Enable PHP extensions with `docker-php-ext-enable` 20 | RUN docker-php-ext-enable \ 21 | pdo_mysql 22 | 23 | # copy the Composer PHAR from the Composer image into the PHP image 24 | COPY --from=composer /usr/bin/composer /usr/bin/composer 25 | 26 | # Install xdebug 27 | RUN pecl install xdebug && docker-php-ext-enable xdebug 28 | COPY xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini 29 | 30 | # Override the default PHP configuration with custom php.ini 31 | COPY overrides.ini /usr/local/etc/php/conf.d/overrides.ini 32 | 33 | # Clean the image 34 | RUN apt -y autoremove && \ 35 | apt clean && \ 36 | rm -rf /var/lib/apt/lists/* /tmp/* /var/temp/* 37 | -------------------------------------------------------------------------------- /.docker/php/overrides.ini: -------------------------------------------------------------------------------- 1 | upload_max_filesize = 100M 2 | post_max_size = 108M 3 | -------------------------------------------------------------------------------- /.docker/php/xdebug.ini: -------------------------------------------------------------------------------- 1 | [xdebug] 2 | xdebug.mode=debug 3 | xdebug.start_with_request=yes 4 | xdebug.client_host=host.docker.internal 5 | xdebug.log_level=0 6 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.yml] 12 | indent_style = space 13 | indent_size = 2 14 | -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | # Project Config 2 | APP_NAME=Project 3 | APP_ENV=local 4 | 5 | # Docker Config 6 | COMPOSE_FILE=.docker/docker-compose.yml 7 | PROJECT_NAME=default 8 | 9 | # Database Config 10 | DB_ROOT_USER=root 11 | DB_ROOT_PASSWORD=root 12 | DB_USER=${PROJECT_NAME} 13 | DB_PASSWORD=root 14 | DB_NAME=${PROJECT_NAME} 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | vendor 3 | logs 4 | composer.lock 5 | .DS_Store 6 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "Listen for XDebug", 6 | "type": "php", 7 | "request": "launch", 8 | "port": 9003, 9 | "log": true, 10 | "pathMappings": { 11 | "/var/www/": "${workspaceFolder}" 12 | }, 13 | "xdebugSettings": { 14 | "max_children": 128, 15 | "max_data": -1, 16 | "show_hidden": 1, 17 | "max_depth": 3 18 | } 19 | } 20 | ] 21 | } 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Walter Nascimento 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # File variables 2 | FILE1 = .env 3 | FILE2 = .env.example 4 | 5 | # Check if .env file exists 6 | ifeq (,$(wildcard $(FILE1))) 7 | $(shell cp $(FILE2) $(FILE1)) 8 | endif 9 | 10 | # Load environment variables 11 | include .env 12 | export $(shell sed 's/=.*//' .env) 13 | 14 | # Color Config 15 | NOCOLOR=\033[0m 16 | GREEN=\033[0;32m 17 | BGREEN=\033[1;32m 18 | YELLOW=\033[0;33m 19 | CYAN=\033[0;36m 20 | 21 | # Default action 22 | .DEFAULT_GOAL := help 23 | 24 | up: 25 | @echo "" 26 | @echo "${YELLOW}Start all container${NOCOLOR}" 27 | @echo "" 28 | docker-compose up -d 29 | 30 | @if [ ! -d vendor ]; then\ 31 | echo "";\ 32 | echo "${YELLOW}Install composer dependencies${NOCOLOR}";\ 33 | echo "";\ 34 | $(MAKE) composer;\ 35 | fi 36 | 37 | stop: 38 | @echo "" 39 | @echo "${YELLOW}Stop all container${NOCOLOR}" 40 | @echo "" 41 | docker-compose stop 42 | 43 | destroy: 44 | @echo "" 45 | @echo "${YELLOW}Destroy all container${NOCOLOR}" 46 | @echo "" 47 | docker-compose down 48 | 49 | build: 50 | @echo "" 51 | @echo "${YELLOW}Build all container${NOCOLOR}" 52 | @echo "" 53 | docker-compose up --build -d 54 | 55 | composer: 56 | docker-compose exec php composer install 57 | 58 | composer_require: 59 | docker-compose exec php composer require $(PACKAGE) 60 | 61 | bash_php: 62 | docker-compose exec php bash 63 | 64 | bash_nginx: 65 | docker-compose exec nginx bash 66 | 67 | restart: stop up 68 | 69 | help: 70 | @echo "" 71 | @echo "${NOCOLOR}Usage: ${CYAN}make [TARGET] [EXTRA_ARGUMENTS]" 72 | @echo "" 73 | @echo "${NOCOLOR}Targets:" 74 | @echo "" 75 | @echo " ${BGREEN}build${YELLOW} build the containers" 76 | @echo " ${BGREEN}up${YELLOW} Start the containers" 77 | @echo " ${BGREEN}stop${YELLOW} Stop the containers" 78 | @echo " ${BGREEN}destroy${YELLOW} Destroy the containers" 79 | @echo " ${BGREEN}composer${YELLOW} Run composer install" 80 | @echo " ${BGREEN}composer_require${YELLOW} Run composer require [PACKAGE]" 81 | @echo " ${BGREEN}bash_php${YELLOW} Open bash in php container" 82 | @echo " ${BGREEN}bash_nginx${YELLOW} Open bash in nginx container" 83 | @echo " ${BGREEN}restart${YELLOW} Restart the containers" 84 | @echo "" 85 | @echo "${NOCOLOR}Examples:" 86 | @echo "" 87 | @echo "${BGREEN}up${WHITE} ${CYAN}make up" 88 | @echo "${BGREEN}composer_require${WHITE} ${CYAN}make composer_require -e PACKAGE=symfony/dotenv" 89 | @echo "" 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Templat PHP-Nginx-Docker 2 | 3 |
4 |

:scroll: Overview

5 | 6 | Template for new projects using php and nginx inside docker containers 7 | 8 |
9 | 10 |
11 |

:desktop_computer: Start Project

12 | 13 | First clone the project: 14 | 15 | ``` 16 | git clone https://github.com/walternascimentobarroso/php-nginx-docker.git 17 | ``` 18 | 19 | Make the build 20 | 21 | _:bulb: NOTE: Before next step, check the variables in `.env` file_ 22 | 23 | ``` 24 | make build 25 | ``` 26 | 27 | _:bulb: NOTE: If the project is already compiled then just run the `make up` command_ 28 | 29 |
30 | 31 |
32 |

:hammer_and_pick: Tools

33 | 34 | - [VSCode](https://code.visualstudio.com/) 35 | - [EditorConfig](https://editorconfig.org/) 36 | - [Docker](https://www.docker.com/) 37 | - [NGINX](https://www.nginx.com/) 38 | - [PHP](https://www.php.net/) 39 | - [Composer](https://getcomposer.org/) 40 | - [Makefile](https://www.gnu.org/software/make/manual/make.html) 41 | 42 |
43 | 44 |
45 |

:open_file_folder: Folder Structure

46 | 47 | ``` 48 | . 49 | ├── .editorconfig 50 | ├── .env.example 51 | ├── .gitignore 52 | ├── .vscode 53 | │ └── launch.json 54 | ├── LICENSE 55 | ├── Makefile 56 | ├── README.md 57 | ├── composer.json 58 | ├── docker 59 | │ ├── docker-compose.yml 60 | │ ├── nginx 61 | │ │ └── default.conf 62 | │ └── php 63 | │ ├── Dockerfile 64 | │ └── xdebug.ini 65 | └── public 66 | └── index.php 67 | ``` 68 | 69 |
70 | 71 |
72 |

:memo: License

73 | 74 | The [MIT License](LICENSE) (MIT) 75 | 76 |
77 | 78 |
79 |

:smiley_cat: Author

80 | 81 | - [@walternascimentobarroso](https://walternascimentobarroso.github.io/) 82 | 83 |
84 | 85 | --- 86 | 87 | Made with  ❤️  88 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "walternascimentobarroso/default", 3 | "autoload": { 4 | "psr-4": { 5 | "App\\": "src/" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /public/index.php: -------------------------------------------------------------------------------- 1 |