概念
虛擬主機(Virtual Host) 是在同一臺機器搭建屬於不同域名或者基於不同 IP 的多個網站服務的技術. 可以爲運行在同一物理機器上的各個網站指配不同的 IP 和端口, 也可讓多個網站擁有不同的域名.
Apache 是世界上使用最廣的 Web 服務器, 從 1.1 版開始支持虛擬主機. 本文將講解在不同服務器 (Redhat Enterprise Linux, Ubuntu Linux, Windows) 上使用 Apache 搭建虛擬主機來搭建多個網站.
Redhat Enterprise Linux
Redhat EnterpriseLinux (包括 CentOS Linux), 是使用最廣的 Linux 服務器, 大量的網站應用都部署在其上.
1. 打開文件 /etc/httpd/conf/httpd.conf, 搜索VirtualHost example, 找到代碼如下:
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *:80>
# ServerAdmin [email protected]
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>
2. 仿照例子, 添加一段代碼來指定某一域名的網站.
#
# DocumentRoot 是網站文件存放的根目錄
# ServerName 是網站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/httpdocs/demo_neoease_com
ServerName demo.neoease.com
ErrorLog logs/demo.neoease.com-error.log
CustomLog logs/demo.neoease.com-access.log common
</VirtualHost>
3. 重啓 httpd 服務, 執行以下語句.
service httpd restart
Ubuntu Linux
Ubuntu 在 Linux 各發行版中, 個人用戶數量最多的. 很多人在本機和虛擬機中使用. 但 Ubuntu 和 Redhat 的 VirtualHost 設置方法不相同.
1. 打開目錄 /etc/apache2/sites-available/, 發現default 和 default-ssl 兩個文件, 其中 default 是 http 虛擬主機服務的配置文件, default-ssl 是配置 https 服務使用的. 可以複製一份 default 文件. 並修改配置文件名, 文件名必須與域名一致 (如: demo.neoease.com)
2. 打開新建的配置文件, 修改 DocumentRoot, ServerName 和對應的配置目錄. 例子如下:
#
# DocumentRoot 是網站文件存放的根目錄
# ServerName 是網站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/httpdocs/demo_neoease_com
ServerName demo.neoease.com
ErrorLog ${APACHE_LOG_DIR}/demo.neoease.com-error.log
CustomLog ${APACHE_LOG_DIR}/demo.neoease.com-access.log combined
</VirtualHost>
3. 通過 a2ensite 激活虛擬主機配置
sudo a2ensite demo.neoease.com
4. 打開目錄 /etc/apache2/sites-enabled/, 你會發現所有激活的虛擬主機, 可以通過 a2dissite 進行註銷
sudo a2dissite demo.neoease.com
5. 重啓 Apache 服務, 激活虛擬主機
sudo /etc/init.d/apache2 restart
Windows
Windows 是市場佔有率最高的 PC 操作系統, 也是很多人的開發環境. 其 VirtualHost 配置方法與 Linux 上有些差異, 以下方式適合原生 Apache, XAMPP 和WAMP 套件.
1. 打開目錄 {Apache2 安裝目錄}\conf\extra\, 找到 httpd-vhosts.conf 文件.
2. 仿照例子, 添加一段代碼來指定某一域名的網站.
#
# DocumentRoot 是網站文件存放的根目錄
# ServerName 是網站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "D:/workspace/php/demo_neoease_com"
ServerName demo.neoease.com
ErrorLog "logs/demo.neoease.com-error.log"
CustomLog "logs/demo.neoease.com-access.log" common
</VirtualHost>
3. 打開 httpd.conf 文件, 添加如下語句.
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
4. 重啓 Apache 服務.
Mac OS
近年蘋果的雄起, 讓 Mac 日催普及, 也成爲很多開發人員的選擇. 因爲與 Linux 同源, 配置方法也相似.
1. 打開文件 /private/etc/apache2/extra/httpd-vhosts.conf.
2. 仿照例子, 添加一段代碼來指定某一域名的網站.
#
# DocumentRoot 是網站文件存放的根目錄
# ServerName 是網站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/usr/docs/httpdocs/demo_neoease_com"
ServerName demo.neoease.com
ErrorLog "/private/var/log/apache2/demo.neoease.com-error_log"
CustomLog "/private/var/log/apache2/demo.neoease.com-access_log" common
</VirtualHost>
3. 打開文件 /private/etc/apache2/httpd.conf, 搜索Virtual hosts, 找到代碼如下:
# Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf
去掉前面的註釋符號 #, 保存文件.
4. 重啓 apache 服務, 執行以下語句.
sudo apachectl restart
總結
最近我在不同的幾臺服務器上搭建了一些網站服務, 這篇文章也算是我的個人筆記, 望日後可自用, 也希望對讀者有用. 文中介紹了幾個主流開發和部署環境上配置虛擬主機的方法, 其他 OS 大同小異.
因爲還有些朋友留言希望知道 Nginx 的配置方法, 作爲補充內容, 我又寫了《Nginx虛擬主機 VirtualHost配置》