sma*_*ber 2 php remote-access virtual-machine php-builtin-server
我有一个太基本的 php 应用程序,我想通过内置 php 服务器运行它,并且它位于 Windows 计算机的虚拟机中:
\n\n<?php\nuse \\Psr\\Http\\Message\\ServerRequestInterface as Request;\nuse \\Psr\\Http\\Message\\ResponseInterface as Response;\n\nrequire \'../vendor/autoload.php\';\n\n$app = new \\Slim\\App();\n\n$app->get(\'/\', function (Request $req, Response $res, $arg = []) {\n return \'hello world!!\';\n});\n\n$app->run();\nRun Code Online (Sandbox Code Playgroud)\n\n我的项目结构
\n\ntree -I vendor\n.\n|-- cache\n| `-- 6a\n| `-- 532bg9987gt23f4356546poo999vvb234234.php\n|-- composer.json\n|-- composer.lock\n|-- public\n| `-- js\n| `-- app.js\n|-- routes\n| `-- web.php\n`-- views\n `-- templates\n `-- index.html\n\n7 directories, 7 files\nRun Code Online (Sandbox Code Playgroud)\n\n当我从虚拟机中运行curl时,它显然可以工作:
\n\nphp -S localhost:9899&\ncurl localhost:9899/routes/web.php\n\nHello world!!\nRun Code Online (Sandbox Code Playgroud)\n\n问题是当我尝试从浏览器(Windows 机器)连接到该服务器时,我得到
\n\nThis site can\xe2\x80\x99t be reached\nRun Code Online (Sandbox Code Playgroud)\n\n虽然这不适用于我的 php 内置服务器,但它对于使用nodejs开发的另外两个项目(与php位于同一虚拟机上)来说非常有效。
\n\n为什么我无法访问 php 内置服务器,尤其是Nodejs内置服务器可以访问?
\n您正在将您的服务器绑定到localhost. 它仅在网络接口上侦听localhost。在该机器之外无法访问它。
告诉它监听您的外部 IP 地址。
或者,告诉它监听所有网络接口:
php -S 0.0.0.0:9889
Run Code Online (Sandbox Code Playgroud)