Kotchasan Framework Documentation
คู่มือการติดตั้งและการใช้งาน (Deployment Guide)
คู่มือการติดตั้งและการใช้งาน (Deployment Guide)
คู่มือฉบับนี้จะแนะนำการติดตั้ง การกำหนดค่า และการปรับปรุงประสิทธิภาพสำหรับ Kotchasan Framework ในสภาพแวดล้อมการใช้งานจริง
สารบัญ
- ความต้องการของระบบ
- การติดตั้งบนเซิร์ฟเวอร์
- การกำหนดค่าฐานข้อมูล
- การกำหนดค่าเว็บเซิร์ฟเวอร์
- การตั้งค่าความปลอดภัย
- การเพิ่มประสิทธิภาพ
- การสำรองข้อมูลและการกู้คืน
- การตรวจสอบและแก้ไขปัญหา
ความต้องการของระบบ
ข้อกำหนดขั้นต่ำ
เซิร์ฟเวอร์:
- CPU: 1 Core 2.0 GHz
- RAM: 512 MB
- Storage: 1 GB SSD
- Bandwidth: 10 GB/เดือน
ซอฟต์แวร์:
- PHP 7.4 หรือสูงกว่า
- MySQL 5.7 หรือ MariaDB 10.2
- Apache 2.4 หรือ Nginx 1.16
- SSL Certificate (แนะนำ)ข้อกำหนดที่แนะนำ
เซิร์ฟเวอร์:
- CPU: 2 Cores 2.4 GHz หรือสูงกว่า
- RAM: 2 GB หรือมากกว่า
- Storage: 20 GB SSD
- Bandwidth: 100 GB/เดือน
ซอฟต์แวร์:
- PHP 8.1 หรือสูงกว่า
- MySQL 8.0 หรือ MariaDB 10.6
- Apache 2.4 หรือ Nginx 1.20
- SSL Certificate
- Redis (สำหรับ Cache)
- Elasticsearch (สำหรับการค้นหา)PHP Extensions ที่ต้องการ
จำเป็น:
- mbstring
- openssl
- PDO
- pdo_mysql
- curl
- gd
- zip
- xml
- json
แนะนำเพิ่มเติม:
- opcache
- redis
- memcached
- imagick
- intlการติดตั้งบนเซิร์ฟเวอร์
1. การติดตั้งบน Ubuntu/Debian
# อัปเดตแพ็กเกจ
sudo apt update && sudo apt upgrade -y
# ติดตั้ง Apache, MySQL, PHP
sudo apt install apache2 mysql-server php8.1 php8.1-mysql php8.1-cli php8.1-common php8.1-mbstring php8.1-gd php8.1-curl php8.1-zip php8.1-xml -y
# เปิดใช้งาน PHP modules
sudo a2enmod php8.1
sudo a2enmod rewrite
sudo a2enmod ssl
sudo a2enmod headers
# รีสตาร์ท Apache
sudo systemctl restart apache2
# เปิดใช้งาน services
sudo systemctl enable apache2
sudo systemctl enable mysql2. การติดตั้ง Composer
# ดาวน์โหลดและติดตั้ง Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer
# ตรวจสอบการติดตั้ง
composer --version3. การติดตั้ง Kotchasan Framework
# สร้างไดเรกทอรีโปรเจค
sudo mkdir -p /var/www/html/myproject
cd /var/www/html/myproject
# Clone หรือดาวน์โหลด Framework (แนะนำ)
git clone https://github.com/kotchasan/kotchasan.git .
# หรือใช้ Composer
composer create-project kotchasan/kotchasan .
# ตั้งค่าสิทธิ์
sudo chown -R www-data:www-data /var/www/html/myproject
sudo chmod -R 755 /var/www/html/myproject
sudo chmod -R 777 /var/www/html/myproject/datas
sudo chmod -R 777 /var/www/html/myproject/tmp4. การกำหนดค่า Virtual Host
# /etc/apache2/sites-available/myproject.conf
<VirtualHost *:80>
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/html/myproject
<Directory /var/www/html/myproject>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/myproject_error.log
CustomLog ${APACHE_LOG_DIR}/myproject_access.log combined
# Redirect to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>
<VirtualHost *:443>
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/html/myproject
SSLEngine on
SSLCertificateFile /etc/ssl/certs/yourdomain.crt
SSLCertificateKeyFile /etc/ssl/private/yourdomain.key
SSLCertificateChainFile /etc/ssl/certs/intermediate.crt
<Directory /var/www/html/myproject>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# Security Headers
Header always set X-Frame-Options DENY
Header always set X-Content-Type-Options nosniff
Header always set X-XSS-Protection "1; mode=block"
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
ErrorLog ${APACHE_LOG_DIR}/myproject_ssl_error.log
CustomLog ${APACHE_LOG_DIR}/myproject_ssl_access.log combined
</VirtualHost># เปิดใช้งาน site
sudo a2ensite myproject.conf
sudo systemctl reload apache2การกำหนดค่าฐานข้อมูล
1. การสร้างฐานข้อมูล
-- เข้าสู่ MySQL
mysql -u root -p
-- สร้างฐานข้อมูล
CREATE DATABASE kotchasan_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- สร้างผู้ใช้
CREATE USER 'kotchasan_user'@'localhost' IDENTIFIED BY 'strong_password_here';
-- ให้สิทธิ์
GRANT ALL PRIVILEGES ON kotchasan_db.* TO 'kotchasan_user'@'localhost';
FLUSH PRIVILEGES;
-- ออกจาก MySQL
EXIT;2. การกำหนดค่าในไฟล์ Config
// settings/config.php
return [
'version' => '1.0.0',
'web_title' => 'My Website',
'web_description' => 'Description of my website',
'timezone' => 'Asia/Bangkok',
// Kotchasan\Database Configuration
'db' => [
'hostname' => 'localhost',
'username' => 'kotchasan_user',
'password' => 'strong_password_here',
'dbname' => 'kotchasan_db',
'prefix' => 'app_'
],
// Cache Configuration
'cache' => [
'driver' => 'file', // file, redis, memcached
'path' => ROOT_PATH.'datas/cache/',
'prefix' => 'kotchasan_',
'default_ttl' => 3600
],
// Session Configuration
'session' => [
'name' => 'KOTCHASAN_SESSION',
'lifetime' => 3600,
'path' => '/',
'domain' => '',
'secure' => true,
'httponly' => true,
'samesite' => 'Strict'
],
// Security Configuration
'security' => [
'encryption_key' => 'your-32-character-secret-key-here',
'password_min_length' => 8,
'max_login_attempts' => 5,
'lockout_duration' => 900, // 15 minutes
'csrf_protection' => true
]
];3. การเพิ่มประสิทธิภาพฐานข้อมูล
-- my.cnf หรือ mysqld.cnf configuration
[mysqld]
# General Settings
default-storage-engine = InnoDB
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
# Performance Settings
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT
# Query Cache
query_cache_type = 1
query_cache_size = 128M
query_cache_limit = 32M
# Connection Settings
max_connections = 200
max_connect_errors = 10000
wait_timeout = 28800
interactive_timeout = 28800
# Slow Query Log
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2การกำหนดค่าเว็บเซิร์ฟเวอร์
1. การกำหนดค่า Apache
# .htaccess
RewriteEngine On
# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove www (หรือเพิ่ม www ตามต้องการ)
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Main Routing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_route=$1 [QSA,L]
# Security Rules
RewriteCond %{THE_REQUEST} [A-Z]{3,}\s/+([^\s]*)\s [NC]
RewriteRule ^(.*)$ - [F,L]
# Block sensitive files
<FilesMatch "\.(htaccess|htpasswd|ini|log|sh|sql|conf)$">
Require all denied
</FilesMatch>
# Block access to includes directory
RedirectMatch 403 ^/includes/.*$
RedirectMatch 403 ^/settings/.*$
RedirectMatch 403 ^/Kotchasan/.*$
# Security Headers
<IfModule mod_headers.c>
Header always set X-Frame-Options "DENY"
Header always set X-Content-Type-Options "nosniff"
Header always set X-XSS-Protection "1; mode=block"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"
</IfModule>
# Compression
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>
# Caching
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>2. การกำหนดค่า Nginx
# /etc/nginx/sites-available/myproject
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://yourdomain.com$request_uri;
}
server {
listen 443 ssl http2;
server_name www.yourdomain.com;
return 301 https://yourdomain.com$request_uri;
ssl_certificate /etc/ssl/certs/yourdomain.crt;
ssl_certificate_key /etc/ssl/private/yourdomain.key;
}
server {
listen 443 ssl http2;
server_name yourdomain.com;
root /var/www/html/myproject;
index index.php index.html;
# SSL Configuration
ssl_certificate /etc/ssl/certs/yourdomain.crt;
ssl_certificate_key /etc/ssl/private/yourdomain.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers off;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
# Security Headers
add_header X-Frame-Options "DENY" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
# Gzip Compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
# Main location
location / {
try_files $uri $uri/ @rewrite;
}
location @rewrite {
rewrite ^(.+)$ /index.php?_route=$1 last;
}
# PHP handler
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
}
# Static files caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf|zip)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
# Block sensitive files
location ~* \.(htaccess|htpasswd|ini|log|sh|sql|conf)$ {
deny all;
}
location /settings/ { deny all; }
location /Kotchasan/ { deny all; }
location /includes/ { deny all; }
}การตั้งค่าความปลอดภัย
1. การป้องกัน Brute Force
// modules/security/models/bruteforce.php
class BruteForceProtection
{
private $maxAttempts = 5;
$lockoutTime = 90; // 0; // 15 minutes
public function checkAttempts($ip)
{
$attempts = $this->getAttempts($ip);
if ($attempts >= $this->maxAttempts) {
$lastAttempt = $this->getLastAttemptTime($ip);
if (time() - $lastAttempt < $this->lockoutTime) {
return false; // Still locked out
} else {
$this->clearAttempts($ip); // Reset attempts
}
}
return true;
}
public function recordFailedAttempt($ip)
{
$this->db()->createQuery()
->insert('login_attempts', [
'ip_address' => $ip,
'attempted_at' => date('Y-m-d H:i:s'),
'success' => 0
])
->execute();
}
public function recordSuccessfulLogin($ip)
{
$this->clearAttempts($ip);
$this->db()->createQuery()
->insert('login_attempts', [
'ip_address' => $ip,
'attempted_at' => date('Y-m-d H:i:s'),
'success' => 1
])
->execute();
}
private function getAttempts($ip)
{
return $this->db()->createQuery()
->from('login_attempts')
->where(['ip_address', $ip])
->andWhere(['success', 0])
->andWhere(['attempted_at', '>', date('Y-m-d H:i:s', time() - $this->lockoutTime)])
->count();
}
}2. การตั้งค่า CSRF Protection
// Kotchasan/Csrf.php
class Csrf
{
public static function generateToken()
{
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
return $_SESSION['csrf_token'];
}
public static function validateToken($token)
{
if (empty($_SESSION['csrf_token']) || empty($token)) {
return false;
}
return hash_equals($_SESSION['csrf_token'], $token);
}
public static function getTokenField()
{
$token = self::generateToken();
return '<input type="hidden" name="_token" value="'.$token.'">';
}
}3. การเข้ารหัสข้อมูลสำคัญ
// Kotchasan/Encryption.php
class Encryption
{
private $key;
private $cipher = 'AES-256-CBC';
public function __construct($key)
{
$this->key = hash('sha256', $key, true);
}
public function encrypt($data)
{
$iv = random_bytes(16);
$encrypted = openssl_encrypt($data, $this->cipher, $this->key, 0, $iv);
return base64_encode($iv . $encrypted);
}
public function decrypt($data)
{
$data = base64_decode($data);
$iv = substr($data, 0, 16);
$encrypted = substr($data, 16);
return openssl_decrypt($encrypted, $this->cipher, $this->key, 0, $iv);
}
}การเพิ่มประสิทธิภาพ
1. การใช้ Redis Cache
// Kotchasan/Cache/Redis.php
class RedisCache
{
private $redis;
private $prefix;
public function __construct($config)
{
$this->redis = new Redis();
$this->redis->connect($config['host'], $config['port']);
if (!empty($config['password'])) {
$this->redis->auth($config['password']);
}
$this->prefix = $config['prefix'] ?? 'kotchasan:';
}
public function get($key)
{
$data = $this->redis->get($this->prefix . $key);
return $data ? unserialize($data) : null;
}
$ttl = 360; // 0)
{
return $this->redis->setex($this->prefix . $key, $ttl, serialize($value));
}
public function delete($key)
{
return $this->redis->del($this->prefix . $key);
}
public function flush()
{
$keys = $this->redis->keys($this->prefix . '*');
if (!empty($keys)) {
return $this->redis->del($keys);
}
return true;
}
}2. การใช้ OPcache
; php.ini configuration
[opcache]
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.max_wasted_percentage=5
opcache.use_cwd=1
opcache.validate_timestamps=0
opcache.revalidate_freq=0
opcache.save_comments=1
opcache.fast_shutdown=13. การบีบอัดและการแคช
// modules/system/controllers/assets.php
class AssetsController
{
public function css($files)
{
$cacheFile = 'datas/cache/css/' . md5(implode(',', $files)) . '.css';
if (!file_exists($cacheFile) || $this->isModified($files, $cacheFile)) {
$content = '';
foreach ($files as $file) {
if (file_exists($file)) {
$content .= file_get_contents($file) . "\n";
}
}
// Minify CSS
$content = $this->minifyCSS($content);
// Save to cache
file_put_contents($cacheFile, $content);
}
header('Content-Type: text/css');
header('Cache-Control: public, max-age=31536000');
readfile($cacheFile);
}
private function minifyCSS($css)
{
// Remove comments
$css = preg_replace('!/\[^]\+([^/][^]\+)/!', '', $css);
// Remove whitespace
$css = str_replace(["\r\n", "\r", "\n", "\t", ' ', ' ', ' '], '', $css);
return $css;
}
}การสำรองข้อมูลและการกู้คืน
1. สคริปต์การสำรองข้อมูลอัตโนมัติ
#!/bin/bash
# backup.sh
# Configuration
DB_NAME="kotchasan_db"
DB_USER="kotchasan_user"
DB_PASS="strong_password_here"
BACKUP_DIR="/backup/kotchasan"
WEB_DIR="/var/www/html/myproject"
DATE=$(date +%Y%m%d_%H%M%S)
# Create backup directory
mkdir -p $BACKUP_DIR
# Database backup
mysqldump -u$DB_USER -p$DB_PASS $DB_NAME | gzip > $BACKUP_DIR/db_$DATE.sql.gz
# Files backup
tar -czf $BACKUP_DIR/files_$DATE.tar.gz -C $WEB_DIR datas/ uploads/ settings/
# Keep only last 7 days of backups
find $BACKUP_DIR -name "*.gz" -mtime +7 -delete
# Log backup
echo "$(date): Backup completed successfully" >> $BACKUP_DIR/backup.log2. การตั้งค่า Cron Job
# เปิดแก้ไข crontab
crontab -e
# เพิ่มบรรทัดต่อไปนี้เพื่อสำรองข้อมูลทุกวันเวลา 2:00 น.
0 2 * * * /path/to/backup.sh
# สำรองข้อมูลฐานข้อมูลทุก 6 ชั่วโมง
0 */6 * * * mysqldump -u kotchasan_user -p'password' kotchasan_db | gzip > /backup/db_$(date +\%Y\%m\%d_\%H\%M\%S).sql.gz3. การกู้คืนข้อมูล
#!/bin/bash
# restore.sh
# Configuration
DB_NAME="kotchasan_db"
DB_USER="kotchasan_user"
DB_PASS="strong_password_here"
WEB_DIR="/var/www/html/myproject"
# Restore database
echo "Restoring database..."
gunzip < $1 | mysql -u$DB_USER -p$DB_PASS $DB_NAME
# Restore files
echo "Restoring files..."
tar -xzf $2 -C $WEB_DIR
# Fix permissions
chown -R www-data:www-data $WEB_DIR
chmod -R 755 $WEB_DIR
chmod -R 777 $WEB_DIR/datas
chmod -R 777 $WEB_DIR/tmp
echo "Restore completed successfully"การตรวจสอบและแก้ไขปัญหา
1. การเปิดใช้งาน Debug Mode
// settings/config.php
return [
'debug' => true, // เปิดเฉพาะในระหว่างพัฒนา
'log_level' => 'debug',
'log_file' => ROOT_PATH.'datas/logs/app.log',
// Error reporting
'error_reporting' => E_ALL,
'display_errors' => true, // ปิดใน production
];2. การติดตาม Log Files
# ดู Apache error log
sudo tail -f /var/log/apache2/myproject_error.log
# ดู MySQL slow query log
sudo tail -f /var/log/mysql/slow.log
# ดู Application log
tail -f /var/www/html/myproject/datas/logs/app.log
# ดู System log
sudo tail -f /var/log/syslog3. การตรวจสอบประสิทธิภาพ
// modules/system/performance.php
class PerformanceMonitor
{
private $startTime;
private $startMemory;
public function start()
{
$this->startTime = microtime(true);
$this->startMemory = memory_get_usage();
}
public function end()
{
$endTime = microtime(true);
$endMemory = memory_get_usage();
$executionTime = $endTime - $this->startTime;
$memoryUsage = $endMemory - $this->startMemory;
error_log("Execution Time: " . number_format($executionTime, 4) . " seconds");
error_log("Memory Usage: " . number_format($memoryUsage / 1024 / 1024, 2) . " MB");
error_log("Peak Memory: " . number_format(memory_get_peak_usage() / 1024 / 1024, 2) . " MB");
}
}4. การทดสอบการทำงาน
# ทดสอบการเชื่อมต่อฐานข้อมูล
mysql -u kotchasan_user -p kotchasan_db -e "SELECT 1"
# ทดสอบ PHP configuration
php -i | grep -E "(memory_limit|max_execution_time|upload_max_filesize)"
# ทดสอบการเขียนไฟล์
touch /var/www/html/myproject/datas/test.txt && rm /var/www/html/myproject/datas/test.txt
# ทดสอบ SSL certificate
openssl s_client -connect yourdomain.com:443 -servername yourdomain.com
# ทดสอบ page load speed
curl -w "@curl-format.txt" -o /dev/null -s "https://yourdomain.com"5. การแก้ไขปัญหาที่พบบ่อย
ปัญหา: 500 Internal Server Error
แก้ไข:
- ตรวจสอบ error log
- ตรวจสอบสิทธิ์ไฟล์
- ตรวจสอบ .htaccess syntax
ปัญหา: Database connection failed
แก้ไข:
- ตรวจสอบ database credentials
- ตรวจสอบ MySQL service status
- ตรวจสอบ firewall settings
ปัญหา: หน้าเว็บโหลดช้า
แก้ไข:
- เปิดใช้งาน caching
- เพิ่มประสิทธิภาพ database queries
- ใช้ CDN สำหรับ static files
ปัญหา: Session ไม่ทำงาน
แก้ไข:
- ตรวจสอบ session configuration
- ตรวจสอบ cookie settings
- ตรวจสอบสิทธิ์ session directoryสรุป
คู่มือนี้ครอบคลุมขั้นตอนการติดตั้งและการกำหนดค่า Kotchasan Framework สำหรับการใช้งานจริงอย่างสมบูรณ์ ตั้งแต่การติดตั้งเซิร์ฟเวอร์ การตั้งค่าความปลอดภัย ไปจนถึงการสำรองข้อมูลและการแก้ไขปัญหา
จุดสำคัญที่ต้องจำ:
- ความปลอดภัย: ใช้ HTTPS, อัปเดต software เสมอ, ตั้งค่า firewall
- ประสิทธิภาพ: ใช้ caching, เพิ่มประสิทธิภาพ database, บีบอัดไฟล์
- การสำรองข้อมูล: สำรองข้อมูลเป็นประจำ, ทดสอบการกู้คืน
- การตรวจสอบ: ติดตาม logs, ตรวจสอบประสิทธิภาพ, แก้ไขปัญหาอย่างรวดเร็ว
การปฏิบัติตามคู่มือนี้จะช่วยให้ระบบของคุณมีความเสถียร ปลอดภัย และมีประสิทธิภาพสูง