V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
picasso250
V2EX  ›  PHP

php build in server vhost

  •  
  •   picasso250 · 2014-03-10 14:53:47 +08:00 · 3249 次点击
    这是一个创建于 4120 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <?php
    // php build in server vhost
    // php -S 0.0.0.0:80 router.php
    // on Windows, router.php must be absolute path, bug of php v5.4.26
    $map = array(
    'host.name' => '/file/path',
    );
    $f = __DIR__.'/router_config.php';
    if (file_exists($f)) {
    $map = require $f;
    }
    $host = $_SERVER['HTTP_HOST'];
    if (isset($map[$host])) {
    error_log("$host $_SERVER[REQUEST_URI]");
    $root = $map[$host];
    $arr = explode('?', $_SERVER["REQUEST_URI"]);
    $path = $arr[0];
    $f = $root.$path;
    if ($f == "$root/favicon.ico") {
    if (is_file($f)) {
    header("Content-Type: image/x-icon");
    readfile($f);
    } else {
    return false;
    }
    }
    $fhtml = "$root/index.html";
    $fphp = "$root/index.php";
    if (is_file($f)) {
    $pathinfo = pathinfo($f);
    if (isset($pathinfo['extension']) && $pathinfo['extension'] == 'php') {
    chdir(dirname($f));
    include $f;
    } else {
    $meme = get_content_type($f);
    header("Content-Type: $meme");
    readfile($f);
    }
    } elseif (is_file($fhtml)) {
    echo file_get_contents($f);
    } elseif (is_file($fphp)) {
    chdir($root);
    include $fphp;
    }
    } else {
    return false;
    }
    function get_content_type($f)
    {
    static $map = array(
    'js' => 'application/javascript',
    'css' => 'text/css',
    'png' => 'image/png',
    'jpg' => 'image/jpeg',
    'jpeg' => 'image/jpeg',
    'gif' => 'image/gif',
    'ico' => 'image/x-icon',
    );
    $pathinfo = pathinfo($f);
    if (isset($pathinfo['extension']) && ($extension = strtolower($pathinfo['extension'])) && isset($map[$extension])) {
    return $map[$extension];
    }
    return 'text/html';
    }
    view raw router.php hosted with ❤ by GitHub


    监听80端口,分发给各个vhost
    3 条回复    1970-01-01 08:00:00 +08:00
    vibbow
        1
    vibbow  
       2014-03-10 18:51:17 +08:00
    PHP Build-in server作为一个单线程服务器,做vhost的意义在哪里?
    picasso250
        2
    picasso250  
    OP
       2014-03-10 20:56:29 +08:00
    @vibbow 在我的开发机,也就是windows下,不需要安装apache

    Anyway, good question.
    picasso250
        3
    picasso250  
    OP
       2014-03-10 23:52:52 +08:00
    发现 Windows 下的一个bug:router.php 必须得用绝对路径指定。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   913 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 21:00 · PVG 05:00 · LAX 14:00 · JFK 17:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.