
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).
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://localhost/"
</FilesMatch>
# Log file locations
LogLevel warn
ErrorLog /home/mysite/logs/error.log
CustomLog /home/mysite/logs/access.log combined
</VirtualHost>
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 đó.<Directory /var/www/html>
Options Indexes
</Directory>
<Directory /var/www/html>
Options FollowSymLinks
</Directory>
<Directory /var/www/cgi-bin>
Options ExecCGI
AddHandler cgi-script .cgi .pl .py
</Directory>
<Directory /var/www/html>
Options FollowSymLinks SymLinksIfOwnerMatch
</Directory>
<Directory /var/www/html>
Options Includes
</Directory>
file, Apache có thể trả về file.html hoặc file.php dựa trên cài đặt cấu hình.<Directory /var/www/html>
Options MultiViews
</Directory>
MultiViews. Đây là tùy chọn mặc định nếu bạn không chỉ định tùy chọn nào khác.<Directory /var/www/html>
Options All
</Directory>
<Directory /var/www/html/private>
Options None