การใช้งาน PHP 8.3 บน Docker

Docker by Keptcode.com

Last Updated: 2024-08-11 11:48

Repo: Github.com (รบกวนกด Start ให้ด้วยนะคับ)

https://github.com/coachmaxz/docker-php83fpm-example

โครงสร้างโฟลเดอร์

Path: [skeleton]

Language: Project Structure

├── build/
│  └── php83fpm/
│     └── Dockerfile
├── conf.d/
│  ├── nginx.conf
│  ├── php83.ini
│  └── localhost.conf
├── www/
│  └── index.php
└── docker-compose.yml

สร้างไฟล์: Dockerfile สำหรับ PHP 8.3 เบื้องต้น

Path: [skeleton]\build\php83fpm\Dockerfile

Language: Dockerfile

FROM php:8.3.10-fpm
ARG PSR_VERSION=1.2.0

RUN set -xe && \
    apt-get update && apt-get install -y build-essential curl git gzip libzip-dev zip libonig-dev --no-install-recommends && \
    docker-php-ext-install -j$(nproc) iconv pdo mysqli pdo_mysql mbstring sockets zip && \
    docker-php-ext-configure zip && \
    ## Download PHP Composer ##
    curl -sS https://getcomposer.org/installer | php && chmod +x composer.phar && \
    mv composer.phar /usr/local/bin/composer && \
    cd .. && \
    ## Download PSR ##
    curl -LO https://github.com/jbboehr/php-psr/archive/v${PSR_VERSION}.tar.gz && \
    tar xzf ${PWD}/v${PSR_VERSION}.tar.gz && \
    docker-php-ext-install -j $(getconf _NPROCESSORS_ONLN) \
        ${PWD}/php-psr-${PSR_VERSION} \
    && \
    ## Remove all temp files ##
    rm -r \
        ${PWD}/v${PSR_VERSION}.tar.gz \
        ${PWD}/php-psr-${PSR_VERSION} \
    && \
    apt-get clean && apt autoremove -y && \
    rm -r /var/lib/apt/lists/* && rm -rf /tmp/* /var/tmp/*

WORKDIR /usr/share/nginx/html
EXPOSE 9000

CMD ["php-fpm"]

ไฟล์: Dockerfile

[skeleton]/build/php83fpm/Dockerfile

สร้างไฟล์: ตั้งค่า PHP เวอร์ชั่น 8.3 เบื้องต้น

Path: [skeleton]\conf.d\php83.ini

Language: PHP Config

file_uploads = On
max_file_uploads = 5
allow_url_fopen = On
allow_url_include = Off

post_max_size = 50M
upload_max_filesize = 50M

memory_limit = 1024M
max_execution_time = 600

[Date]
date.timezone = Asia/Bangkok

[Session]
session.name = "SESSID"
session.auto_start = 1
session.cache_expire = 3600

ไฟล์: php83.ini

[skeleton]/conf.d/php83.ini

สร้างไฟล์: ตั้งค่า Nginx สำหรับ Web Server เบื้องต้น

Path: [skeleton]\conf.d\nginx.conf

Language: Nginx

user nginx;
worker_processes auto;

pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log warn;

events {
  worker_connections 1024;
}

http {

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

  access_log off;
  error_log /var/log/nginx/access.error.log;

  sendfile on;

  keepalive_timeout  500;
  send_timeout  600;

  client_max_body_size  100M;
  client_header_timeout  600;
  client_body_timeout  600;

  fastcgi_read_timeout  300;

  proxy_read_timeout  600;
  proxy_connect_timeout  600;
  proxy_send_timeout  600; 

  include /etc/nginx/conf.d/*.conf;

}

ไฟล์: nginx.conf

[skeleton]/conf.d/nginx.conf

สร้างไฟล์: ตั้งค่า Web Server บน Nginx Server เบื้องต้น

Path: [skeleton]\conf.d\localhost.conf

Language: Nginx

server {

  listen 80 default_server;
  listen [::]:80 default_server;

  server_name _;
  charset utf-8;

  client_max_body_size 100M;

  index index.php index.html index.htm;
  root /usr/share/nginx/html/www;

  add_header X-Frame-Options "SAMEORIGIN";
  add_header X-XSS-Protection "1; mode=block";
  add_header X-Content-Type-Options "nosniff";

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }

  location ~ [^/]\.php(/|$) {

    try_files $uri =404;

    fastcgi_index /index.php;
    fastcgi_pass php83fpm:9000;
    fastcgi_read_timeout 1800;

    include fastcgi_params;

    fastcgi_split_path_info ^(.+?\.php)(/.*)$;

    if (!-f $document_root$fastcgi_script_name) {
      return 404;
    }

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param PATH_INFO $fastcgi_path_info;

    fastcgi_hide_header X-Powered-By;

  }

  location = /favicon.ico {
    log_not_found off;
    access_log off;
  }

  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
  }

  location ~ /\.(ht|svn|git) {
    deny all;
    return 404;
  }

  location ~ /.well-known {
    allow all;
  }

  location ~ /\. {
    deny all;
  }

  access_log off;
  error_log /var/log/nginx/localhost-access.error.log;
  error_page 401 403 404 /404.html;

}

ไฟล์: localhost.conf

[skeleton]/conf.d/localhost.conf

สร้างไฟล์: รัน Container บน Docker Engine เบื้องต้น

Path: [skeleton]\docker-compose.yml

Language: Docker Engine

services:

# ======================================================================
# PHP 8.3.x
# ======================================================================

  webapp_php83_01:
    image: webapp_php83_01:latest
    container_name: webapp-php83-01
    build:
      context: ./build/php83fpm
      dockerfile: Dockerfile
    restart: on-failure
    working_dir: /usr/share/nginx/html
    environment:
      - VIRTUAL_PORT=9000
      - TERM=xterm
    volumes:
      - ./www:/usr/share/nginx/html/www
      - ./conf.d/php83.ini:/usr/local/etc/php/php.ini
    ports:
      - "9000:9000"
    networks:
      - webapp_network

# ======================================================================
# Web Server
# ======================================================================

  webapp_nginx_01:
    image: nginx:1.27.0-alpine
    container_name: webapp-nginx-01
    working_dir: /usr/share/nginx/html
    restart: always
    environment:
      - NGINX_PORT=80
      - TERM=xterm
    volumes:
      - ./www:/usr/share/nginx/html/www
      - ./conf.d/nginx.conf:/etc/nginx/nginx.conf
      - ./conf.d/localhost.conf:/etc/nginx/conf.d/default.conf
    links:
      - "webapp_php83_01:php83fpm"
    ports:
      - "80:80"
    networks:
      - webapp_network

networks:
  webapp_network:
    driver: bridge

ไฟล์: docker-compose.yml

[skeleton]/docker-compose.yml

รัน: คำสั่ง Docker เพื่อสร้าง Container ที่ต้องการ

Path: [skeleton]

Language: Bash

docker compose up -d

ตัวอย่าง: PHP 8.3.x ร่วมกับ Nginx (Web Server)

http://127.0.0.1