Skip to content

nanoninja/docker-nginx-php-mysql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nginx PHP MySQL

Docker running Nginx, PHP-FPM, MySQL.

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 :

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

docker compose

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

Project tree

.
β”œβ”€β”€ data
β”‚   └── db
β”‚       β”œβ”€β”€ dumps
β”‚       └── mysql
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ etc
β”‚   β”œβ”€β”€ nginx
β”‚   β”‚   β”œβ”€β”€ default.conf
β”‚   β”‚   └── default.template.conf
β”‚   β”œβ”€β”€ php
β”‚   β”‚   └── php.ini
β”‚   └── ssl
└── web
    └── 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

    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 :

    # server {
    #     server_name ${NGINX_HOST};
    #
    #     listen 443 ssl;
    #     fastcgi_param HTTPS on;
    #     ...
    # }

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();
    }
?>