PIMCORE เป็นระบบที่มากกว่าการเป็น Product Information Management (PIM) เพราะการที่จะมี PIM ได้นั้น ต้องมีการบริหารจัดการหลังบ้านอย่าง Digital Asset Management (DAM) และ Master Data Management (MDM) อีกทั้งยังสามารถเชื่อมต่อกับระบบอื่นๆ ได้โดยผ่าน API ไม่ว่าจะเป็น GraphQL หรือ REST API หรือแม้แต่จะพัฒนาต่อยอดด้วย PHP + Symfony

Pimcore นั้นเป็น Opensource และมี 2 editions หลัก คือ Community และ Enterprise สามารถศึกษาความแตกต่างได้จากบนหน้าเวปของ Pimcore

การติดตั้ง

อ้างอิงการติดตั้งจาก Pimcore Development Documentation และติดตั้งตัว Community edition

ในการติดตั้งนี้ใช้ personal preference จึงกล่าวอ้างเฉพาะระบบ หรือ software ที่ต้องใช้ ดังนี้

  • debian 11 bulleye
  • nginx
  • php 8.1

แบ่งการติดตั้งออกเป็น 2 ส่วน คือ ส่วนของ system และ application

System

เนื่องด้วยการทดสอบติดตั้งนั้นทำบน Raspberry Pi และ GCP จึงขอไม่ขอกล่าวถึงการติดตั้ง Debian ซึ่งสามารถอ่านได้จากบทความนี้ การติดตั้ง Debian แต่ขอกล่าวถึงการตั้งค่าพื้นฐานเบื้องต้น และการติดตั้ง software/tool ต่างๆ ดังนี้

  • เรื่องเวลาของระบบ แนะนำว่าควรตั้งค่าตาม timezone ที่อยู่ เพราะเวลาอ่าน log ต่างๆ จะได้ไม่ต้องมาแปลงเวลา

# echo Asia/Bangkok > /etc/timezone
# rm /etc/localtime
# ln -s /usr/share/zoneinfo/Asia/Bangkok /etc/localtime
.
  • ทำการ update/upgrade software package และ ติดตั้ง systems tools ต่างๆ

# apt update && apt upgrade -y
# apt install -y wget curl lsb-release ca-certificates apt-transport-https software-properties-common gnupg2 git unzip cron vim neofetch inxi htop
.
  • ติดตั้ง NGINX และ MariaDB

# apt install -y nginx mariadb-server
.
  • เนื่องด้วย PHP 8.1 นั้น ไม่มี package บน Debian repository ดังนั้นจึงต้องทำการเพิ่ม repo ของ sury.org ก่อน แล้วจึงทำการติดตั้ง PHP 8.1 พร้อมกับ library ที่ต้องใช้ใน Pimcore

# echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/sury-php.list

# wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

# apt update && sudo apt upgrade -y

# apt install -y php8.1-fpm php8.1-gd php8.1-mbstring php8.1-xml php8.1-exif php8.1-zip php8.1-curl php8.1-mysql php8.1-cli php8.1-intl php8.1-imagick php8.1-redis 
.
  • ทำการติดตั้ง Composer ซึ่งจะใช้ในการติดตั้ง Pimcore

# cd ~
# curl -sS https://getcomposer.org/installer -o composer-setup.php
# php composer-setup.php --install-dir=/usr/local/bin --filename=composer
.

Pimcore Community Edition

เมื่อเตรียมความพร้อมของระบบเป็นที่เรียบร้อย ก็มาเริ่มการติดตั้งส่วนของ Pimcore และการตั้งค่าระบบต่างๆ

  • เริ่มต้นด้วยการกำหนดว่าจะติดตั้งไว้ที่ directory ใดแล้วทำการ download source code ด้วย git ในกรณีนี้จะติดต้้งไว้ที่ /srv/pimcore
โครงสร้างไฟล์ระบบ อันนี้ขึ้นอยู่กับแต่ละคน แต่ละองค์กร บางที่อาจจะใช้ /var/www บางที่อาจจะใช้ /app/pimcore แล้วแต่ความชอบและมาตรฐานขององค์กร

# cd /srv
# git clone https://github.com/pimcore/pimcore.git
.
  • ทำการสร้าง database, user, password และ grant สิทธิ

# mysql
MariaDB [(none)]> CREATE DATABASE pimcore;
MariaDB [(none)]> CREATE USER 'pimcore'@'localhost' IDENTIFIED BY 'password';
MariaDB [(none)]> GRANT ALL ON pimcore.* TO 'pimcore'@'localhost';
.
  • ทำการติดตั้ง Pimcore project ด้วย composer หากต้องการติดตั้งระบบ demo ให้เปลี่ยนตรงคำว่า skeleton เป็น demo ก็จะได้ระบบพร้อมข้อมูลตัวอย่าง

    ในส่วนของ pimcore-install นั้น จะมีการสร้าง admin account พร้อม password และกำหนด database, user, password ที่สร้างไว้ในขั้นตอนด้านบน

# cd /srv/pimcore
# COMPOSER_MEMORY_LIMIT=-1 composer create-project pimcore/skeleton ./my-project
# cd ./my-project
# ./vendor/bin/pimcore-install
.
  • เมื่อสร้าง project เป็นที่เรียบร้อย จะได้ folder ชื่อตาม project ที่กำหนด ภายใต้ /srv/pimcore ให้ทำการกำหนดสิทธิให้กับ folder นั้น โดยให้ owner:group เป็น web service account ในกรณีที่ใช้ nginx จะเป็น www-data:www-data

# cd /srv/pimcore
# chown -R www-data:www-data my-project
.

# cd /etc/nginx/sites-available
# vi pimcore.conf
# cd ../sites-enable
# ln -s ../sites-enable/pimcore.conf
# nginx -t
# nginx -s reload
.

ตัวอย่างไฟล์ pimcore.conf ที่ให้เข้าผ่าน LAN IP ที่ port 5000

# mime types are already covered in nginx.conf
#include mime.types;
types {
    image/avif avif;
}

upstream php-pimcore10 {
    server unix:/var/run/php/php8.1-fpm.sock;
}

map $args $static_page_root {
    default                                 /var/tmp/pages;
    "~*(^|&)pimcore_editmode=true(&|$)"     /var/nonexistent;
    "~*(^|&)pimcore_preview=true(&|$)"      /var/nonexistent;
    "~*(^|&)pimcore_version=[^&]+(&|$)"     /var/nonexistent;
}

server {
    listen 5000;
    listen [::]:5000;
    server_name _;
    root /srv/pimcore/my-project/public;
    index index.php;
    
    # Filesize depending on your data
    client_max_body_size 100m;

    # It is recommended to seclude logs per virtual host
    access_log  /var/log/access.log;
    error_log   /var/log/error.log error;

    # Pimcore Head-Link Cache-Busting
    rewrite ^/cache-buster-(?:\d+)/(.*) /$1 last;

    # Stay secure
    #
    # a) don't allow PHP in folders allowing file uploads
    location ~* /var/assets/.*\.php(/|$) {
        return 404;
    }
    # b) Prevent clients from accessing hidden files (starting with a dot)
    # Access to `/.well-known/` is allowed.
    # https://www.mnot.net/blog/2010/04/07/well-known
    # https://tools.ietf.org/html/rfc5785
    location ~* /\.(?!well-known/) {
        deny all;
        log_not_found off;
        access_log off;
    }
    # c) Prevent clients from accessing to backup/config/source files
    location ~* (?:\.(?:bak|conf(ig)?|dist|fla|in[ci]|log|psd|sh|sql|sw[op])|~)$ {
        deny all;
    }

    # Some Admin Modules need this:
    # Database Admin, Server Info
    location ~* ^/admin/(adminer|external) {
        rewrite .* /index.php$is_args$args last;
    }
    
    # Thumbnails
    location ~* .*/(image|video)-thumb__\d+__.* {
        try_files /var/tmp/thumbnails$uri /index.php;
        expires 2w;
        access_log off;
        add_header Cache-Control "public";
    }

    # Assets
    # Still use a whitelist approach to prevent each and every missing asset to go through the PHP Engine.
    location ~* ^(?!/admin)(.+?)\.((?:css|js)(?:\.map)?|jpe?g|gif|png|svgz?|eps|exe|gz|zip|mp\d|m4a|ogg|ogv|webm|pdf|docx?|xlsx?|pptx?)$ {
        try_files /var/assets$uri $uri =404;
        expires 2w;
        access_log off;
        log_not_found off;
        add_header Cache-Control "public";
    }

    location / {
        error_page 404 /meta/404;
        try_files $static_page_root$uri.html $uri /index.php$is_args$args;
    }

    # Use this location when the installer has to be run
    # location ~ /(index|install)\.php(/|$) {
    #
    # Use this after initial install is done:
    location ~ ^/index\.php(/|$) {
        send_timeout 1800;
        fastcgi_read_timeout 1800;
        # regex to split $uri to $fastcgi_script_name and $fastcgi_path_info
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # Check that the PHP script exists before passing it
        try_files $fastcgi_script_name =404;
        # include fastcgi.conf if needed
        include fastcgi.conf;
        # Bypass the fact that try_files resets $fastcgi_path_info
        # see: http://trac.nginx.org/nginx/ticket/321
        set $path_info $fastcgi_path_info;
        fastcgi_param PATH_INFO $path_info;

        # Activate these, if using Symlinks and opcache
        # fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        # fastcgi_param DOCUMENT_ROOT $realpath_root;

        fastcgi_pass php-pimcore10;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/index.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

    # PHP-FPM Status and Ping
    location /fpm- {
        access_log off;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        location /fpm-status {
            allow 127.0.0.1;
            # add additional IP's or Ranges
            deny all;
            fastcgi_pass php-pimcore10;
        }
        location /fpm-ping {
            fastcgi_pass php-pimcore10;
        }
    }
    # nginx Status
    # see: https://nginx.org/en/docs/http/ngx_http_stub_status_module.html
    location /nginx-status {
        allow 127.0.0.1;
        deny all;
        access_log off;
        stub_status;
    }
}

http://your_lan_ip:5000/
http://your_lan_ip:5000/admin

ปัญหาที่พบ และการแก้ไข

  • ไม่สามารถ upload file ขนาดใหญ่เกิน 2 MB ได้ — ปัญหานี้เป็นเรื่องปกติของการติดตั้ง nginx และ php และใช้ค่าปริยายเริ่มต้น (default)

    ในส่วนของ nginx นั้น ตาม config ด้านบน ได้เปิดให้สามารถ upload file ขนาดได้ถึง 100m แล้ว ในส่วนของ PHP นั้น ให้ทำการแก้ไข parameter ที่ชื่อว่า post_max_size และ upload_max_filesize ซึ่งจะอยู่ใน /etc/php/8.1/fpm/php.ini
  • ใช้งานสักพัก ปล่อย idle browser จะ freeze หรือโหลดนานมาก — อันนี้ยังหาสาเหตุที่แน่ชัดไม่ได้ แต่ได้ทดลองปรับแก้ PHP ใน /etc/php/8.1/fpm/pool.d/www.conf

pm.max_children = 15
pm.start_servers = 2
pm.max_spare_servers = 10
pm.max_requests = 500
.

ศึกษาเพิ่มเติม

สามารถเข้าไปเรียนรู้เกี่ยวกับ Pimcore ได้ฟรีที่ https://pimcore.com/academy/ หากต้องการได้ certification ก็ต้องสมัครจ่ายรายปี

1 thought on “การติดตั้ง PIMCORE

Leave a Reply