Membuat Web Aplikasi Sederhana Menggunakan PHP-FPM, Nginx, MariaDB dan Docker

2 menit waktu baca

Anda bisa menggunakan contoh konfigurasi berikut jika ingin membuat aplikasi sederhana menggunakan php-fpm, nginx, dan mariadb dengan docker compose

Struktur

sharing3-php-nginx-compose$ tree -a
.
├── docker-compose.yml
├── .env
├── nginx
│   ├── nginx.conf
│   └── site.conf
├── php
│   ├── Dockerfile
│   └── www.conf
└── www
    ├── conn.php
    └── index.php

Berkas-berkas

.env

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

docker-compose.yml

version: '3'
services:
  php:
      build:
            context: ./php
      ports:
          - "9000:9000"
      volumes:
          - ./www:/var/www/html
          - ./php/www.conf:/etc/php/7.1/fpm/pool.d/www.conf
      environment:
          MYSQL_USER: ${MYSQL_USER}
          MYSQL_PASSWORD: ${MYSQL_PASSWORD}
  nginx:
      image: nginx:1.14.2
      ports:
          - "8080:80"
      volumes:
          - ./www:/var/www/html:rw
          - ./nginx/site.conf:/etc/nginx/sites-enabled/site.conf
          - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      depends_on:
          - php
  dba:
      image: mariadb:10.3
      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}

nginx/nginx.conf

user www-data;
worker_processes auto;

events {
}

http {

    include mime.types;
    default_type application/octet-stream;
    include /etc/nginx/sites-enabled/*;
}

nginx/site.conf

server {
    index index.php;
    autoindex on;
    server_name php-docker.local;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html;

    location / {
      try_files $uri /index.php$is_args$args;
    }

    location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }
    fastcgi_param HTTP_PROXY "";
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    fastcgi_read_timeout 600;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }
}

php/Dockerfile

FROM php:7.1-fpm

RUN docker-php-ext-install mysqli

php/www.conf

[www]

user = www-data
group = www-data

listen = php:9000

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

www/index.php

<?php
echo "Hallo, world!";
?>

www/conn.php

<?php
$host = 'dba';
$user = getenv('MYSQL_USER');
$pass = getenv('MYSQL_PASSWORD');

$conn = mysqli_connect($host, $user, $pass);
if (!$conn) {
    exit('Kenksi ke dba gagal!: '.mysqli_connect_error().PHP_EOL);
}

echo 'Koneksi ke dba berhasil!'.PHP_EOL;

 ?>

Build

$ docker-compose up
Creating sharing3phpnginxcompose_php_1 ...
Creating sharing3phpnginxcompose_php_1 ... done
Creating sharing3phpnginxcompose_dba_1 ...
Creating sharing3phpnginxcompose_dba_1
Creating sharing3phpnginxcompose_nginx_1 ...
Creating sharing3phpnginxcompose_dba_1 ... done
Attaching to sharing3phpnginxcompose_php_1, sharing3phpnginxcompose_nginx_1, sharing3phpnginxcompose_dba_1
...
$ docker ps -a
CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS                    NAMES
ec39c09888f0        nginx:1.14.2                  "nginx -g 'daemon of…"   2 minutes ago       Up About a minute   0.0.0.0:8080->80/tcp     sharing3phpnginxcompose_nginx_1
4e2a60141e17        mariadb:10.3                  "docker-entrypoint.s…"   2 minutes ago       Up About a minute   0.0.0.0:3306->3306/tcp   sharing3phpnginxcompose_dba_1
a99d429f7d5a        sharing3phpnginxcompose_php   "docker-php-entrypoi…"   2 minutes ago       Up 2 minutes        0.0.0.0:9000->9000/tcp   sharing3phpnginxcompose_php_1
$ docker-compose ps
             Name                            Command              State           Ports         
------------------------------------------------------------------------------------------------
sharing3phpnginxcompose_dba_1     docker-entrypoint.sh mysqld     Up      0.0.0.0:3306->3306/tcp
sharing3phpnginxcompose_nginx_1   nginx -g daemon off;            Up      0.0.0.0:8080->80/tcp  
sharing3phpnginxcompose_php_1     docker-php-entrypoint php-fpm   Up      0.0.0.0:9000->9000/tcp

Test

Cek ip address yang didapatkan oleh network container

$ docker network ls
NETWORK ID          NAME                              DRIVER              SCOPE
...
98dce301e4d1        sharing3phpnginxcompose_default   bridge              local
$ echo $(docker network inspect sharing3phpnginxcompose_default | grep Gateway | grep -o -E '[0-9\.]+')
172.19.0.1
$ curl 172.19.0.1:8080
Hallo, world!
curl 172.19.0.1:8080/conn.php
Koneksi ke dba berhasil!

Label:

Kategori:

Diperbaharui:

Tinggalkan komentar