Phalcon Framework

Docker by Keptcode.com

Last Updated: 2024-09-30 20:08

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

Path: [skeleton]

Language: Project Structure

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

สร้างไฟล์: Dockerfile สำหรับ PHP 8.3 (Phalcon 5)

Path: [skeleton]\build\php83fpm\Dockerfile

Language: Dockerfile

FROM php:8.3.12-fpm

ARG PHALCON_VERSION=5.8.0

RUN set -xe && \
    apt-get update && apt-get install -y curl git gzip libzip-dev zip --no-install-recommends && \
    docker-php-ext-install -j$(nproc) iconv pdo mysqli pdo_mysql sockets exif bcmath 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 && \
    ## Download Mongo DB ##
    git clone https://github.com/mongodb/mongo-php-driver.git && cd mongo-php-driver && \
    git submodule update --init && phpize && ./configure && make all && make install && \
    echo "[mongodb]" >> /usr/local/etc/php/conf.d/mongodb.ini && \
    echo "extension=mongodb.so" >> /usr/local/etc/php/conf.d/docker-php-ext-mongodb.ini && \
    cd .. && \
    ## Download Phalcon ##
    curl -LO https://github.com/phalcon/cphalcon/archive/v${PHALCON_VERSION}.tar.gz && \
    tar xzf ${PWD}/v${PHALCON_VERSION}.tar.gz && cd ${PWD}/cphalcon-${PHALCON_VERSION}/build && ./install && \
    echo "[phalcon]" >> /usr/local/etc/php/conf.d/phalcon.ini && \
    echo "extension=phalcon.so" >> /usr/local/etc/php/conf.d/docker-php-ext-phalcon.ini && \
    cd .. && cd .. && \
    ## Remove all temp files ##
    rm -r \
        ${PWD}/mongo-php-driver \
        ${PWD}/v${PHALCON_VERSION}.tar.gz \
        ${PWD}/cphalcon-${PHALCON_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"]

สร้างไฟล์: ตั้งค่า PHP สำหรับ Phalcon 5.8 เบื้องต้น

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

สร้างไฟล์: ตั้งค่า 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;

}

สร้างไฟล์: ตั้งค่า 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/public;

    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/ @phalcon;
    }

    location @phalcon {
        rewrite ^(.*)$ /index.php?_url=$1;
    }

    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;

}

สร้างไฟล์: ตั้งค่า "รหัสผ่าน" ให้กับระบบ (บังคับให้ใส่รหัสผ่านก่อนเข้าใช้งาน PhpMyAdmin ทุกครั้ง)

Path: [skeleton]\pma.conf\.htpasswd

Language: Apache Config

admin:{SHA}fEqNCco3Yq9h5ZUglD3CZJT4lBs=

สร้างไฟล์: ตั้งค่า Apache2 สำหรับ PhpMyAdmin (รองรับความปลอดภัย)

Path: [skeleton]\pma.conf\default.conf

Language: Apache Config

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory "/var/www/html">
        AuthType Basic
        AuthName "Restricted Content"
        AuthUserFile /etc/apache2/.htpasswd
        Require valid-user
    </Directory>

</VirtualHost>

สร้างไฟล์: รัน Container บน Docker Engine (ตัวอย่าง)

Path: [skeleton]\docker-compose.yml

Language: Docker Engine

services:

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

  php83fpm_01:
    image: keptcode/php83phalcon:latest
    container_name: phalcon-php83-01
    build:
      context: ./build
      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:
      - php83fpm_network

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

  nginx_01:
    image: nginx:1.27.1-alpine
    container_name: phalcon-nginx-01
    working_dir: /usr/share/nginx/html
    restart: always
    environment:
      - VIRTUAL_HOST=localhost
      - VIRTUAL_PORT=80
      - NGINX_HOST=localhost
      - 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:
      - "php83fpm_01:php83fpm"
    ports:
      - "80:80"
    networks:
      - php83fpm_network

# ======================================================================
# Database
# ======================================================================

  db_mariadb_01:
    image: mariadb:11.5.2
    container_name: phalcon-mariadb-01
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=123456
      - MYSQL_PASSWORD=123456
      - VIRTUAL_PORT=3306
      - TERM=xterm
    volumes:
      - ./data/mariadb_01:/var/lib/mysql
    ports:
      - "3306:3306"
    networks:
      - php83fpm_network

  db_pma_01:
      image: phpmyadmin/phpmyadmin:5.2.1
      container_name: phalcon-pma-01
      restart: always
      environment:
        - PMA_ARBITRARY=1
        - PMA_HOST=mariadb
        - PMA_USER=root
        - PMA_PASSWORD=123456
        - MYSQL_ROOT_PASSWORD=123456
        - MYSQL_PASSWORD=123456
        - VIRTUAL_PORT=8080
        - TERM=xterm
      links:
        - "db_mariadb_01:mariadb"
      volumes:
        - ./pma.conf/.htpasswd:/etc/apache2/.htpasswd
        - ./pma.conf/default.conf:/etc/apache2/sites-available/000-default.conf
      ports:
        - "8080:80"
      networks:
        - php83fpm_network

networks:
  php83fpm_network:
    driver: bridge

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

Path: [skeleton]

Language: Bash

# รัน Docker เพื่อสร้าง Container ที่ต้องการ
$ docker compose up -d