วิธีทำ Virtual Host บน XAMPP ในระบบปฏิบัติการ Windows

Virtual Host คืออะไร ?

Virtual Host คือ การทำให้ Server เครื่องเดียวสามารถให้บริการได้มากกว่า 1 เว็บไซต์ ซึ่งโดยปกติสามารถให้บริการได้เพียงเว็บไซต์เดียว การทำ Virtual Host สามารถทำได้หลายวิธี ดังนี้

  1. Name-based Virtual Host - การอ้างอิงโดยใช้ Domain Name หรือ Host Name
  2. Port-based Virtual Host - การอ้างอิงโดยใช้ Port Number
  3. IP-based Virutal Host - การอ้างอิงโดยใช้ IP Address
  4. การใช้ทั้ง Port-based ร่วมกับ IP-based
  5. การใช้ทั้ง Name-based ร่วมกับ IP-based

เริ่มต้นการทำ Virtual Host แบบ Step By Step

การทำ Virtual Host บน XAMPP ในระบบปฏิบัติการ Window  ต้องแก้ไขไฟล์ดังนี้

  • httpd-vhosts.conf
  • hosts

ขั้นตอนที่ 1 แก้ไข httpd-vhosts.conf

ไปที่ C:\xampp\apache\conf\extra\httpd-vhosts.conf เปิดไฟล์ขึ้นมา หาคำว่า NameVirtualHost แก้ไขให้เป็น

NameVirtualHost *:80

กำหนด Virtual Host ของ localhost เพื่อให้สามารถใช้ localhost ได้เหมือนเดิม

<VirtualHost *:80>
    DocumentRoot c:/xampp/htdocs
    ServerName localhost
    <Directory c:/xampp/htdocs>
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order Deny,Allow
        Allow from all
    </Directory>
</VirtualHost>

 

ต่อมาทำการเพิ่มชื่อ Domain Name หรือ Host Name เพื่ออ้างอิงไปยัง Path ที่เก็บเว็บไซต์ที่ต้องการและกำหนดสิทธิ์การเข้าถึง

<VirtualHost *:80>
    DocumentRoot c:/xampp/htdocs/share/public
    ServerName share.olanlab.com
    <Directory c:/xampp/htdocs/share/public>
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order Deny,Allow
        Allow from all
    </Directory>
</VirtualHost>

ตัวอย่างด้านบน

Path ที่เก็บเว็บไซต์เป็น - c:/xamp/htdocs/share/public

Domain Name เป็น     - share.olanlab.com

 

ขั้นตอนที่ 2 แก้ไข hosts 

ไปที่ C:\Windows\System32\drivers\etc\hosts เปิดไฟล์ขึ้นมาทำการเพิ่ม Host Name และ IP Address  เพื่อให้เครื่องของเรารู้จักชื่อ Host Name นี้ว่าจะต้องไปที่ IP Adress อะไร

127.0.0.1       localhost
127.0.0.1       share.olanlab.com

*ไฟล์ hosts ทำหน้าที่เก็บข้อมูล Host Name กับ IP Address ที่ตรงกัน 

 

ขั้นตอนที่ 3 ทำการ Restart Apache

ขั้นตอนสุดท้ายคือการ Restart Apache เพื่อให้ Apache ทำการโหลดค่าที่เรากำหนดใน httpd-vhosts.conf ใหม่เท่านี้ก็สามารถใช้ Virtual Host บน XAMPP ในระบบปฏิบัติการ Window ได้แล้วครับ

  • 30 Users Found This Useful
Was this answer helpful?

Related Articles

Configuring Virtual Hosts on Xampp – How to Run Multiple Domains

1. add : httpd-vhosts.conf C:\xampp\apache\conf\extra\httpd-vhosts.conf <VirtualHost...

New xampp security concept: Access Forbidden Error 403 - Windows - phpMyAdmin

ขั้นตอน config XAMPP ให้สามารถเข้าถึงจากเครื่องอื่นได้Access forbidden! New XAMPP security...

Changing the maximum upload size in php.ini

Error text: file_name" has failed to upload due to an error - This file exceeds the maximum...

วิธีติดตั้ง SSL Certificate บน XAMPP Web Server

ขั้นตอนการติดตั้ง SSL Certificate เตรียมไฟล์ดังต่อไปนี้ให้พร้อม 1.certificate file - server.crt...