本文摘自PHP中文网,作者不言,侵删。
VirtualHosting是在单个服务器上托管多个域的一种实现。它能够利用服务器的最大资源并降低消耗现在,大多数Web服务器都支持虚拟主机环境。

在我们之前的文章中,我们介绍了在CentOS / RHEL上安装Lighttpd服务器。本篇文章将介绍关于在Lighttpd服务器中设置VirtualHosts。
例如,我们使用以下域名:
site1.php.cn
site2.php.cn
步骤1:创建服务器文档根目录
首先为两个域创建文件夹(如果不存在)
1 2 | # mkdir -p /sites/vhosts/site1.php.cn/www
# mkdir -p /sites/vhosts/site2.php.cn/www
|
出于测试目的,我们在两个文档根目录中创建index.html文件
1 2 | # 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:更新主配置文件
现在编辑Lighttpd主配置文件/etc/lighttpd/lighttpd.conf并启用包含虚拟主机的文件。通过删除起始#符号取消对以下行的注释。
1 | include_shell "cat /etc/lighttpd/vhosts.d/*.conf"
|
步骤3:创建VirtualHost配置文件
现在开始为两个域或子域创建virutalhost配置文件,首先为site1.php.cn创建
1 | # vim /etc/lighttpd/vhosts.d/site1.php.cn.conf
|
1 2 3 4 5 6 | $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"
}
|
现在为site2.php.cn创建配置文件
1 | # vim /etc/lighttpd/vhosts.d/site2.php.cn.conf
|
1 2 3 4 5 | $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:验证配置并重新启动lighttpd
首先验证所有配置文件的语法,包括主配置文件
1 2 3 | # lighttpd -t -f /etc/lighttpd/lighttpd.conf
Syntax OK
|
如果发现所有语法都正常,让我们重新启动服务。
1 | # service lighttpd restart
|
完成后在浏览器中测试你的两个域,并检查是否获得了步骤1中创建的页面上的正确内容。
【相关推荐:Linux视频教程】
以上就是如何在Lighttpd Server中设置VirtualHosts(虚拟主机)的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
如何在lighttpd server中设置VirtualHosts(虚拟主机)
如何在lighttpd server中设置VirtualHosts(虚拟主机)
更多相关阅读请进入《VirtualHosts》频道 >>
转载请注明出处:木庄网络博客 » 如何在Lighttpd Server中设置VirtualHosts(虚拟主机)