PlusMagi's Blog By Pitt Phunsanit

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 phunsanit@hotmail.com
  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 phunsanit@hotmail.com
  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 ใหม่และเปิดเว็บ

Exit mobile version