Hướng dẫn cấu hình Virtual Host trong XAMPP (Apache)

Mặc đinh trên Localhost Khi cài Xampp, bạn chỉ chạy được 1 site với url, dạng http://localhost/dirroot. Khi muốn chạy nhiều site chúng ta thường tạo các subfolder trong thư mục htdocs.  Với virtual host bạn có thể tùy  chỉnh được domain dưới dạng http://projects,  http://project.net, hoặc tạo subdomain http://app.project.net (Name-based Virtual Hosts). Một hướng khác, bạn có thể cấu hình cho mỗi domain một server, mỗi port một server khác nhau (IP-based Virtual Hosts).

Như ví dụ dưới đây, bạn có thể cấu hình RootDocument (htdocs) ở bất cứ đâu cho từng domain.

[code lang=”php”]
http://webtopviet.net với server ip 113.58.9.0
http://webtopviet.net:8080 với server ip 189.33.266.55
[/code]

 

TẠO VÀ CẤU HÌNH VITUAL HOST TRONG 3 BƯỚC ĐƠN GIẢN SAU ĐÂY:

Sau đây là các bước cấu hình tạo Virtual Host theo Name-based, nhiệm vụ của ta là đi cấu hình một domain mới projects trỏ về thư mục gốc của ứng dụng, ví dụ C:/var/www/projects

1: Cấu hình file hosts

Cấu hình thêm dòng sau để đảm bảo domain được trỏ về đúng host lưu trữ

[sourcecode language=”plain”]
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
# ::1 localhost
127.0.0.1 projects
[/sourcecode]

2: Cấu hình file httpd-vhosts.conf

Mở file C:/xampp/apache/conf/extra/httpd-vhosts.conf và sửa tương tự:

[sourcecode language=”plain”]
<virtualhost *>
DocumentRoot "C:/var/www/projects"
ServerName projects
</virtualhost>
[/sourcecode]

Hãy đảm bảo dòng này đã được uncomment
NameVirtualHost *:80

 

3: Cấu hình httpd.conf

Mở file xampp/apache/conf/httpd.conf cấu hình cho phép quyền truy cập thư mục làm RootDocument mới.

[sourcecode language=”plain”]
<Directory"C:/var/www/projects">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
[/sourcecode]

Note: Nếu có lỗi 403 Forbidden hãy thay đổi lại cấu hình như sau, cho phép toàn quyền truy cập thư mục RootDocument

[sourcecode language=”plain”]
<Directory "C:/var/www/projects">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
[/sourcecode]

Khởi động lại Apache

Hãy restart lại xampp apache để các cấu hình mới được thiết lập.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Website này sử dụng Akismet để hạn chế spam. Tìm hiểu bình luận của bạn được duyệt như thế nào.