Cách cấu hình virtuhost trên linux

Apache Virtual Hosts là 1 giải pháp giúp bạn có thể chạy nhiều website (hay nhiều domain) trên cùng Apache Web Server. Nó giúp bạn tận dụng tài nguyên máy chủ, tài nguyên địa chỉ IP, giúp giảm chi phí vận hành.

Bài viết sau sẽ hướng dẫn cơ bản cách tạo Apache Virtual Hosts trên Ubuntu Server.

1. Tiền đề bài viết

Máy chủ server / vps chạy hệ điều hành Ubuntu Server 18.04 (Hoặc bất cứ phiên bản Ubuntu / Debian / Linux Mint). Nếu chưa có thể sử dụng dịch vụ cloud server của Vultr

Tạo 2 website với các thông tin sau đây:

Website 1 Website 2
Domain Name vinasupport-a.com.test vinasupport-b.com.test
Thư mực lưu trữ website /var/www/html/vinasupport_a/public_html /var/www/html/vinasupport_b/public_html

2. Cấu hình Apache Virtual Hosts trên Ubuntu

2.1. Cài đặt Apache Web Server

Để cài đầy đủ Web Server (LAMP Stack) với Linux, Apache, MariaDB và PHP vui lòng tham khảo bài viết sau: Hướng dẫn cài đặt Web Server ( LAMP Stack ) trên Ubuntu Server

Hoặc đơn giản nếu bạn chỉ muốn cài Apache trên Ubuntu thôi thì chỉ cần chạy command sau:

sudo apt-get install apache2

2.2. Tạo thư mục lưu trữ web cho mỗi một Website.

Tạo thư mục lưu trữ source code cho 2 website:

sudo mkdir -p /var/www/html/vinasupport_a/public_html
sudo mkdir -p /var/www/html/vinasupport_b/public_html

Tạo file index.html cho website vinasupport-a.com:

sudo vi /var/www/html/vinasupport_a/public_html/index.html

Với nội dung như sau:


 
 vinasupport.com
 
 
   

Website: vinasupport-a.test

Tương tư tạo file index.html cho website vinasupport-b.com

sudo vi /var/www/html/vinasupport_b/public_html/index.html

Với nội dung như sau:


 
 vinasupport.com
 
 
   

Website: vinasupport-b.test

2.3. Phân quyền cho thư mục lưu trữ website

Để phân quyền chúng ta sử dụng lệnh sau:

sudo chown -R : /var/www/html/vinasupport_a/public_html
sudo chown -R : /var/www/html/vinasupport_b/public_html
sudo chmod -R 755 /var/www/html

Với tương ứng là User và Group của Apache Server. Để tim thông tin này sử dụng command  “apachectl -S

Cách cấu hình virtuhost trên linux

2.4. Tạo file Virtual hosts config cho mỗi website

Tạo file config bằng cách copy file template của file config.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/vinasupport-a.test.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/vinasupport-b.test.conf

Sau đó sử nội dung của 2 file vinasupport-a.test.conf và vinasupport-a.test.conf như sau:


        ServerAdmin [email protected]
        ServerName vinasupport-a.test
        ServerAlias www.vinasupport-a.test
        DocumentRoot /var/www/html/vinasupport_a/public_html


        ServerAdmin [email protected]
        ServerName vinasupport-b.test
        ServerAlias www.vinasupport-b.test
        DocumentRoot /var/www/html/vinasupport_b/public_html

2.5. Kích hoạt file config của Apache Virtual Hosts

Vô hiệu hóa file config mặc định của Apache và kích hoạt file config cho 2 domain.

sudo a2dissite 000-default.conf
sudo a2ensite vinasupport-a.test.conf
sudo a2ensite vinasupport-b.test.conf

Sau đó khởi động lại Apache

sudo systemctl reload apache2

2.6. Test config của bạn.

Sửa file /etc/hosts và thêm 2 domain của bạn vào nếu bạn cần test trên local. Còn không thì cứ mở trình duyệt web gõ tên domain của bạn. Kết quả:

Cách cấu hình virtuhost trên linux

Nguồn: vinasupport.com

Cách cấu hình virtuhost trên linux

Đã đăng vào Nov 19th, 2017 4:59 p.m. 1 phút đọc

1. Mở Terminal rồi thực hiện các bước như sau:

  • Mở thư mục sites-available – Nơi chứa các file cấu hình virtual host của apache

cd /etc/apache2/sites-available

  • Tạo file cấu hình domain bằng lệnh

sudo vim domain_name.conf

domain_name: tên file cấu hình ( my_project.dev.conf )

2. Cấu hình file virtual host

sudo gedit /etc/apache2/sites-available/domain_name.conf

nhập nội dung sau vào sau đó lưu lại.

Cách cấu hình virtuhost trên linux

Trong đó: source_web_path: Thư mục chứa project của bạn. Ví dụ /var/www/html/my_project

3. Thông báo cho Apache biết các trang mình vừa cấu hình bằng câu lệnh sau:

sudo a2ensite my_project.dev.conf

4. Thêm địa chỉ IP về domain vừa cấu hình:

sudo gedit /etc/hosts

127.0.0.1 localhost 127.0.0.1 domain_name Trong đó: domain_name là tên domain chúng ta vừa cấu hình ( my_project.dev )

5. Thao tác với Server Apache

  • Nếu Apache chưa được start:

sudo service apache2 start

  • Nếu Apache đã start thì dùng lệnh sau để restart

sudo service apache2 restart

  • Reload lại cấu hình Apache bằng lệnh

sudo /etc/init.d/apache2 reload

Mọi thứ đã hoàn tất. Chúng ta mở trình duyệt và nhập địa chỉ domain vừa cấu hình và đón xem thành quả nhé. Ví dụ (my_project.dev) Lưu ý: Với project là Laravel Framework các bạn chạy thêm câu lệnh sau để kích hoạt mod_rewrite nhé :slight_smile:

sudo a2enmod rewrite

Chúc các bạn thành công!

All rights reserved