2017-07-29 14:16:49 +02:00
2017-07-28 20:05:51 +02:00
2017-07-27 20:01:19 +02:00
2017-07-28 14:27:07 +02:00
2017-06-13 22:20:18 +02:00
2017-07-29 14:16:49 +02:00
2017-07-29 14:16:49 +02:00
2017-07-29 11:25:21 +02:00

Nginx PHP MySQL

Docker running Nginx, PHP-FPM, Composer, MySQL and PHPMyAdmin.

Overview

  1. Install prerequisites

    Before installing project make sure the following prerequisites have been met.

  2. Clone the project

    Well download the code from its repository on GitHub.

  3. Configure Nginx With SSL Certificates

    We'll generate and configure SSL certificate for nginx before running server.

  4. Run the application

    By this point well have all the project pieces in place.

  5. Use Makefile

    When developing, you can use Makefile for doing recurrent operations.

  6. Use Docker Commands

    When running, you can use docker commands for doing recurrent operations.


Install prerequisites

All requisites should be available for your distribution. The most important are :

Check if docker-compose is already installed by entering the following command :

which docker-compose

The following is optional but makes life more enjoyable :

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.

sudo apt install build-essential

Images to use

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
Nginx 8000
Nginx SSL 3000

Clone the project

To install Git, download it and install following the instructions :

git clone https://github.com/nanoninja/docker-nginx-php-mysql.git

Go to the project directory :

cd docker-nginx-php-mysql

Project tree

.
├── Makefile
├── README.md
├── bin
│   └── linux
│       └── clean.sh
├── data
│   └── db
│       ├── dumps
│       └── mysql
├── docker-compose.yml
├── etc
│   ├── nginx
│   │   └── default.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

  1. Generate SSL certificates

    sudo docker run --rm -v $(pwd)/etc/ssl:/certificates -e "SERVER=localhost" jacoelho/generate-certificate
    
  2. Configure Nginx

    Edit nginx file etc/nginx/default.conf and uncomment the server section :

    # server {
    #     server_name localhost;
    #
    #     listen 443 ssl;
    #     ...
    # }
    

Run the application

  1. Copying the composer configuration file :

    cp web/app/composer.json.dist web/app/composer.json
    
  2. Start the application :

    sudo docker-compose up -d
    

    Please wait this might take a several minutes...

    sudo docker-compose logs -f # Follow log output
    
  3. Open your favorite browser :

  4. Stop and clear services

    sudo docker-compose stop && sudo docker-compose kill && sudo docker-compose rm -f
    

Use Makefile

When developing, you can use Makefile for doing the following operations :

Name Description
apidoc Generate documentation of API
clean Clean directories for reset
composer-up Update php composer
docker-start Create and start containers
docker-stop Stop 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

Examples

Start the application :

sudo make docker-start

Show help :

make help

Use Docker commands

Updating PHP dependencies with composer

sudo docker run --rm -v $(pwd)/web/app:/app composer/composer update

Generating PHP API documentation

sudo docker exec -i $(sudo docker-compose ps -q php) php ./app/vendor/apigen/apigen/bin/apigen generate -s app/src -d app/doc

Testing PHP application with PHPUnit

sudo docker exec -i $(sudo docker-compose ps -q php) ./app/vendor/bin/phpunit --colors=always --configuration app/

Handling database

MySQL shell access

sudo docker exec -it mysql bash

and

mysql -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD"

Backup of database

mkdir -p data/db/dumps
source .env && sudo docker exec -i mysql mysqldump --all-databases -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" > "data/db/dumps/db.sql"

or

source .env && sudo docker exec -i mysql mysqldump test -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" > "data/db/dumps/test.sql"

Connecting MySQL from PDO

<?php
    try {
        $dsn = 'mysql:host=mysql;dbname=test;charset=utf8;port=3306';
        $pdo = new PDO($dsn, 'dev', 'dev');
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
?>

Help us !

Any thought, feedback or (hopefully not!)

Developed by @letvinz

Description
Docker running Nginx, PHP-FPM, MySQL, and PHPMyAdmin
Readme 17 MiB
Languages
Shell 76.7%
Batchfile 15.8%
Makefile 7.5%