Apache: ทำ Virtual Host สำหรับ test

Byphunsanit

Apache: ทำ Virtual Host สำหรับ test

เพราะว่าต้องแยกส่วนของ frontend และ backend ไว้คนละ server เพื่อความสดวกและปลอดภัย โดยทั้งคู่จะติดต่อกันผ่านทาง api แต่เวลาเขียนมีคอมพิวเตอร์แค่เครื่องเดียว ตามปกติสามารถทำได้โดยแยกงานไว้คนละ folder และเพื่อให้เหมือนของจริงยิ่งขึ้นก็ใข้วิธีทำ Virtual Host แยกออกเป็น 2 เซิร์ฟเวอร์ (ถึงจะแค่เครื่องจำลองก็เถอะ)

ทำได้โดยการเปิดไฟล์ C:\xampp\apache\conf\extra\httpd-vhosts.conf และเพิ่ม

## backend
Listen 81
<VirtualHost *:81>
 
    CustomLog "D:\xampp\htdocs\cms_backend\logs\apacheAccess.txt" common
    ErrorLog "D:\xampp\htdocs\cms_backend\logs\apacheError.txt"
 
    php_flag display_errors on
    php_flag log_errors on
    php_value error_log "D:\xampp\htdocs\cms_backend\logs\phpError.txt"
    php_value error_reporting 2147483647
 
    DocumentRoot "D:\xampp\htdocs\cms_backend\www"
 
    ServerAdmin [email protected]
    ServerName backend.localhost
 
</VirtualHost>
 
## frontend
Listen 82
<VirtualHost *:82>
 
    CustomLog "D:\xampp\htdocs\cms_frontend\logs\apacheAccess.txt" common
    ErrorLog "D:\xampp\htdocs\cms_frontend\logs\apacheError.txt"
 
    php_flag display_errors on
    php_flag log_errors on
    php_value error_log "D:\xampp\htdocs\cms_frontend\logs\phpError.txt"
    php_value error_reporting 2147483647
 
    DocumentRoot "D:\xampp\htdocs\cms_frontend\www"
 
    ServerAdmin [email protected]
    ServerName frontend.localhost
 
</VirtualHost>

อธิบาย

Listen
คือหมายเลขพอร์ตที่จะให้รอรับ request อย่าตั้งให้ชนกับตัวอื่น
CustomLog, ErrorLog
คือไฟล์ log ที่แยกออกมาของแต่ละ virtual host โดยสามารถอ่านคู่มือได้จาก Log Files
php_flag, php_value
คือ การปรับแต่ง php เป็นพิเศษสำหรับ server ตัวนี้เท่านั้น
DocumentRoot
คือ folder ที่ไว้เก็บ php ที่เราตั้งใจแยกไว้ เป็น code ชุดเดียวกับที่เราจะเอาขึ้น server จริง
ServerAdmin, ServerName
คือ ข้อมูล server แต่ละตัว

ทดสอบโดย restart apache ใหม่และเปิดเว็บ

About the author

phunsanit administrator