Apache là máy chủ web phổ biến nhất hiện nay. Nó mạnh mẽ và dễ sử dụng, đặc biệt nếu bạn chạy nó trên Ubuntu. Apache chia các chức năng và thành phần của nó ra riêng rẽ, vì thế bạn có thể dễ dàng để cấu hình và quản lý. Vậy làm thế nào đê chạy nhiều website trên 1 máy chủ, giải pháp đưa ra là sử dụng Virtual Host.
Một phần không thể thiếu của một máy chủ web (bao gồm cả Apache, Nginx, Lighttpd…) là chạy nhiều web trên 1 server. Bạn có thể thêm domain vào máy chủ và cấu hình chúng riêng rẽ. Mỗi cấu hình như vậy được gọi là một virtual host.
Mỗi một virtual host sẽ quy định cho mỗi tên miền riêng biệt. Bạn có thể tạo ra vô số virtual host để thêm domain vào VPS và chạy nhiều web trên 1 server.
Đầu tiên, bạn cần tạo ra hai thư mục để chứa tập tin và dữ liệu của hai trang web trên. Mặc định Apache sử dụng /var/www
làm thư mục chủ. Và mình sẽ tạo ra hai thư mục ở đó để lưu trữ dữ liệu cho hai trang web. Bạn có thể tạo thư mục ở bất kỳ đâu mà bạn muốn, theo ví dụ ở đây mình sẽ có hai thư mục là/var/www/vidu1.com
và /var/www/vidu2.com
.
Thông thường người ta sẽ sử dụng một thư mục con để lưu trữ dữ liệu để xử lý trang web cho người truy cập là public_html
. Mục đích của điều này là để phục vụ lưu trữ dữ liệu một cách linh hoạt, bạn có thể lưu dữ liệu chỉ dành cho admin ở một thư mục khác.
Tạo hai thư mục bằng hai lệnh sau:
$ sudo mkdir -p /var/www/vidu1.com/public_html
$ sudo mkdir -p /var/www/vidu2.com/public_html
Trên Ubuntu, các tập tin cấu hình virtual host của Apache sẽ nằm trong thư mục /etc/apache2/sites-available
. Bạn sẽ nhìn thấy hai virtual host mặc định là 000-default.conf
và default-ssl.conf,
ở đây mình sẽ dựa trên 2 virtual host này để thêm cấu hình cho domain khác.
Để kiểm tra các virtual host có sẵn, sử dụng lệnh sau:
$ ls -l /etc/apache2/sites-available
Sao chép 2 virtual host mặc định cho hai tên miền mới:
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/vidu1.com.conf
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/vidu2.com.conf
Mở và chỉnh sửa tập tin cấu hình/etc/apache2/sites-available/vidu1.com.conf
$ sudo nano /etc/apache2/sites-available/vidu1.com.conf
Bạn sẽ nhìn thấy nội dung như sau:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request’s Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, …, trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with “a2disconf”.
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Ở trên bạn sẽ có một số thông tin cần cấu hình lại:
/var/www/vidu1.com/public_html
Sau khi chỉnh sửa, bạn sẽ có một tập tin cấu hình virtual host với nội dung như sau:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request’s Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName vidu1.com
ServerAlias www.vidu1.com
ServerAdmin webmaster@vidu1.com
DocumentRoot /var/www/vidu1.com/public_html
# Available loglevels: trace8, …, trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with “a2disconf”.
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Lưu lại và tiếp tục mở tập tin virtual host thứ 2:
$ sudo nano /etc/apache2/sites-available/vidu2.com.conf
Cũng chỉnh sửa tương tự như trước:
ServerName vidu2.com
ServerAlias www.vidu2.com
ServerAdmin webmaster@vidu2.com
DocumentRoot /var/www/vidu2.com/public_html
Sử dụng lệnh sau để bật virtual host:
$ sudo a2ensite vidu1.com.conf
$ sudo a2ensite vidu2.com.conf
Khởi động lại máy chủ web Apache
$ sudo service apache2 restart
Có thể bạn sẽ gặp thông báo sau, nhưng nó không ảnh hưởng tới trang web của bạn nhé
AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1. Set the ‘ServerName’ directive globally to suppress this message
Để kiểm tra xem việc tạo virtual host trên Apache xem đã đúng chưa, bạn cần phải tạo 2 tập tin và chạy nó trên trình duyệt.
Tạo tập tin index.html trong /var/www/vidu1.com/public_html
và /var/www/vidu2.com/public_html
$ sudo nano /var/www/vidu1.com/public_html/index.html
$ sudo nano /var/www/vidu2.com/public_html/index.html
Với nội dung như sau, nhớ thay thế nội dung để phân biệt 2 trang web nhé:
<html>
<head>
<title>vidu1.com lam viec tot!</title>
</head>
<body>
<h1>OK! Tao virtual host cho vidu1.com thanh cong!</h1>
</body>
</html>
Cuối cùng, trỏ domain về IP của server, sau khi DNS được cập nhật thì bạn truy cập vào địa chỉ vidu1.com và vidu2.com. Nếu bạn nhìn thấy trang web hiển thị đúng như trên thì việc thêm domain vào VPS đã thành công.
Bạn có thể tạo virtual host trên Apache để thêm domain vào VPS và chạy nhiều web trên 1 server với số lượng không giới hạn. Miễn là server của bạn có đủ khả năng để xử lý. Chúc các bạn thành công!
Unpublished comment
Viết câu trả lời