diff --git a/.env b/.env index 393089d78..1138e9315 100644 --- a/.env +++ b/.env @@ -6,9 +6,12 @@ NGINX_HOST=localhost # PHP + +# See https://hub.docker.com/r/nanoninja/php-fpm/tags/ PHP_VERSION=latest # MySQL +MYSQL_VERSION=8.0.21 MYSQL_HOST=mysql MYSQL_DATABASE=test MYSQL_ROOT_USER=root diff --git a/.gitignore b/.gitignore index e60fccf2a..9a4aa7a18 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,11 @@ # Application web/app/composer.json +web/app/composer.lock +web/app/vendor/ +web/app/doc/ +web/app/report/ +data/ # PHPStorm .idea/**/workspace.xml @@ -14,4 +19,10 @@ web/app/composer.json nbproject/ /nbproject/ /nbproject/private/ -/nbproject/private/private.properties \ No newline at end of file +/nbproject/private/private.properties + +# VSCode +.vscode/ + +# SSL Certs +etc/ssl/ diff --git a/.travis.yml b/.travis.yml index 20b00e0be..8ec197855 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ sudo: required env: - DOCKER_COMPOSE_VERSION: 1.14.0 + DOCKER_COMPOSE_VERSION: 1.18.0 services: - docker @@ -19,11 +19,12 @@ before_script: - sleep 2m script: - - sudo make gen-certs - sudo make apidoc - - sudo make test + - sudo make gen-certs - sudo make mysql-dump - sudo make mysql-restore + - sudo make phpmd + - sudo make test after_script: - sudo make docker-stop \ No newline at end of file diff --git a/Makefile b/Makefile index 11484216b..b396bfdf2 100644 --- a/Makefile +++ b/Makefile @@ -18,15 +18,16 @@ help: @echo " docker-stop Stop and clear all services" @echo " gen-certs Generate SSL certificates" @echo " logs Follow log output" - @echo " mysql-dump Create backup of whole database" - @echo " mysql-restore Restore backup from whole database" + @echo " mysql-dump Create backup of all databases" + @echo " mysql-restore Restore backup of all databases" + @echo " phpmd Analyse the API with PHP Mess Detector" @echo " test Test application" init: @$(shell cp -n $(shell pwd)/web/app/composer.json.dist $(shell pwd)/web/app/composer.json 2> /dev/null) apidoc: - @docker-compose exec -T php ./app/vendor/bin/apigen generate app/src --destination app/doc + @docker run --rm -v $(shell pwd):/data phpdoc/phpdoc -i=vendor/ -d /data/web/app/src -t /data/web/app/doc @make resetOwner clean: @@ -66,6 +67,11 @@ mysql-dump: mysql-restore: @docker exec -i $(shell docker-compose ps -q mysqldb) mysql -u"$(MYSQL_ROOT_USER)" -p"$(MYSQL_ROOT_PASSWORD)" < $(MYSQL_DUMPS_DIR)/db.sql 2>/dev/null +phpmd: + @docker-compose exec -T php \ + ./app/vendor/bin/phpmd \ + ./app/src text cleancode,codesize,controversial,design,naming,unusedcode + test: code-sniff @docker-compose exec -T php ./app/vendor/bin/phpunit --colors=always --configuration ./app/ @make resetOwner diff --git a/README.md b/README.md index 85f41bfb3..faae0b8b9 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,11 @@ Docker running Nginx, PHP-FPM, Composer, MySQL and PHPMyAdmin. We’ll download the code from its repository on GitHub. -3. [Configure Nginx With SSL Certificates](#configure-nginx-with-ssl-certificates) [Optional] +3. [Configure Nginx With SSL Certificates](#configure-nginx-with-ssl-certificates) [`Optional`] We'll generate and configure SSL certificate for nginx before running server. -4. [Configure Xdebug](#configure-xdebug) [Optional] +4. [Configure Xdebug](#configure-xdebug) [`Optional`] We'll configure Xdebug for IDE (PHPStorm or Netbeans). @@ -24,7 +24,7 @@ Docker running Nginx, PHP-FPM, Composer, MySQL and PHPMyAdmin. By this point we’ll have all the project pieces in place. -6. [Use Makefile](#use-makefile) [Optional] +6. [Use Makefile](#use-makefile) [`Optional`] When developing, you can use `Makefile` for doing recurrent operations. @@ -36,6 +36,12 @@ ___ ## Install prerequisites +To run the docker commands without using **sudo** you must add the **docker** group to **your-user**: + +``` +sudo usermod -aG docker your-user +``` + For now, this project has been mainly created for Unix `(Linux/MacOS)`. Perhaps it could work on Windows. All requisites should be available for your distribution. The most important are : @@ -52,7 +58,7 @@ which docker-compose Check Docker Compose compatibility : - - [Compose file version 3 reference](https://docs.docker.com/compose/compose-file/) +* [Compose file version 3 reference](https://docs.docker.com/compose/compose-file/) The following is optional but makes life more enjoyable : @@ -86,17 +92,17 @@ This project use the following ports : | Nginx | 8000 | | Nginx SSL | 3000 | ---- +___ ## Clone the project -To install [Git](http://git-scm.com/book/en/v2/Getting-Started-Installing-Git), download it and install following the instructions : +To install [Git](http://git-scm.com/book/en/v2/Getting-Started-Installing-Git), download it and install following the instructions : ```sh git clone https://github.com/nanoninja/docker-nginx-php-mysql.git ``` -Go to the project directory : +Go to the project directory : ```sh cd docker-nginx-php-mysql @@ -134,7 +140,7 @@ cd docker-nginx-php-mysql └── index.php ``` ---- +___ ## Configure Nginx With SSL Certificates @@ -145,7 +151,7 @@ If you modify the host name, do not forget to add it to the `/etc/hosts` file. 1. Generate SSL certificates ```sh - source .env && sudo docker run --rm -v $(pwd)/etc/ssl:/certificates -e "SERVER=$NGINX_HOST" jacoelho/generate-certificate + source .env && docker run --rm -v $(pwd)/etc/ssl:/certificates -e "SERVER=$NGINX_HOST" jacoelho/generate-certificate ``` 2. Configure Nginx @@ -164,7 +170,7 @@ If you modify the host name, do not forget to add it to the `/etc/hosts` file. # } ``` ---- +___ ## Configure Xdebug @@ -185,7 +191,7 @@ For a better integration of Docker to PHPStorm, use the [documentation](https:// ```sh xdebug.remote_host=192.168.0.1 # your IP ``` ---- +___ ## Run the application @@ -198,13 +204,13 @@ For a better integration of Docker to PHPStorm, use the [documentation](https:// 2. Start the application : ```sh - sudo docker-compose up -d + docker-compose up -d ``` **Please wait this might take a several minutes...** ```sh - sudo docker-compose logs -f # Follow log output + docker-compose logs -f # Follow log output ``` 3. Open your favorite browser : @@ -216,35 +222,36 @@ For a better integration of Docker to PHPStorm, use the [documentation](https:// 4. Stop and clear services ```sh - sudo docker-compose down -v + docker-compose down -v ``` ---- +___ ## Use Makefile When developing, you can use [Makefile](https://en.wikipedia.org/wiki/Make_(software)) for doing the following operations : -| Name | Description | -|---------------|--------------------------------------------| -| apidoc | Generate documentation of API | -| clean | Clean directories for reset | -| code-sniff | Check the API with PHP Code Sniffer (PSR2) | -| composer-up | Update PHP dependencies with composer | -| docker-start | Create and start containers | -| docker-stop | Stop and clear all services | -| gen-certs | Generate SSL certificates for `nginx` | -| logs | Follow log output | -| mysql-dump | Create backup of whole database | -| mysql-restore | Restore backup from whole database | -| test | Test application with phpunit | +| Name | Description | +|---------------|----------------------------------------------| +| apidoc | Generate documentation of API | +| clean | Clean directories for reset | +| code-sniff | Check the API with PHP Code Sniffer (`PSR2`) | +| composer-up | Update PHP dependencies with composer | +| docker-start | Create and start containers | +| docker-stop | Stop and clear all services | +| gen-certs | Generate SSL certificates for `nginx` | +| logs | Follow log output | +| mysql-dump | Create backup of all databases | +| mysql-restore | Restore backup of all databases | +| phpmd | Analyse the API with PHP Mess Detector | +| test | Test application with phpunit | ### Examples -Start the application : +Start the application : ```sh -sudo make docker-start +make docker-start ``` Show help : @@ -253,44 +260,56 @@ Show help : make help ``` ---- +___ ## Use Docker commands ### Installing package with composer ```sh -sudo docker run --rm -v $(pwd)/web/app:/app composer require symfony/yaml +docker run --rm -v $(pwd)/web/app:/app composer require symfony/yaml ``` ### Updating PHP dependencies with composer ```sh -sudo docker run --rm -v $(pwd)/web/app:/app composer update +docker run --rm -v $(pwd)/web/app:/app composer update ``` ### Generating PHP API documentation ```sh -sudo docker-compose exec -T php ./app/vendor/bin/apigen generate app/src --destination ./app/doc +docker run --rm -v $(pwd):/data phpdoc/phpdoc -i=vendor/ -d /data/web/app/src -t /data/web/app/doc ``` ### Testing PHP application with PHPUnit ```sh -sudo docker-compose exec -T php ./app/vendor/bin/phpunit --colors=always --configuration ./app/ +docker-compose exec -T php ./app/vendor/bin/phpunit --colors=always --configuration ./app +``` + +### Fixing standard code with [PSR2](http://www.php-fig.org/psr/psr-2/) + +```sh +docker-compose exec -T php ./app/vendor/bin/phpcbf -v --standard=PSR2 ./app/src ``` ### Checking the standard code with [PSR2](http://www.php-fig.org/psr/psr-2/) ```sh -sudo docker-compose exec -T php ./app/vendor/bin/phpcs -v --standard=PSR2 ./app/src +docker-compose exec -T php ./app/vendor/bin/phpcs -v --standard=PSR2 ./app/src +``` + +### Analyzing source code with [PHP Mess Detector](https://phpmd.org/) + +```sh +docker-compose exec -T php ./app/vendor/bin/phpmd ./app/src text cleancode,codesize,controversial,design,naming,unusedcode ``` ### Checking installed PHP extensions ```sh -sudo docker-compose exec php php -m +docker-compose exec php php -m ``` ### Handling database @@ -298,7 +317,7 @@ sudo docker-compose exec php php -m #### MySQL shell access ```sh -sudo docker exec -it mysql bash +docker exec -it mysql bash ``` and @@ -307,28 +326,37 @@ and mysql -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" ``` -#### Backup of database +#### Creating a backup of all databases ```sh mkdir -p data/db/dumps ``` ```sh -source .env && sudo docker exec $(sudo docker-compose ps -q mysqldb) mysqldump --all-databases -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" > "data/db/dumps/db.sql" +source .env && docker exec $(docker-compose ps -q mysqldb) mysqldump --all-databases -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" > "data/db/dumps/db.sql" ``` -or +#### Restoring a backup of all databases ```sh -source .env && sudo docker exec $(sudo docker-compose ps -q mysqldb) mysqldump test -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" > "data/db/dumps/test.sql" +source .env && docker exec -i $(docker-compose ps -q mysqldb) mysql -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" < "data/db/dumps/db.sql" ``` -#### Restore Database +#### Creating a backup of single database + +**`Notice:`** Replace "YOUR_DB_NAME" by your custom name. ```sh -source .env && sudo docker exec -i $(sudo docker-compose ps -q mysqldb) mysql -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" < "data/db/dumps/db.sql" +source .env && docker exec $(docker-compose ps -q mysqldb) mysqldump -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" --databases YOUR_DB_NAME > "data/db/dumps/YOUR_DB_NAME_dump.sql" ``` +#### Restoring a backup of single database + +```sh +source .env && docker exec -i $(docker-compose ps -q mysqldb) mysql -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" < "data/db/dumps/YOUR_DB_NAME_dump.sql" +``` + + #### Connecting MySQL from [PDO](http://php.net/manual/en/book.pdo.php) ```php @@ -342,10 +370,8 @@ source .env && sudo docker exec -i $(sudo docker-compose ps -q mysqldb) mysql -u ?> ``` ---- - -## Help us ! +___ -Any thought, feedback or (hopefully not!) +## Help us -Developed by [@letvinz](https://twitter.com/letvinz) \ No newline at end of file +Any thought, feedback or (hopefully not!) \ No newline at end of file diff --git a/doc/configure-cacert-for-local-macosx.md b/doc/configure-cacert-for-local-macosx.md new file mode 100644 index 000000000..5af87dbfd --- /dev/null +++ b/doc/configure-cacert-for-local-macosx.md @@ -0,0 +1,26 @@ +# Configure Local CA CERT with MacOS + +## 1. The warning you receive while developing locally. + +![Warning](images/cacert-1-warning.png) + +## 2. Open the keychain app. + +![Open Keychain](images/cacert-2-open-keychain.png) + +## 3. Use File --> Import to add the ca cert you've already created. + +![Add CA Cert](images/cacert-3-add-cacert-file-import.png) + +## 4. Once added, locate it via the search box. + +![Localhost Search Box](images/cacert-4-locate-cert.png) + +## 5. Set to always trust. + +![Trust The Cert](images/cacert-5-set-to-always-trust.png) + +## 6. Reload the webpage. + +![The Results](images/cacert-6-reload-page.png) + diff --git a/doc/images/cacert-1-warning.png b/doc/images/cacert-1-warning.png new file mode 100644 index 000000000..2a1365b65 Binary files /dev/null and b/doc/images/cacert-1-warning.png differ diff --git a/doc/images/cacert-2-open-keychain.png b/doc/images/cacert-2-open-keychain.png new file mode 100644 index 000000000..7a90788be Binary files /dev/null and b/doc/images/cacert-2-open-keychain.png differ diff --git a/doc/images/cacert-3-add-cacert-file-import.png b/doc/images/cacert-3-add-cacert-file-import.png new file mode 100644 index 000000000..f7dea1fbb Binary files /dev/null and b/doc/images/cacert-3-add-cacert-file-import.png differ diff --git a/doc/images/cacert-4-locate-cert.png b/doc/images/cacert-4-locate-cert.png new file mode 100644 index 000000000..4031a8a5e Binary files /dev/null and b/doc/images/cacert-4-locate-cert.png differ diff --git a/doc/images/cacert-5-set-to-always-trust.png b/doc/images/cacert-5-set-to-always-trust.png new file mode 100644 index 000000000..6e7fee09d Binary files /dev/null and b/doc/images/cacert-5-set-to-always-trust.png differ diff --git a/doc/images/cacert-6-reload-page.png b/doc/images/cacert-6-reload-page.png new file mode 100644 index 000000000..3bbeb4201 Binary files /dev/null and b/doc/images/cacert-6-reload-page.png differ diff --git a/docker-compose.yml b/docker-compose.yml index 88ff7f68f..b30cd5728 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -40,7 +40,7 @@ services: depends_on: - mysqldb mysqldb: - image: mysql + image: mysql:${MYSQL_VERSION} container_name: ${MYSQL_HOST} restart: always env_file: diff --git a/etc/nginx/default.conf b/etc/nginx/default.conf index 1088f8f2a..17db04a90 100644 --- a/etc/nginx/default.conf +++ b/etc/nginx/default.conf @@ -10,6 +10,20 @@ server { access_log /var/log/nginx/access.log; root /var/www/html/public; + set $virtualdir ""; + set $realdir ""; + + if ($request_uri ~ ^/([^/]*)/.*$ ) { + set $virtualdir /$1; + } + + if (-d "$document_root$virtualdir") { + set $realdir "${virtualdir}"; + } + + location / { + try_files $uri $uri/ $realdir/index.php?$args; + } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; @@ -36,6 +50,21 @@ server { # access_log /var/log/nginx/access.log; # root /var/www/html/public; +# set $virtualdir ""; +# set $realdir ""; + +# if ($request_uri ~ ^/([^/]*)/.*$ ) { +# set $virtualdir /$1; +# } + +# if (-d "$document_root$virtualdir") { +# set $realdir "${virtualdir}"; +# } + +# location / { +# try_files $uri $uri/ $realdir/index.php?$args; +# } + # location ~ \.php$ { # try_files $uri =404; # fastcgi_split_path_info ^(.+\.php)(/.+)$; @@ -45,4 +74,4 @@ server { # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # fastcgi_param PATH_INFO $fastcgi_path_info; # } -# } \ No newline at end of file +# } diff --git a/etc/nginx/default.template.conf b/etc/nginx/default.template.conf index 2a485890f..3d76e28d9 100644 --- a/etc/nginx/default.template.conf +++ b/etc/nginx/default.template.conf @@ -10,6 +10,20 @@ server { access_log /var/log/nginx/access.log; root /var/www/html/public; + set $virtualdir ""; + set $realdir ""; + + if ($request_uri ~ ^/([^/]*)/.*$ ) { + set $virtualdir /$1; + } + + if (-d "$document_root$virtualdir") { + set $realdir "${virtualdir}"; + } + + location / { + try_files $uri $uri/ $realdir/index.php?$args; + } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; @@ -36,6 +50,21 @@ server { # access_log /var/log/nginx/access.log; # root /var/www/html/public; +# set $virtualdir ""; +# set $realdir ""; + +# if ($request_uri ~ ^/([^/]*)/.*$ ) { +# set $virtualdir /$1; +# } + +# if (-d "$document_root$virtualdir") { +# set $realdir "${virtualdir}"; +# } + +# location / { +# try_files $uri $uri/ $realdir/index.php?$args; +# } + # location ~ \.php$ { # try_files $uri =404; # fastcgi_split_path_info ^(.+\.php)(/.+)$; @@ -45,4 +74,4 @@ server { # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # fastcgi_param PATH_INFO $fastcgi_path_info; # } -# } \ No newline at end of file +# } diff --git a/etc/php/php.ini b/etc/php/php.ini index 9453036cb..4f47c01f7 100644 --- a/etc/php/php.ini +++ b/etc/php/php.ini @@ -1,5 +1,18 @@ +; PHP Configuration + +;[Date] +; Defines the default timezone used by the date functions +; http://php.net/date.timezone +;date.timezone = + +; Error handling +;display_errors = Off + +; Xdebug +; See https://xdebug.org/docs/all_settings + ;PHPStorm -[xdebug] +[Xdebug] xdebug.remote_enable=1 xdebug.idekey=PHPSTORM xdebug.profiler_enable=0 @@ -8,7 +21,7 @@ xdebug.remote_host=192.168.0.1 # your ip xdebug.remote_port=9000 ;Netbeans -;[xdebug] +;[Xdebug] ;xdebug.remote_enable=1 ;xdebug.remote_handler=dbgp ;xdebug.remote_mode=req diff --git a/web/app/composer.json.dist b/web/app/composer.json.dist index 44e1caa31..b4850f712 100644 --- a/web/app/composer.json.dist +++ b/web/app/composer.json.dist @@ -1,12 +1,11 @@ { "require": { - + "symfony/yaml": "^5.1" }, "require-dev": { - "squizlabs/php_codesniffer": "3.*", - "phpunit/phpunit": "^6.0", - "apigen/apigen": "dev-master", - "roave/better-reflection": "dev-master#c87d856" + "phpmd/phpmd": "@stable", + "phpunit/phpunit": "^9.0", + "squizlabs/php_codesniffer": "3.5.*" }, "autoload": { "psr-4": { @@ -18,6 +17,6 @@ "AppTest\\Acme\\": "test/" } }, - "minimum-stability": "dev", + "minimum-stability": "stable", "prefer-stable": true -} \ No newline at end of file +} diff --git a/web/app/phpunit.xml.dist b/web/app/phpunit.xml.dist index d31dbbb33..7cd9c11cf 100644 --- a/web/app/phpunit.xml.dist +++ b/web/app/phpunit.xml.dist @@ -1,9 +1,5 @@ - - + @@ -25,7 +21,7 @@ - + + - \ No newline at end of file + diff --git a/web/app/src/Foo.php b/web/app/src/Foo.php index f94d679cd..f982f487f 100644 --- a/web/app/src/Foo.php +++ b/web/app/src/Foo.php @@ -1,9 +1,19 @@