Cách cài đặt Virtual Host trên nhiều cổng

XAMPP mặc định cổng 80 và 443. Nếu bạn có nhiều dự án và muốn cài đặt dự án của bạn tới một cổng khác trên XAMPP, bạn có thể sử dụng “Virtual Hosts”. Chức năng này cho phép bạn có thể cài đặt các host ảo (virtual host) trên XAMPP ở các cổng khác nhau.

I. Trên Windows

1. Cài đặt thêm cổng

Giả sử bạn cài đặt XAMPP mặc định ở ổ C, bạn tìm file  C:\xampp\apache\conf\httpd.conf và thêm cổng lắng nghe, ví dụ tôi thêm cổng 84


Listen 80
Listen 84 // thêm cổng mới

2. Cập nhật file httpd-vhosts.conf

Truy cập vào file C:\xampp\apache\conf\extra\httpd-vhosts.conf để thêm cấu hình cho host.


<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
ServerName localhost
ErrorLog "logs/dummy-host2.example.com-error_log"
CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>

<VirtualHost *:84>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/aic"
ServerName demo.com
ErrorLog "logs/dummy-host2.example.com-error_log"
CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>

3. Cập nhật file hosts

Truy cập vào file C:\Windows\System32\drivers\etc\hosts để cập nhật file hosts, và thêm cổng


# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 127.0.0.1:84 // host với cổng port.

4. Khởi động lại Apache và chạy thử địa chỉ demo.com trên trình duyệt .

II. Trên MacOS

1. Kích hoạt VirtualHost

Bạn tìm đường đến file httpd.conf trog thư mục Applications => XAMPP => xamppfiles => etc tìm đến phần Listen 80 và thêm một cổng nữa, ví dụ như cổng 84.


Listen 80
Listen 84 // thêm cổng mới

Sau đó tìm đến dòng Virtual hosts để kích hoạt VirtualHost hoặt động bằng cách bỏ chú thích (bỏ dấu #) như hình dưới:


# Virtual hosts
Include etc/extra/httpd-vhosts.conf

2. Tạo VirtualHost mới

Bạn tìm đường đến file httpd-vhosts.conf trog thư mục Applications => XAMPP => xamppfiles => etc =>extra, cập nhật nội dung giống như bên dưới, trong đó cổng 80 trỏ đến thư mục mặc định của htdocs, còn cổng 88 trỏ đến thư mục mywebsite trong htdocs, domain đặt tên là demo.com.


<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
ServerName localhost
ErrorLog "logs/dummy-host2.example.com-error_log"
CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>

<VirtualHost *:84>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/mywebsite"
ServerName demo.com
ErrorLog "logs/dummy-host2.example.com-error_log"
CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>

3. Chỉnh sửa tập tin host


# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 demo.com

4. Khởi động lại XAMPP và chạy thử địa chỉ demo.com trên trình duyệt

Có thể bạn sẽ thích…

Trả lời

EnglishVietnamese