轻型(Lighttpd)虚拟主机配置


Lighty (Lighttpd) Virtual Host Config

我想在我的开发环境中设置PHP使用3个不同的域名:

  1. example1.domain.com
  2. example2.domain.com
  3. example4.domain.com

我已经使用Homebrew安装了PHP 5.4和Lighty,我的目标是在开发过程中为启动/停止web服务器提供一个类似rails的解决方案。基本上,我想要:

$ rails s

的行为类似于:

$ lighttpd -D -f lighttpd.conf

我已经让服务器运行,并指向localhost:8000,但一些资产不能正确加载,因为一些资产的前缀与上述域。

所以我的问题,我如何正确配置轻量级与虚拟主机与上述域?

这是我当前的Lighty配置:

server.bind = "0.0.0.0"
server.port = 8000
server.document-root = CWD + "/public"
server.errorlog = CWD + "/lighttpd.error.log"
accesslog.filename = CWD + "/lighttpd.access.log"
index-file.names = (
  "index.php",
  "index.html",
  "index.htm",
  "default.htm"
)
server.modules = (
  "mod_fastcgi",
  "mod_accesslog",
  "mod_rewrite"
)
fastcgi.server = (
  ".php" => ((
    "bin-path" => "/usr/local/bin/php-cgi",
    "socket" => CWD + "/php5.socket"
  ))
)
mimetype.assign = (
  ".css"        =>  "text/css",
  ".gif"        =>  "image/gif",
  ".htm"        =>  "text/html",
  ".html"       =>  "text/html",
  ".jpeg"       =>  "image/jpeg",
  ".jpg"        =>  "image/jpeg",
  ".js"         =>  "text/javascript",
  ".png"        =>  "image/png",
  ".swf"        =>  "application/x-shockwave-flash",
  ".txt"        =>  "text/plain"
)
# Making sure file uploads above 64k always work when using IE or Safari
# For more information, see http://trac.lighttpd.net/trac/ticket/360
$HTTP["useragent"] =~ "^(.*MSIE.*)|(.*AppleWebKit.*)$" {
  server.max-keep-alive-requests = 0
}
# Vhost settings
$HTTP["host"] =~ "example[0-9]+.domain.com" {
  server.document-root = CWD + "/public"
  server.errorlog = CWD + "/lighttpd.error.log"
  accesslog.filename = CWD + "/lighttpd.access.log"
}

我有两个想法,您可以使用mod_simple_vhost或在主配置中设置根文档目录。

比如:

    $HTTP["host"] =~ "^example([0-9])'.domain'.com$" {
        server.document-root = CWD + "/example%1/"
    }

或使用mod_simple_vhost,这里有一个示例配置:

    server.modules += ( "mod_simple_vhost" )
    simple-vhost.server-root   = CWD + "/"
    simple-vhost.document-root = $HTTP["host"] + "/"
    simple-vhost.default-host  = "domain.com"

这将把example1.domain.com的页面放在/CWD/example1.domain.com/。如果不工作,尝试simple-vhost.document-root = CWD + "/" + $HTTP["host"] + "/"代替。

但是,如果您遇到DNS问题,要么编辑hosts文件并添加域,要么配置您的名称服务器

如果它们都使用与主服务器配置相同的设置,则不需要配置任何虚拟主机。您应该能够将域名添加到hosts文件中,这样它们就会被定向回您的计算机,而不是指向真正的domain.com。在mac和linux的/etc/hosts或windows的C:'Windows'System32'drivers'etc'hosts中添加:

127.0.0.1   example1.domain.com
127.0.0.1   example2.domain.com
127.0.0.1   example3.domain.com