luizsantos85 / docker-lemp-php7-phpmyadmin

Docker running Nginx, PHP-FPM, MySQL & PHPMyAdmin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nginx PHP 7.1 MySQL and PHPMyAdmin (LEMP)

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

    We’ll 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. Configure Xdebug

    We'll configure Xdebug for IDE (PHPStorm or Netbeans).

  5. Run the application

    By this point we’ll have all the project pieces in place.

  6. Use Makefile

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

  7. Use Docker Commands

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


Install prerequisites

For now, this project has been mainly created for Unix (Linux/MacOS). Perhaps it could work on Windows. Make sure to make compatible the relative paths in the docker-compose.yml file configuration like c:\foo\bar.

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
PHPMyAdmin 8080
Nginx 8000
Nginx SSL 3001

Clone the project

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

git clone https://github.com/carlosfgti/docker-lemp-php7-phpmyadmin.git

Go to the project directory :

cd docker-lemp-php7-phpmyadmin

Project tree

.
├── README.md
├── data
│   └── db
│       ├── dumps
│       └── mysql
├── docker-compose.yml
├── etc
│   ├── nginx
│   │   └── default.conf
│   ├── php
│   │   └── php.ini
│   └── ssl
└── web
    └── public
        └── index.php

Configure Nginx With SSL Certificates (Optional)

  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 SSL server section :

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

Configure Xdebug (Optional)

If you use another IDE than PHPStorm or Netbeans, go to the remote debugging section of Xdebug documentation.

  1. Get your own local IP address :

    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 :

    xdebug.remote_host=192.168.0.100 # your IP

Run the application

  1. Start the application :

    sudo docker-compose up -d

    Please wait this might take a several minutes...

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

  3. Stop and clear services

    sudo docker-compose down -v

Use Docker commands

Checking installed PHP extensions

sudo docker-compose exec php php -m

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 $(shell docker-compose ps -q mysqldb) mysqldump --all-databases -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" > "data/db/dumps/db.sql"

or

source .env && sudo docker exec $(shell docker-compose ps -q mysqldb) mysqldump test -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" > "data/db/dumps/test.sql"

Restore Database

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"

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!)

By Carlos Ferreira

About

Docker running Nginx, PHP-FPM, MySQL & PHPMyAdmin


Languages

Language:Shell 92.3%Language:PHP 7.7%