virtualhosting是在單個(gè)服務(wù)器上托管多個(gè)域的一種實(shí)現(xiàn)。它能夠利用服務(wù)器的最大資源并降低消耗現(xiàn)在,大多數(shù)web服務(wù)器都支持虛擬主機(jī)環(huán)境。
在我們之前的文章中,我們介紹了在CentOS / RHEL上安裝Lighttpd服務(wù)器。本篇文章將介紹關(guān)于在Lighttpd服務(wù)器中設(shè)置VirtualHosts。
例如,我們使用以下域名:
site1.php.cn
site2.php.cn
步驟1:創(chuàng)建服務(wù)器文檔根目錄
首先為兩個(gè)域創(chuàng)建文件夾(如果不存在)
#?mkdir?-p?/sites/vhosts/site1.php.cn/www #?mkdir?-p?/sites/vhosts/site2.php.cn/www
出于測(cè)試目的,我們?cè)趦蓚€(gè)文檔根目錄中創(chuàng)建index.html文件
#?echo?"Welcome?to?Site1"?>?/sites/vhosts/site1.php.cn/www/index.html #?echo?"Welcome?to?Site2"?>?/sites/vhosts/site2.php.cn/www/index.html
步驟2:更新主配置文件
現(xiàn)在編輯Lighttpd主配置文件/etc/lighttpd/lighttpd.conf并啟用包含虛擬主機(jī)的文件。通過(guò)刪除起始#符號(hào)取消對(duì)以下行的注釋。
include_shell?"cat?/etc/lighttpd/vhosts.d/*.conf"
步驟3:創(chuàng)建VirtualHost配置文件
現(xiàn)在開(kāi)始為兩個(gè)域或子域創(chuàng)建virutalhost配置文件,首先為site1.php.cn創(chuàng)建
#?vim?/etc/lighttpd/vhosts.d/site1.php.cn.conf
$HTTP["host"]?==?"site1.php.cn"?{ ????????server.document-root?=?"/sites/vhosts/site1.php.cn/public" ????????server.errorlog?=?"/var/log/lighttpd/site1.php.cn.error.log" ????????accesslog.filename?=?"/var/log/lighttpd/site1.php.cn.access.log" }
現(xiàn)在為site2.php.cn創(chuàng)建配置文件
#?vim?/etc/lighttpd/vhosts.d/site2.php.cn.conf
$HTTP["host"]?==?"site2.php.cn"?{ ????????server.document-root?=?"/sites/vhosts/site2.php.cn/public" ????????server.errorlog?=?"/var/log/lighttpd/site2.php.cn.error.log" ????????accesslog.filename?=?"/var/log/lighttpd/site2.php.cn.access.log" }
步驟4:驗(yàn)證配置并重新啟動(dòng)lighttpd
首先驗(yàn)證所有配置文件的語(yǔ)法,包括主配置文件
#?lighttpd?-t?-f?/etc/lighttpd/lighttpd.conf Syntax?OK
如果發(fā)現(xiàn)所有語(yǔ)法都正常,讓我們重新啟動(dòng)服務(wù)。
#?service?lighttpd?restart
完成后在瀏覽器中測(cè)試你的兩個(gè)域,并檢查是否獲得了步驟1中創(chuàng)建的頁(yè)面上的正確內(nèi)容。
【相關(guān)推薦:在CentOS / RHEL上安裝Lighttpd服務(wù)器】