Tạo tên miền ảo trên máy cục bộ (virtual host on localhost) bằng XAMPP

Step 1: hiệu chỉnh C:xampp\apache\conf\extra\httpd-vhosts.conf

Sử dụng giao thức http, port 80

  • Thay thế [email protected] thành email quản trị của bạn
  • Thay thế D:/dnpcuong/src/sunshine/public thành đường dẫn gốc (root path) đến thư mục source code của bạn
  • Thay thế sunshine.local thành tên miền bạn mong muốn, ví dụ: tenmiencuaban.local
<VirtualHost *:80>
    #Thay thế bằng email của Quản trị web của bạn
	ServerAdmin [email protected]
	
	#Thay thế bằng đường dẫn đến source của bạn
    DocumentRoot "D:/dnpcuong/src/sunshine/public"
	
	#Thay thế bằng tên trang web bạn mong muốn
    ServerName  sunshine.local
	
	#Thay thế bằng tên file log bạn mong muốn
    ErrorLog "logs/sunshine.local-error.log"
    CustomLog "logs/sunshine.local-access.log" common
	
	#Thay thế bằng đường dẫn đến source của bạn
    <Directory "D:/dnpcuong/src/sunshine/public">
        Options FollowSymLinks
        AllowOverride All
        DirectoryIndex index.php
        Require all granted
    </Directory>
</VirtualHost>

Sử dụng giao thức https, port 443

  • Thay thế [email protected] thành email quản trị của bạn
  • Thay thế D:/dnpcuong/src/sunshine/public thành đường dẫn gốc (root path) đến thư mục source code của bạn
  • Thay thế sunshine.local thành tên trang miền bạn mong muốn, ví dụ: tenmiencuaban.local
<VirtualHost *:443>
  #Thay thế bằng email của Quản trị web của bạn
  ServerAdmin [email protected]
  
  #Thay thế bằng đường dẫn đến source của bạn
  DocumentRoot "D:/dnpcuong/src/sunshine/public"
  
  #Thay thế bằng tên trang web bạn mong muốn
  ServerName sunshine.local

  #Thay thế bằng tên file log bạn mong muốn
  CustomLog "D:/dnpcuong/src/sunshine/logs/xampp.access.log" combined
  ErrorLog "D:/dnpcuong/src/sunshine/logs/xampp.error.log"

  #Bật chế độ bảo mật SLL và cung cấp chứng chỉ SSL
  SSLEngine on
  SSLCACertificateFile "C:/xampp/apache/conf/ssl.sunshine.local/ssl/ca_bundle.crt"
  SSLCertificateFile "C:/xampp/apache/conf/ssl.sunshine.local/ssl.crt/server.crt"
  SSLCertificateKeyFile "C:/xampp/apache/conf/ssl.sunshine.local/ssl.key/server.key"

  #Thay thế bằng đường dẫn đến source của bạn
  <Directory "D:/dnpcuong/src/sunshine/public">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
  </Directory>
</VirtualHost>

Step 2: hiệu chỉnh file C:\Windows\System32\drivers\etc\hosts

  • Lưu ý: để hiệu chỉnh file hệ thống, bạn cần quyền Administrator
  • Thay thế sunshine.local bằng tên miền bạn đã cấu hình trong Step 1
#Bổ sung địa chỉ IP ánh xạ với tên miền bạn mong muốn (Tương ứng với tên miền bạn đặt trong host ảo)
127.0.0.1		sunshine.local

Step 3: khởi động lại Apache của XAMPP

Step 4: truy cập đường dẫn sau để test lại

  • Truy cập trang web bằng tên miền của bạn đã cấu hình. Ví dụ:
    • Nếu bạn sử dụng giao thức http, port 80, truy cập địa chỉ: http://sunshine.local
    • Nếu bạn sử dụng giao thức https, port 443, truy cập địa chỉ: https://sunshine.local

Để test và vận hành thực tế trên Server, ta sẽ tạo tên miền ảo trên máy cục bộ như sau: