Thêm nhiều domain trong Apache – Web Server

4.3/5 - (9 votes)

Để thêm nhiều domain trong môi trường LAMP ở local hay live thì bạn cũng cần làm những thao tác như sau:

Bước 1: Config VirtualHost trên Apache

Đầu tiên sửa file sau vhost.conf bằng câu lệnh sudo vi /etc/httpd/conf.d/vhost.conf

Trên Debian OS dùng câu lệnh: cd /etc/apache2/sites-available vì cấu trúc trên debian và centos hơi khác nhau 1 tý.

vi là vim editor, bạn có thể thay thế bằng nano hay bất kỳ editor nào mà bạn thích. Nếu chưa có file này bạn tạo ra bằng touch vhost.conf.

Sau khi tạo xong bạn thêm nội dung sau:

<VirtualHost *:80>
    ServerName default:80
    DocumentRoot /var/www/html
</VirtualHost>

<VirtualHost *:80>
    ServerName ec2.dotrinh.com
    DocumentRoot /var/www/html/dotrinh
    <Directory "/var/www/html/dotrinh">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName edu.dotrinh.com
    DocumentRoot /var/www/html/edu
    <Directory "/var/www/html/edu">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Giải thích:

Mặc định không tìm thấy khai báo domain và thư mục domain trỏ đến nó sẽ chạy thư mục /var/www/html

<VirtualHost *:80>
    ServerName default:80
    DocumentRoot /var/www/html
</VirtualHost>

Nếu có domain ec2 trỏ tới thì gọi source trong folder /var/www/html/dotrinh

<VirtualHost *:80>
    ServerName ec2.dotrinh.com
    DocumentRoot /var/www/html/dotrinh
    <Directory "/var/www/html/dotrinh">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Nếu có domain edu trỏ tới thì gọi source trong folder /var/www/html/edu

<VirtualHost *:80>
    ServerName edu.dotrinh.com
    DocumentRoot /var/www/html/edu
    <Directory "/var/www/html/edu">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Nhớ tạo thư mục theo như đường dẫn khai báo bên file vhost.conf không tý nữa chạy lệnh restart apache nó báo lỗi là chưa khai báo thư mục.

Trên Debian: bật file cấu hình lên trước sudo a2ensite dotrinh.com.conf

Và cuối cùng khởi động lại apache bằng cmd: sudo service httpd restart

Chú ý ở đoạn này hay xảy ra lỗi khi thay đổi tên miền đó là: 

Job for apache2.service failed because the control process exited with error code.
See "systemctl status apache2.service" and "journalctl -xe" for details.

Nếu gặp lỗi này thì đó chính là do cái file config cũ của bạn chưa được xóa đi (hay tắt đi) vậy để sửa lỗi này bạn cần chạy câu lệnh sau và xem câu lệnh khởi động lại apche đã được chưa.

sudo a2dissite 000-default.conf

Bước 2: Trỏ domain về ip của VPS

Bước này quan trọng không kém, bạn cần lấy ip của vps của bạn sau đó vào phần quản lý domain của nhà cung cấp tên miền như godaddy,… để trỏ nó về cái ip mà bạn đã lấy được như thế này:

Vậy là xong bạn gõ vào domain đã setup xem sao, hãy comment bên dưới để được hỗ trợ và giải đáp tức thì. Xin cám ơn bạn rất nhiều.

PS: Bài viết này mình đã test và chạy ngon trên Amazon Linux AMI của AWS.

Update #1 Lỗi hay gặp trong khi làm việc với Virtual Host

Nếu gặp trường hợp không tìm thấy link “The requested was not found on this server.”  nhưng mà thực sự đường link này có, hãy sửa lại file xxx.com.conf cẩn thận theo định dạng:

<VirtualHost *:80>
        ServerName xxx.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/
        ServerAlias www.xxx.com
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        <Directory /var/www/html/>
                    Options Indexes FollowSymLinks MultiViews
                    AllowOverride All
                    Order allow,deny
                    allow from all
                </Directory>
</VirtualHost>

Update #2 Lỗi 500 Internal Server Error

Nên sửa lại file htaccess như thế này.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Restart lại Apache:

Debian: sudo service apache2 restart

CentOS: sudo service httpd start

Ubuntu Nginx: sudo service php8.1-fpm restart

Tham khảo thêm nhiều domain trong Debian ở đây:

https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-debian-8

Các bài viết không xem thì tiếc:

Xem thêm
Mở file sau: ‪C:\Program Files\NetBeans 8.1\etc\netbeans.conf bằng editor bất kỳ.Tìm biến netbeans_default_optionsThêm…
 
 
 
 
Facetime iPhone

Main Menu