diff --git a/README.md b/README.md index faae0b8b9..8b1378917 100644 --- a/README.md +++ b/README.md @@ -1,377 +1 @@ -# Nginx PHP MySQL [![Build Status](https://travis-ci.org/nanoninja/docker-nginx-php-mysql.svg?branch=master)](https://travis-ci.org/nanoninja/docker-nginx-php-mysql) [![GitHub version](https://badge.fury.io/gh/nanoninja%2Fdocker-nginx-php-mysql.svg)](https://badge.fury.io/gh/nanoninja%2Fdocker-nginx-php-mysql) -Docker running Nginx, PHP-FPM, Composer, MySQL and PHPMyAdmin. - -## Overview - -1. [Install prerequisites](#install-prerequisites) - - Before installing project make sure the following prerequisites have been met. - -2. [Clone the project](#clone-the-project) - - We’ll download the code from its repository on GitHub. - -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`] - - We'll configure Xdebug for IDE (PHPStorm or Netbeans). - -5. [Run the application](#run-the-application) - - By this point we’ll have all the project pieces in place. - -6. [Use Makefile](#use-makefile) [`Optional`] - - When developing, you can use `Makefile` for doing recurrent operations. - -7. [Use Docker Commands](#use-docker-commands) - - When running, you can use docker commands for doing recurrent operations. - -___ - -## 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 : - -* [Git](https://git-scm.com/downloads) -* [Docker](https://docs.docker.com/engine/installation/) -* [Docker Compose](https://docs.docker.com/compose/install/) - -Check if `docker-compose` is already installed by entering the following command : - -```sh -which docker-compose -``` - -Check Docker Compose compatibility : - -* [Compose file version 3 reference](https://docs.docker.com/compose/compose-file/) - -The following is optional but makes life more enjoyable : - -```sh -which make -``` - -On Ubuntu and Debian these are available in the meta-package build-essential. On other distributions, you may need to install the GNU C++ compiler separately. - -```sh -sudo apt install build-essential -``` - -### Images to use - -* [Nginx](https://hub.docker.com/_/nginx/) -* [MySQL](https://hub.docker.com/_/mysql/) -* [PHP-FPM](https://hub.docker.com/r/nanoninja/php-fpm/) -* [Composer](https://hub.docker.com/_/composer/) -* [PHPMyAdmin](https://hub.docker.com/r/phpmyadmin/phpmyadmin/) -* [Generate Certificate](https://hub.docker.com/r/jacoelho/generate-certificate/) - -You should be careful when installing third party web servers such as MySQL or Nginx. - -This project use the following ports : - -| Server | Port | -|------------|------| -| MySQL | 8989 | -| PHPMyAdmin | 8080 | -| 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 : - -```sh -git clone https://github.com/nanoninja/docker-nginx-php-mysql.git -``` - -Go to the project directory : - -```sh -cd docker-nginx-php-mysql -``` - -### Project tree - -```sh -. -├── Makefile -├── README.md -├── data -│ └── db -│ ├── dumps -│ └── mysql -├── doc -├── docker-compose.yml -├── etc -│ ├── nginx -│ │ ├── default.conf -│ │ └── default.template.conf -│ ├── php -│ │ └── php.ini -│ └── ssl -└── web - ├── app - │ ├── composer.json.dist - │ ├── phpunit.xml.dist - │ ├── src - │ │ └── Foo.php - │ └── test - │ ├── FooTest.php - │ └── bootstrap.php - └── public - └── index.php -``` - -___ - -## Configure Nginx With SSL Certificates - -You can change the host name by editing the `.env` file. - -If you modify the host name, do not forget to add it to the `/etc/hosts` file. - -1. Generate SSL certificates - - ```sh - source .env && docker run --rm -v $(pwd)/etc/ssl:/certificates -e "SERVER=$NGINX_HOST" jacoelho/generate-certificate - ``` - -2. Configure Nginx - - Do not modify the `etc/nginx/default.conf` file, it is overwritten by `etc/nginx/default.template.conf` - - Edit nginx file `etc/nginx/default.template.conf` and uncomment the SSL server section : - - ```sh - # server { - # server_name ${NGINX_HOST}; - # - # listen 443 ssl; - # fastcgi_param HTTPS on; - # ... - # } - ``` - -___ - -## Configure Xdebug - -If you use another IDE than [PHPStorm](https://www.jetbrains.com/phpstorm/) or [Netbeans](https://netbeans.org/), go to the [remote debugging](https://xdebug.org/docs/remote) section of Xdebug documentation. - -For a better integration of Docker to PHPStorm, use the [documentation](https://github.com/nanoninja/docker-nginx-php-mysql/blob/master/doc/phpstorm-macosx.md). - -1. Get your own local IP address : - - ```sh - sudo ifconfig - ``` - -2. Edit php file `etc/php/php.ini` and comment or uncomment the configuration as needed. - -3. Set the `remote_host` parameter with your IP : - - ```sh - xdebug.remote_host=192.168.0.1 # your IP - ``` -___ - -## Run the application - -1. Copying the composer configuration file : - - ```sh - cp web/app/composer.json.dist web/app/composer.json - ``` - -2. Start the application : - - ```sh - docker-compose up -d - ``` - - **Please wait this might take a several minutes...** - - ```sh - docker-compose logs -f # Follow log output - ``` - -3. Open your favorite browser : - - * [http://localhost:8000](http://localhost:8000/) - * [https://localhost:3000](https://localhost:3000/) ([HTTPS](#configure-nginx-with-ssl-certificates) not configured by default) - * [http://localhost:8080](http://localhost:8080/) PHPMyAdmin (username: dev, password: dev) - -4. Stop and clear services - - ```sh - 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 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 : - -```sh -make docker-start -``` - -Show help : - -```sh -make help -``` - -___ - -## Use Docker commands - -### Installing package with composer - -```sh -docker run --rm -v $(pwd)/web/app:/app composer require symfony/yaml -``` - -### Updating PHP dependencies with composer - -```sh -docker run --rm -v $(pwd)/web/app:/app composer update -``` - -### Generating PHP API documentation - -```sh -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 -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 -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 -docker-compose exec php php -m -``` - -### Handling database - -#### MySQL shell access - -```sh -docker exec -it mysql bash -``` - -and - -```sh -mysql -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" -``` - -#### Creating a backup of all databases - -```sh -mkdir -p data/db/dumps -``` - -```sh -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" -``` - -#### Restoring a backup of all databases - -```sh -source .env && docker exec -i $(docker-compose ps -q mysqldb) mysql -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" < "data/db/dumps/db.sql" -``` - -#### Creating a backup of single database - -**`Notice:`** Replace "YOUR_DB_NAME" by your custom name. - -```sh -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 -getMessage(); - } -?> -``` - -___ - -## Help us - -Any thought, feedback or (hopefully not!) \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index b30cd5728..5c3e37e2f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,56 +1,75 @@ -version: '3' +--- +version: "3" + services: - web: - image: nginx:alpine - volumes: - - "./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf" - - "./etc/ssl:/etc/ssl" - - "./web:/var/www/html" - - "./etc/nginx/default.template.conf:/etc/nginx/conf.d/default.template" - ports: - - "8000:80" - - "3000:443" - environment: - - NGINX_HOST=${NGINX_HOST} - command: /bin/sh -c "envsubst '$$NGINX_HOST' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'" - restart: always - depends_on: - - php - - mysqldb - php: - image: nanoninja/php-fpm:${PHP_VERSION} - restart: always - volumes: - - "./etc/php/php.ini:/usr/local/etc/php/conf.d/php.ini" - - "./web:/var/www/html" - composer: - image: "composer" - volumes: - - "./web/app:/app" - command: install - myadmin: - image: phpmyadmin/phpmyadmin - container_name: phpmyadmin - ports: - - "8080:80" - environment: - - PMA_ARBITRARY=1 - - PMA_HOST=${MYSQL_HOST} - restart: always - depends_on: - - mysqldb - mysqldb: - image: mysql:${MYSQL_VERSION} - container_name: ${MYSQL_HOST} - restart: always - env_file: - - ".env" - environment: - - MYSQL_DATABASE=${MYSQL_DATABASE} - - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} - - MYSQL_USER=${MYSQL_USER} - - MYSQL_PASSWORD=${MYSQL_PASSWORD} - ports: - - "8989:3306" - volumes: - - "./data/db/mysql:/var/lib/mysql" \ No newline at end of file + web-nginx: + image: nginx:alpine + restart: always + command: /bin/sh -c "envsubst '$$NGINX_HOST' < /etc/nginx/conf.d/default.template + > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'" + ports: + - "8001:80" + - "4430:443" + depends_on: + - php + - mysql + environment: + NGINX_HOST: ${NGINX_HOST} + volumes: + - ./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf + - ./etc/ssl:/etc/ssl + - ./web:/var/www/html + - ./etc/nginx/default.template.conf:/etc/nginx/conf.d/default.template + + php: + image: nanoninja/php-fpm:${PHP_VERSION} + restart: always + volumes: + - ./etc/php/php.ini:/usr/local/etc/php/conf.d/php.ini + - ./web:/var/www/html + + composer: + image: composer:latest + restart: always + command: install + volumes: + - ./web/app:/app + + phpmyadmin: + image: phpmyadmin/phpmyadmin:latest + container_name: phpmyadmin + restart: always + ports: + - "8080:80" + depends_on: + - mysql + environment: + PMA_ARBITRARY: '1' + PMA_HOST: ${MYSQL_HOST} + + mysql: + image: mysql:${MYSQL_VERSION} + container_name: ${MYSQL_HOST} + restart: always + ports: + - "8989:3306" + environment: + MYSQL_DATABASE: ${MYSQL_DATABASE} + MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} + MYSQL_USER: ${MYSQL_USER} + MYSQL_PASSWORD: ${MYSQL_PASSWORD} + volumes: + - ./data/db/mysql:/var/lib/mysql + + web-apache2: + image: ubuntu/apache2:latest + restart: always + ports: + - "8001:80" + - "4431:443" + depends_on: + - php + - mysql + volumes: + - ./etc/ssl:/etc/ssl + - ./web:/var/www/html