JREAM
JREAM

Reputation: 5931

How to get ReactPHP to listen to any available port/socket through Vagrant?

I've tried updating my Vagrantfile with:

config.vm.network :forwarded_port, guest: 8080, host: 8080
..
$ vagrant halt && vagrant up

This is the code exactly from ReactPHP (I just tried different ports):

    $app = function ($request, $response) {
        $response->writeHead(200, array('Content-Type' => 'text/plain'));
        $response->end("Hello World\n");
    };

    $loop = React\EventLoop\Factory::create();
    $socket = new React\Socket\Server($loop);
    $http = new React\Http\Server($socket, $loop);

    $http->on('request', $app);
    echo "Server running at http://127.0.0.1:8080\n";

    $socket->listen(8080);
    $loop->run();

Upvotes: 1

Views: 800

Answers (1)

Sgoettschkes
Sgoettschkes

Reputation: 13189

What you are trying to do is access an application which is not exposed to the network. Your ReactPHP is only accessible from inside the vm (127.0.0.1), which is why you can access it from the inside of your vagrant vm but not from the outside.

You can make your app reachable from your network in many different ways:

Port forwarding

You already took the first step for port forwarding in that you added this to your Vagrantfile (btw., there is vagrant reload which does vagrant halt and vagrant up). Now, if you access http://localhost:8080 from your host it should forward this request into your vm to port 8080. This should work without any further changes.

Binding to 0.0.0.0

You can try to tell ReactPHP to bind to 0.0.0.0 which means the app is exposed to the network. This means you don't need any port forwarding and can access the app through http://{VM_IP}:8080.

Using a webserver

In a production environment, you'd use a webserver like apache/nginx to expose your app to the internet. The webserver would listen on port 80 and route traffic for different domains to apps running internally on different ports.

Upvotes: 3

Related Questions