phpvirtual-machineremote-accessphp-builtin-server

I cannot reach php built-in server running on a VM


I have a too basic php application that I wante to run through the built-in php server and that lives in my VM in my windows machine:

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';

$app = new \Slim\App();

$app->get('/', function (Request $req, Response $res, $arg = []) {
    return 'hello world!!';
});

$app->run();

My project structure

tree -I vendor
.
|-- cache
|   `-- 6a
|       `-- 532bg9987gt23f4356546poo999vvb234234.php
|-- composer.json
|-- composer.lock
|-- public
|   `-- js
|       `-- app.js
|-- routes
|   `-- web.php
`-- views
    `-- templates
        `-- index.html

7 directories, 7 files

When I run curl from within my VM it obviously works:

php -S localhost:9899&
curl localhost:9899/routes/web.php

Hello world!!

The problem is when I try to connect to this server from my browser (windows machine) I get

This site can’t be reached

Although this doesn't work for my php built-in server, it works perfectly for two other projects developed with that are on the same VM as the one.

Why I'm not able to reach the php built-in server especially that built-in server are accessible?


Solution

  • You are binding your server to localhost. It is only listening on the localhost network interface. It won't be accessible outside of that machine.

    Tell it to listen on your externally facing IP address instead.

    Alternatively, tell it to listen on all network interfaces:

    php -S 0.0.0.0:9889