top of page

HƯỚNG DẪN CÀI ĐẶT VIRTUALHOST VỚI APACHE TRÊN CENTOS 7

Writer's picture: Tuan NguyenTuan Nguyen

Updated: Jul 23, 2018


Giả sử chúng ta có hai websites cùng chạy trên một server (một IP), cùng port mặc định là 80. Yêu cầu được đặt ra như sau: Khi người dùng gõ vào trình duyệt web1.yourdomain.com thì sẽ vào website thứ nhất, khi gõ web2.yourdomain.com thì sẽ vào website thứ 2. VirtualHost trên Centos7 sẽ giải quyết vấn đề này.


Nếu có DNS server thì trên Server tạo 2 host A cùng trỏ về một IP của web server




1. Tạo thư mục chứa data của từng web-site


1.1. Tạo nơi lưu web 1


Tạo trong /var/www

# cd /var/www
# mkdir -p web1.pivotalvietnam.com/public_html
# cd web1.pivotalvietnam.com/public_html

Tạo nội dung của web 1

# vi index.html

<html>

<head>

<title>Web 1</title>

</head>

<body>

<h1>Here is website 1</h1>

</body>

</html>



1.2. Tạo nơi lưu web 2

# cd /var/www
# mkdir -p web2.pivotalvietnam.com/public_hmtl
# cd web2.pivotalvietnam.com/public_html

Tạo nội dung của web 2

# vi index.html

<html>

<head>

<title>Web 2</title>

</head>

<body>

<h1>Here is website 2</h1>

</body>

</html>

2. Gán quyền cho thư mục chứa data của từng website đã tạo

# chown -R apache:apache /var/www/web1.pivotalvietnam.com/public_html
# chown -R apache:apache /var/www/web2.pivotalvietnam.com/public_html
# chomod -R 755 /var/www


3. Tạo thư mục chứa virtual host file


Tạo trong /etc/httpd. Mỗi website ta sẽ tạo mỗi virtual host file riêng có đuôi .conf

# cd /etc/httpd

Tạo nơi chứa tất cả các virtualhost file

# mkdir sites-availables
# cd sites-availables
# vi web1.pivotalvietnam.com.conf

<VirtualHost *:80>

ServerName web1.pivotalvietnam.com

ServerAlias wwww.web1.pivotalvietnam.com

DocumentRoot /var/www/web1.pivotalvietnam.com/public_html

ErrorLog /var/www/web1.pivotalvietnam.com/error.log

CustomLog /var/www/web1.pivotalvietnam.com/requests.log combined

</VirtualHost>


# vi web2.pivotalvietnam.com.conf

<VirtualHost *:80>

ServerName web2.pivotalvietnam.com

ServerAlias wwww.web2.pivotalvietnam.com

DocumentRoot /var/www/web2.pivotalvietnam.com/public_html

ErrorLog /var/www/web2.pivotalvietnam.com/error.log

CustomLog /var/www/web2.pivotalvietnam.com/requests.log combined

</VirtualHost>


4. Tạo thư mục link


Thư mục này chúng ta sẽ symbolic link từ thư mục chứa virtual host file đến

# cd /etc/httpd
# mkdir sites-enabled

5. Chỉnh file cấu hình Apache


# vi /etc/httpd/conf/httpd.conf

Thêm vào cuối file

IncludeOptional sites-enabled/*.conf


6. Tạo symbolic link để enable virtual host file


# ln -s /etc/httpd/sites-availables/web1.pivotalvietnam.com.conf /etc/httpd/sites-enabled/web1.picotalvietnam.com.conf
# ln -s /etc/httpd/sites-availables/web2.pivotalvietnam.com.conf /etc/httpd/sites-enabled/web2.picotalvietnam.com.conf

7.Khởi động lại apache


# systemctl restart httpd

Nếu lỗi không khởi động được thì ta xử lý như sau:

# systemctl status -l httpd

Lỗi bên trên thường do tạo symbolic link bị sai. Cách khắc phục là xóa symbolic link rồi tạo lại, chú ý lúc tạo symbolic link phải gõ đầy đủ đường dẫn.


9 views0 comments

Comments


 

© 2018 by Tuấn Nguyễn

 Liên hệ tôi
  • Facebook - Black Circle
  • Google+ - Black Circle
bottom of page