Apache Virtual Host là một tính năng của máy chủ web Apache cho phép bạn chạy nhiều trang web trên cùng một máy chủ vật lý. Mỗi Virtual Host có thể có cấu hình riêng biệt như tên miền, thư mục gốc, cổng, và địa chỉ IP. Điều này giúp bạn dễ dàng quản lý nhiều trang web trên cùng một máy chủ, cho phép các trang web hoạt động độc lập và bảo mật tốt hơn. Virtual Host có thể được cấu hình để hoạt động dựa trên tên miền (Name-based Virtual Host) hoặc địa chỉ IP (IP-based Virtual Host).
Cài đặt Virtual Host :
cd /etc/apache2/sites-available
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerName mysite.com
ServerAlias www.mysite.com
# Index file and Document Root (where the public files are located)
DocumentRoot /home/mysite/
<Directory /home/mysite/>
DirectoryIndex index.php
Options -Indexes -ExecCGI +FollowSymLinks -SymLinksIfOwnerMatch
AllowOverride All
Require all granted
allow from all
</Directory>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
Header set Access-Control-Allow-Headers "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type"
Header set Access-Control-Allow-Credentials "true"
#set version php apply cho website
<FilesMatch .php$>
SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://thanhnh.id.vn/"
</FilesMatch>
# Log file locations
LogLevel warn
ErrorLog /home/mysite/logs/error.log
CustomLog /home/mysite/logs/access.log combined
</VirtualHost>
Giải thích
1. Indexes
Mô tả: Khi Indexes được bật, nếu không có tệp index (như index.html), Apache sẽ tự động tạo và hiển thị danh sách các tệp trong thư mục đó.
Mô tả: Apache chỉ theo dõi các liên kết tượng trưng nếu chủ sở hữu của liên kết tượng trưng và mục tiêu là cùng một người. Điều này tăng cường bảo mật khi sử dụng symbolic links.
Mô tả: Cho phép xử lý các chỉ thị Server-Side Includes (SSI), giúp bạn chèn nội dung từ tệp khác vào tệp HTML.
<Directory /var/www/html>
Options Includes
</Directory>
6. MultiViews
Mô tả: Cho phép Apache chọn phiên bản tệp dựa trên phần mở rộng. Ví dụ: nếu bạn yêu cầu một tệp tên file, Apache có thể trả về file.html hoặc file.php dựa trên cài đặt cấu hình.