Apache Virtual Host

Giới thiệu
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://localhost/"
</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ệpindex
(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>
2. FollowSymLinks
- Mô tả: Cho phép Apache theo dõi các liên kết tượng trưng (symbolic links) để truy cập tệp hoặc thư mục mà liên kết tượng trưng trỏ tới.
<Directory /var/www/html>
Options FollowSymLinks
</Directory>
3. ExecCGI
- Mô tả: Cho phép thực thi các tệp CGI (Common Gateway Interface) trong thư mục. Thường được sử dụng cho các tập lệnh Perl hoặc Python
<Directory /var/www/cgi-bin>
Options ExecCGI
AddHandler cgi-script .cgi .pl .py
</Directory>
4. SymLinksIfOwnerMatch
- 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.
<Directory /var/www/html>
Options FollowSymLinks SymLinksIfOwnerMatch
</Directory>
5. Includes
- 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ặcfile.php
dựa trên cài đặt cấu hình.
<Directory /var/www/html>
Options MultiViews
</Directory>
7. All
- Mô tả: Bật tất cả các tùy chọn ngoại trừ
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>
8. None
- Mô tả: Vô hiệu hóa tất cả các tùy chọn cho thư mục. Điều này có thể hữu ích nếu bạn muốn khóa các quyền xử lý trong một thư mục cụ thể.
<Directory /var/www/html/private>
Options None
Post Comment