Ratchet WebSocket won't work over Vagrant machine - php

I have a WebSocket server using Ratchet (literally the example app). I'm serving it to localhost:8080 on my Vagrant machine (which is a CentOS 6) and trying to connect to it through the private network IP set in the Vagrantfile 192.168.33.10.
I'm getting a connect ECONNREFUSED 192.168.33.10:8080 (the uri is ws://192.168.33.10:8080/chat).
I already exposed the port config.vm.network "forwarded_port", guest: 8080, host: 8080.
I've tried serving the server (inside vagrant) to localhost, 127.0.0.1 and 192.168.33.10, but the client still can't connect.
It works fine outside of Vagrant using localhost on both client and server.
What am I missing?

If you bind the app to localhost there is no way you can access it via 192.168.33.10, hence port forwarding won't work either.
You need to make your app to listen on 192.168.33.10:8080.
Also add the guest's IP address to the Vagrantfile:
config.vm.network "forwarded_port", guest_ip: "192.168.33.10", guest: 8080, host: 8080

Related

Vagrant scotch box not working

I installed vagrant 2.0 with Virtualbox on a Windows 7.
I followed the https://box.scotch.io/ guide but and I cannot access the webserver at http://192.168.33.10:8080
I entered the box via ssh and says that apache2 is not installed and neither is php. They should be, right?
My VagrantFile
Vagrant.configure("2") do |config|
config.vm.box = "scotch/box"
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.hostname = "scotchbox"
config.vm.synced_folder ".","/var/www", :mount_options => ["dmode=777", "fmode=666"]
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
end
you do not need forward_port if you use a private network, when you have a static ip, you will reach the server directly using the IP; or you could with the forward port, but certainly not a mix of your VM static IP and the forwarding port to your host machine; with your setup either http://192.168.33.10 or http://127.0.0.1:8080 would work, but I'd just recommend to remove the forward port and just use the VM IP
you dont need config.ssh parameters, just login with the ssh key.

Cannot access site from host machine on a vagrant virtual machine

I setup a nginx+mysql puphpet/vagrant image, with a virtual host "test.com". I mapped test.com to 192.168.56.101 on the host machine, and put an index.php file inside /var/www/test.
However, when I try opening the browser at the address test.com on the host machine I get no response. I feel like I'm missing something really simple, because I can ping 192.168.56.101. I also checked nginx logs on the virtual machine, but they are empty. Any clues where the problem might be? Am I not supposed to use it this way? I can ssh into vagrant just fine, and also I can access the mysql db.
In your Vagrantfile, you need to forward the port to your host machine.
For example map port 80 of the Vagrant machine to port 8080 of the host machine.
config.vm.network "forwarded_port", guest: 80, host: 8080

PhpStorm + Vagrant + Symfony2

I want to display Symfony2 page using vagrant box.
If i turn on vagrant box (vagrant up), and use by ssh command "php app/console server:run"
Then i see info " Server running on http://127.0.0.1:8000 "
and if i go to the website "192.168.33.10" or "127.0.0.1" or "127.0.0.1:8000" on host machine, Then i don't see symfony2 site.
Question: How is correctly configuration the vagrant file, that allow me see symfony2 site ?
Its keys lines in my vagrant file .
....
config.vm.box = "symfony-v0.2.0"
config.vm.network "forwarded_port", guest: 8000, host: 80
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.network "public_network"
config.vm.synced_folder "./", "/var/www/"
....
Add informations:
==> default: Mounting shared folders...
default: /var/www => C:/Users/Wojtek/PhpstormProjects/DragonisProject/Symfony2Project
default: /vagrant => C:/Users/Wojtek/PhpstormProjects/DragonisProject/Symfony2Project
Delete line config.vm.network "private_network", ip: "192.168.33.10" from Vagrantfile.
Then start server server as php app/console server:run 0.0.0.0:8000 it will listen to all ip addresses and access it as http://localhost because you have port 8000 forwarded to port 80 on host machine.
1) Make sure you set your local host file to point your site to the correct vagrant IP (192.168.33.10), if applicable. Otherwise just use the IP directly.
2) If your vagrant instance is running, the issue is likely due to your apache conf within the vagrant box.
Make sure you have the proper Apahce/nginx vhost in place for your site.

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

I am trying to run ReactPHP through a Vagrant Box.
I am having problems accessing port 8080 through the host machine
This causes me to get an http-not-found, or a websocket onclose (1006) error.
I am able to: $ telnet 127.0.0.1 8080 with the service running.
I am not able to access http://192.168.33.10:8080
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();
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.

Vagrant VM shows "SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.1" in browser when navigating to "127.0.0.1:2222"

Why ?
I've set up an extremely simple server, like 50 times before, but this is my first try with Vagrant.
Current behaviour:
Vagrant VM shows "SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.1" in browser when navigating to "127.0.0.1:2222". 2222 is the standard port for the first Vagrant VM.
Expected behaviour:
127.0.0.1 should show the index.html/php of the apache2 web folder.
When you run a vagrant up, you'll see a message showing [default] Forwarding ports... -- 22 => 2222 (adapter 1). ssh is listening on port 22 on your VM, you can ssh to port 22 (default) from your local box to connect to the VM; but if you have configured the VM to allow external access, then anyone else on another machine needs to use your IP address but port 2222 to connect to the VM.
This might be useful if you ran the VM on a box in work, but wanted to be able to access it when you were at home, or in a client's office, or even from another machine in your work's network.
Using a browser to connect to the SSH port 2222 will redirect to port 22 on the virtual box, but it's ssh that's listening on this port; and a web-browser isn't the right tool for an ssh connection.
Personally I use putty as my ssh client, but with vagrant you can also issue a vagrant ssh after starting the virtual machine.
Additional
The values of port forwarding
If you set
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.network :forwarded_port, guest: 443, host: 8443
in your vagrantFile, and you've allowed public access to the VM, then you can access the webserver on the VM from home, a client or another box in your work network by pointing the browser to port 8080 (or 8443 for https) rather than the more normal default ports 80 and 443... which can be particularly useful for running demos from a laptop that you can take to a client site while your VM is running on a bulky desktop under your desk in work
MarkBaker's answer solves the problem, but Vagrant 1.2.7 (and newer) needs this syntax:
# Forward guest port 80 to host port 80
config.vm.forward_port 80, 80
The full Vagrantfile (you can find it where your box file is) would look like this:
Vagrant::Config.run do |config|
# Forward guest port 80 to host port 80
config.vm.forward_port 80, 80
end
This example would route a request on port 80 (standart port) in your host OS to port 80 in your Vagrant VM. The result is, http://127.0.0.1 in the host browser will show you the localhost of the VM.
When working with multiple VMs please notice that you should use a unique port for every VM.

Categories