Herkese merhaba!

Uzun yıllardır bol miktarda kişisel zaman ve enerji harcayarak bilgimizi hepinizle paylaşıyoruz. Ancak şu andan itibaren bu blogu çalışır durumda tutabilmek için yardımınıza ihtiyacımız var. Yapmanız gereken tek şey, sitedeki reklamlardan birine tıklamak olacaktır, aksi takdirde hosting vb. masraflar nedeniyle maalesef yayından kaldırılacaktır. Teşekkürler.

Aşağıdaki örneği kullanarak docker compose ile basit bir PHP-FPM, Nginx ve MySQL uygulaması yaratabilirsiniz.


Yapı


api$ tree
.
├── docker
│ ├── nginx
│ │ └── default.conf
│ └── php
│ ├── Dockerfile
│ └── www.conf
├── .env
├── docker-compose.yml
├── index.html
├── index.php
└── connect.php

Dosyalar


.env


MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=dummy
MYSQL_USER=dummy
MYSQL_PASSWORD=dummy

docker-compose.yml


version: '3'

services:

php:
build:
context: ./docker/php
ports:
- 9000:9000
volumes:
- ./:/srv/www/api
- ./docker/php/www.conf:/usr/local/etc/php-fpm.d/www.conf
environment:
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}

nginx:
image: nginx:1.13.8
ports:
- 80:80
volumes:
- ./:/srv/www/api
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php

mysql:
image: mysql:5.7
ports:
- 3306:3306
depends_on:
- php
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}

index.html


HTML

index.php


echo 'Welcome!';

connect.php


$host = 'mysql';
$user = getenv('MYSQL_USER');
$pass = getenv('MYSQL_PASSWORD');

$conn = mysqli_connect($host, $user, $pass);
if (!$conn) {
exit('Connection failed: '.mysqli_connect_error().PHP_EOL);
}

echo 'Successful database connection!'.PHP_EOL;

docker/nginx/default.conf


server {
listen 80 default_server;

server_name api.com www.api.com;

root /srv/www/api;

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}

error_log /var/log/nginx/api_error.log;
access_log /var/log/nginx/api_access.log;
}

docker/php/www.conf


[www]

user = www-data
group = www-data

listen = nginx:9000

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

docker/php/Dockerfile


FROM php:7.2-fpm

RUN docker-php-ext-install mysqli

Kurulum


$ docker-compose up
Creating network "api_default" with the default driver
Creating api_php_1 ... done
Creating api_nginx_1 ... done
Creating api_mysql_1 ... done
Attaching to api_php_1, api_nginx_1, api_mysql_1
...

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e517fbd9cb63 mysql:5.7 "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:3306->3306/tcp api_mysql_1
4caace5a0d52 nginx:1.13.8 "nginx -g 'daemon of…" 8 minutes ago Up 8 minutes 0.0.0.0:80->80/tcp api_nginx_1
c113fb3ba41e api_php "docker-php-entrypoi…" 8 minutes ago Up 8 minutes 0.0.0.0:9000->9000/tcp api_php_1

$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------
api_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
api_nginx_1 nginx -g daemon off; Up 0.0.0.0:80->80/tcp
api_php_1 docker-php-entrypoint php-fpm Up 0.0.0.0:9000->9000/tcp

Test


Önce container ağının IP adresini alın ve sonra test edin.


$ echo $(docker network inspect api_default | grep Gateway | grep -o -E '[0-9\.]+')
172.18.0.1

$ curl 172.18.0.1
HTML

$ curl 172.18.0.1/index.php
Welcome!

$ curl 172.18.0.1/connect.php
Successful database connection!