1. 程式人生 > >apache2 多域名共享單主機(多域名配置)

apache2 多域名共享單主機(多域名配置)

正式的域名和IP地址的捆綁是需要在你購買域名的服務商那裡進行的,比如阿里去,godaddy等。

不過我們可以通過修改 /etc/hosts 來指定兩個測試域名指向127.0.0.1,方便我們進行多域名配置的模擬。

(具體操作見:ubuntu 通過修改 /etc/hosts 強制捆綁域名和IP地址

現在我們假設我們申請了兩個域名 abc.com、 ff99.com ,都要共用 127.0.0.1 這個伺服器。具體步驟:

一、新建兩個目錄分別做為 abc.com def.com 這兩個網站的根目錄

/var/www/abc.com/

/var/www/ff99.com/

並在兩個目錄分別新建一個 index.html 檔案,內容不同,方便後面我們驗證多域名配置:

$ cat ff99.com/index.html 
hi, this is ff99

$ cat abc.com/index.html 
hi, this is abc.com
 

二、在/etc/apache2/sites-enabled/ 目錄下新建兩個配置檔案

/etc/apache2/sites-enabled/abc.com.conf

內容如下:

<VirtualHost *:80>
ServerName abc.com
ServerAlias *.abc.com
DocumentRoot /var/www/abc.com/
</VirtualHost>
 

/etc/apache2/sites-enabled/ff99.com.conf

內容如下:

<VirtualHost *:80>
ServerName ff99.com
ServerAlias *.ff99.com
DocumentRoot /var/www/ff99.com/
</VirtualHost>

三、重啟

service apache2 restart

四、驗證