1. 程式人生 > >Ubuntu16.04配置Apache支援多個站點

Ubuntu16.04配置Apache支援多個站點

怎樣在一個Ubuntu的機器上(虛擬機器)配置Apache支援多個網站呢?

比如你有一臺獨立的Ubuntu虛擬機器,配有一個外網的IP(45.46.47.48),並且註冊了兩個域名AAA.com和BBB.com,將這兩個域名DNS解析到你虛機的IP地址。假設你已經安裝好了Apache,一切都是預設的設定。
我們需要在這一個server上面,同時host AAA.com,BBB.com
第一步:修改hosts檔案

在Ubuntu系統中,hosts檔案目錄為/etc/hosts,可以用vi編輯
sudo vi /etc/hosts
新增一下兩行內容:
127.0.0.1 AAA.com
127.0.0.1 BBB.com

第二步:建立站點目錄

預設一個站點,我們的站點目錄為/var/www/html,這裡我們分別為兩個站點建立兩個目錄:
建立目錄/var/www/html/AAA/, 並建立一個index.html檔案,新增內容”Hello, site AAA”
然後,
建立目錄/var/www/html/BBB/, 並建立一個index.html檔案,新增內容”Hello, site BBB”

第三步:修改apache config檔案

進入目錄 /etc/apache2/sites-available/
可以看到有一個預設檔案000-default.conf,我們可以直接將其作為A站點的config檔案,它的內容如下:

<VirtualHost *:80>
ServerName AAA.com ServerAlias www.AAA.com <Directory /var/www/html/AAA/> AllowOverride All </Directory> ServerAdmin [email protected] DocumentRoot /var/www/html/AAA ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}
/access.log combined </VirtualHost>

執行命令為BBB.com建立配置檔案:
sudo cp 000-default.conf 001-default.conf
修改其內容如下:

<VirtualHost *:80>
        ServerName BBB.com
        ServerAlias www.BBB.com
        <Directory /var/www/html/BBB/>
            AllowOverride All
        </Directory>
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/BBB
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

第四步:啟動站點

執行命令:
sudo a2ensite 000-default.conf
sudo a2ensite 001-default.conf
如果提示需要執行apache load,你可以按照提示執行命令。

第五步:重啟Apache service

執行命令:
sudo service apache2 restart

第六步:驗證

你可以在瀏覽器中分別輸入AAA.com和BBB.com檢視是否和index裡面的內容一致。如果和預期一致,那麼就完成了Apache多站點的配置了。