PHP Hosting คืออะไร เลือก Version ไหน เพิ่ม Performance อย่างไร
PHP Hosting explained: choosing PHP versions, OPcache, PHP-FPM, and performance optimization.
สารบัญ
PHP คืออะไร
PHP (Hypertext Preprocessor) คือภาษาโปรแกรม Server-side ที่ใช้สร้างเว็บไซต์แบบ Dynamic มากกว่า 77% ของเว็บที่รู้ภาษา Server-side ทำงานด้วย PHP รวมถึง WordPress, Drupal, Joomla, Magento, Laravel และอีกมากมาย
PHP (Hypertext Preprocessor) is a server-side programming language for dynamic websites. Over 77% of server-side websites use PHP, including WordPress, Drupal, Joomla, Magento, Laravel, and many more.
- WordPress, Drupal, Joomla: ทำงานบน PHP
- Laravel, Symfony: PHP Framework ยอดนิยม
- PHP: ฟรี Open Source
- ทำงานบน Linux หรือ Windows
- ส่วนใหญ่ใช้กับ MySQL/MariaDB Database
PHP Version ที่ควรใช้
PHP 8.2 หรือ 8.3 แนะนำในปี 2024-2025 เร็วกว่า PHP 7.4 ถึง 3 เท่า และมีฟีเจอร์ใหม่มากมาย PHP 7.4 End of Life แล้ว ไม่ได้รับ Security Update ควร Upgrade PHP 8.0, 8.1 ยังได้รับ Security Support แต่ 8.2/8.3 ดีที่สุด
PHP 8.2 or 8.3 is recommended for 2024-2025 — 3× faster than PHP 7.4 with many new features. PHP 7.4 is End of Life with no security updates, so upgrade. PHP 8.0/8.1 still receive security support, but 8.2/8.3 is best.
- PHP 8.3: เร็วสุด ล่าสุด Active Support
- PHP 8.2: แนะนำ Stable + Security Support
- PHP 7.4: EOL แล้ว ไม่มี Security Update
- ตรวจสอบ Plugin/CMS รองรับ PHP 8.x ก่อน Upgrade
- JIT Compiler: PHP 8.0+ เร็วขึ้นอีก
OPcache คืออะไรและเปิดอย่างไร
OPcache เป็น Extension ใน PHP ที่ Cache Compiled PHP Bytecode ไว้ใน Memory แทนที่จะ Parse และ Compile ใหม่ทุก Request ทำให้เร็วขึ้น 3-5 เท่าสำหรับ PHP Application ทั่วไป ส่วนใหญ่ Hosting ดีๆ เปิด OPcache ให้อัตโนมัติ
OPcache caches compiled PHP bytecode in memory instead of parsing and compiling on every request — making PHP 3-5× faster. Good hosting enables OPcache automatically.
- เปิด OPcache ใน php.ini: opcache.enable=1
- opcache.memory_consumption=256 (256MB สำหรับ WordPress)
- opcache.max_accelerated_files=10000
- ตรวจสอบ: phpinfo() เช็ค OPcache enabled
- เร็วขึ้น 3-5 เท่าเมื่อเปิด OPcache
PHP-FPM คืออะไร
PHP-FPM (FastCGI Process Manager) คือวิธีรัน PHP ที่ดีกว่า mod_php ใน Apache ทำงานเป็น Process Pool แยก ไม่ผูกกับ Web Server รองรับ Connection พร้อมกันได้มากกว่า ใช้ Memory ฉลาดกว่า Nginx + PHP-FPM คือ Combo ที่นิยมที่สุด
PHP-FPM (FastCGI Process Manager) is a better PHP execution method than Apache's mod_php. It runs as a separate process pool, not tied to the web server, handling more concurrent connections more efficiently. Nginx + PHP-FPM is the most popular combination.
- PHP-FPM: แยก Process จาก Web Server
- Nginx + PHP-FPM: เร็วกว่า Apache + mod_php
- Process Pool: จัดการ Worker อัตโนมัติ
- ใช้ Memory น้อยกว่า mod_php
- Hosting ส่วนใหญ่ใช้ PHP-FPM อยู่แล้ว
PHP Memory Limit และ Timeout
Memory Limit กำหนดว่า PHP Script ใช้ RAM ได้มากแค่ไหน WordPress ต้องการอย่างน้อย 128MB แนะนำ 256MB WooCommerce หรือ Plugin หนักๆ ต้องการ 512MB+ Max Execution Time กำหนด Timeout ของ Script อย่าตั้งสูงเกินจำเป็น
Memory Limit controls how much RAM a PHP script can use. WordPress needs minimum 128MB; 256MB is recommended. WooCommerce or heavy plugins need 512MB+. Max Execution Time defines the script timeout — don't set it higher than necessary.
- WordPress: memory_limit=256M ดีที่สุด
- WooCommerce: 512M หรือสูงกว่า
- upload_max_filesize: ขนาดไฟล์สูงสุดที่ Upload ได้
- max_execution_time: 120-300 วินาทีสำหรับ E-commerce
- post_max_size: ต้อง ≥ upload_max_filesize
PHP Extension ที่ต้องมี
Extension สำคัญ: GD/Imagick (รูปภาพ), cURL (HTTP Request), MySQLi/PDO (Database), mbstring (Unicode), openssl (SSL), zip (ไฟล์ Zip), XML/SimpleXML (XML), intl (Internationalization) WordPress ต้องการ Extension เหล่านี้เกือบทั้งหมด
Essential extensions: GD/Imagick (images), cURL (HTTP requests), MySQLi/PDO (database), mbstring (Unicode), openssl (SSL), zip (file compression), XML/SimpleXML (XML), intl (internationalization). WordPress requires almost all of these.
- GD หรือ Imagick: สร้างและจัดการรูปภาพ
- cURL: เรียก API ภายนอก
- MySQLi/PDO: เชื่อมต่อ MySQL Database
- mbstring: รองรับ UTF-8 และ Unicode
- openssl: SSL/HTTPS Functionality
Error Reporting บน Production
บน Production Server ห้ามแสดง Error ให้ผู้ใช้เห็น เพราะแสดง Path, Variable และข้อมูล Server ที่ Hacker อาจใช้ประโยชน์ ใน php.ini ตั้ง display_errors=Off log_errors=On error_log=/path/to/error.log
On production servers, never display errors to users — they reveal paths, variables, and server information hackers can exploit. In php.ini: display_errors=Off, log_errors=On, error_log=/path/to/error.log.
- Production: display_errors=Off เสมอ
- log_errors=On: ส่ง Error ไป Log File แทน
- Error Log: ตรวจสอบ Log สม่ำเสมอ
- Development: display_errors=On ได้
- WordPress: WP_DEBUG=false บน Production
เพิ่ม Performance PHP Application
ปัจจัยที่ส่งผลต่อ Performance PHP มากที่สุดคือ PHP Version ใหม่, OPcache, PHP-FPM, MySQL Optimization (Query Cache, Index), Redis Object Cache สำหรับ WordPress และ CDN สำหรับ Static File
Key PHP performance factors: newer PHP version, OPcache, PHP-FPM, MySQL optimization (query cache, indexes), Redis Object Cache for WordPress, and CDN for static files.
- PHP 8.x: เร็วกว่า PHP 7.x 3 เท่า
- OPcache: ลด PHP Parse Time 80-90%
- PHP-FPM Tuning: pm=dynamic ปรับ Worker ตามโหลด
- MySQL Query Optimization: Add Index ที่เหมาะสม
- Redis Object Cache: WordPress เร็วขึ้นอีก 2-5 เท่า
วิธีเปลี่ยน PHP Version บน Hosting
ส่วนใหญ่เปลี่ยนได้ง่ายผ่าน cPanel หรือ DirectAdmin ที่ Software Section → PHP Version ควรทดสอบบน Staging ก่อน ตรวจสอบว่า Plugin/Theme ทั้งหมด Compatible กับ PHP Version ใหม่ก่อน Switch Production
Most hosting allows easy switching through cPanel or DirectAdmin → Software → PHP Version. Test on staging first. Verify all plugins/themes are compatible with the new PHP version before switching production.
- cPanel: Software → PHP Version
- DirectAdmin: Extra Features → PHP Version
- ทดสอบบน Staging ก่อน Switch Production
- ตรวจสอบ Plugin Compatibility กับ PHP เวอร์ชันใหม่
- WordPress Health Check: ตรวจสอบหลัง Switch