- Notifications
You must be signed in to change notification settings - Fork857
Docker running Nginx, PHP-FPM, MySQL & PHPMyAdmin
nanoninja/docker-nginx-php-mysql
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Docker running Nginx, PHP-FPM, Composer, MySQL and PHPMyAdmin.
Before installing project make sure the following prerequisites have been met.
We’ll download the code from its repository on GitHub.
Configure Nginx With SSL Certificates [
Optional]We'll generate and configure SSL certificate for nginx before running server.
Configure Xdebug [
Optional]We'll configure Xdebug for IDE (PHPStorm or Netbeans).
By this point we’ll have all the project pieces in place.
Use Makefile [
Optional]When developing, you can use
Makefilefor doing recurrent operations.When running, you can use docker commands for doing recurrent operations.
To run the docker commands without usingsudo you must add thedocker group toyour-user:
sudo usermod -aG docker your-userFor 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 ifdocker-compose is already installed by entering the following command :
which docker-compose
Check Docker Compose compatibility :
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
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 |
To installGit, 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.├── 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
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.
Generate SSL certificates
source .env&& docker run --rm -v$(pwd)/etc/ssl:/certificates -e"SERVER=$NGINX_HOST" jacoelho/generate-certificate
Configure Nginx
Do not modify the
etc/nginx/default.conffile, it is overwritten byetc/nginx/default.template.confEdit nginx file
etc/nginx/default.template.confand uncomment the SSL server section :# server {# server_name ${NGINX_HOST};## listen 443 ssl;# fastcgi_param HTTPS on;# ...# }
If you use another IDE thanPHPStorm orNetbeans, go to theremote debugging section of Xdebug documentation.
For a better integration of Docker to PHPStorm, use thedocumentation.
Get your own local IP address :
sudo ifconfig
Edit php file
etc/php/php.iniand comment or uncomment the configuration as needed.Set the
remote_hostparameter with your IP :xdebug.remote_host=192.168.0.1# your IP
Copying the composer configuration file :
cp web/app/composer.json.dist web/app/composer.json
Start the application :
docker-compose up -d
Please wait this might take a several minutes...
docker-compose logs -f# Follow log outputOpen your favorite browser :
- http://localhost:8000
- https://localhost:3000 (HTTPS not configured by default)
- http://localhost:8080 PHPMyAdmin (username: dev, password: dev)
Stop and clear services
docker-compose down -v
When developing, you can useMakefile 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 fornginx |
| 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 |
Start the application :
make docker-start
Show help :
makehelpdocker run --rm -v$(pwd)/web/app:/app composer require symfony/yamldocker run --rm -v$(pwd)/web/app:/app composer updatedocker run --rm -v$(pwd):/data phpdoc/phpdoc -i=vendor/ -d /data/web/app/src -t /data/web/app/docdocker-composeexec -T php ./app/vendor/bin/phpunit --colors=always --configuration ./appFixing standard code withPSR2
docker-composeexec -T php ./app/vendor/bin/phpcbf -v --standard=PSR2 ./app/srcChecking the standard code withPSR2
docker-composeexec -T php ./app/vendor/bin/phpcs -v --standard=PSR2 ./app/srcAnalyzing source code withPHP Mess Detector
docker-composeexec -T php ./app/vendor/bin/phpmd ./app/src text cleancode,codesize,controversial,design,naming,unusedcodedocker-composeexec php php -mdockerexec -it mysql bashand
mysql -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD"
mkdir -p data/db/dumps
source .env&& dockerexec$(docker-compose ps -q mysqldb) mysqldump --all-databases -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD">"data/db/dumps/db.sql"
source .env&& dockerexec -i$(docker-compose ps -q mysqldb) mysql -u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD"<"data/db/dumps/db.sql"
Notice: Replace "YOUR_DB_NAME" by your custom name.
source .env&& dockerexec$(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"
source .env&& dockerexec -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 fromPDO
<?phptry {$dsn ='mysql:host=mysql;dbname=test;charset=utf8;port=3306';$pdo =newPDO($dsn,'dev','dev'); }catch (PDOException$e) {echo$e->getMessage(); }?>
Any thought, feedback or (hopefully not!)
About
Docker running Nginx, PHP-FPM, MySQL & PHPMyAdmin
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.