How do I make node.js work with vagrant share? - php

I built a small chat using PHP and node.js (using the socket.io library)
Essentially I use node.js for the server of the chat and PHP to handle the actual webpages.
Vagrant has the option to share your HTTP server with users: https://www.vagrantup.com/docs/share/http.html
The HTTP server is running on port 80 and node.js is running on port 3000.
On the chat.php page, I have this line of code:
socket = io.connect("http://localhost:3000");
When I execute the vagrant share command, it provides a URL which you can provide to other people and they will be able to access the site.
So given that URL, I edit the line of code mentioned above to include that URL:
socket = io.connect("http://ugly-elk-1232.vagrantshare.com:3000");
and then I start SSH into vagrant and start node from there.
However it doesn't work. On the chat page I can see timeout errors when socket.io tries to access port 3000.
Here's the error I get in the console (in chrome):
GET http://ugly-elk-1232.vagrantshare.com:3000/socket.io/?EIO=3&transport=polling&t=Lcp9sZh net::ERR_CONNECTION_TIMED_OUT
(the URL is random and will change every time I run vagrant share, but I always update it on the chat page)
Here's what's in my vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "scotch/box"
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.vm.network "forwarded_port", guest: 3306, host: 3306
# Optional NFS. Make sure to remove other synced_folder line too
#config.vm.synced_folder ".", "/var/www", :nfs => { :mount_options => ["dmode=777","fmode=666"] }
end
Is there any way to make this work and allow port 3000 to be shared and not just port 80?
To be clear, the actual webpages are served perfectly fine. It's just that node is not accessible when I use vagrant share.
Edit:
I managed to somewhat solve the problem.
I use vagrant share --http 80 in one window and vagrant share --http 3000 in another.
Then I change the URL so that it connects to the node server that was shared on port 3000.
So the code then looks like this:
socket = io.connect("http://abc123.vagrantshare.com");
Including :3000 (the port) in the URL stops it from working. (not sure why, but I don't think this is the issue).
The problem now is that socket.io now resorts to polling and no longer uses websockets. I tried to force it to use websockets, but it gives 400 bad request every time it tries. Polling isn't necessarily bad and it works, but I wanted it to use websockets since I need to test how the site will behave when it's actually up and websockets are what will be used in that case.

because of
socket = io.connect("http://localhost:3000");
you're listening only locally and the app works only from your VM (it does not even work if you try to access it from your host machine)
If you want to share the app directly from port 3000 you should be able to run vagrant share as
vagrant share --http 3000

Related

Installing SSL in NodeJS (didn't send any data, ERR_EMPTY_RESPONSE)

I have downloaded a SSL certificate that I have recieved from app.zerossl.com and placed it in the same directory as my main node script; and have used this code to install it.
var fs = require('fs');
let options = {
cert: fs.readFileSync(__dirname + '/certificate.crt'),
ca: fs.readFileSync(__dirname + '/ca_bundle.crt'),
key: fs.readFileSync(__dirname + '/private.key')
};
My configuration for running the server is the following:
var express = require('express');
var app = express();
var server = require('https').createServer(options, app);
var io = require('socket.io')(server);
Now i'm running MYSQL and PHP on XAMPP with the port set to 1337. In my modem i've set the DMZ to my Computer/Servers' internal IP Address. When I try to access my domain over the internet it comes up with an error. (didn't send any data, ERR_EMPTY_RESPONSE) assumingly from my Node JS server.
Now when I go on the https version of my website using the address bar, it comes up with a warning then redirects to my XAMPP server. The port is not set on the address bar, so i'm not sure why it's redirecting to the XAMPP server.
I'm wondering why isn't my SSL working and why is it redirecting to my XAMPP server instead of using the NodeJS server when I place in https?
Since you are running two servers and one public entry point, then you will need to use something like nginx to be able to access both from your external IP.
XAMPP is probably taking priority over the express server which is why is going there.
NOTE: If you are dealing with HTTPS, make sure to add router rule to utilize port 443.
Here are some docs on how to host two servers in one. In this case, it's two websites but you can change it to make it works for one website and one backend server since this is specifically for routing to different ports.
NOTE: You can skip server_name and just add the port forward in each configuration. This way you can have one port forward to 1337 for your xampp, and another port for your express server.
https://webdock.io/en/docs/how-guides/how-configure-nginx-to-serve-multiple-websites-single-vps#:~:text=If%20you%20are%20using%20a,to%20host%20all%20your%20domains.

Running PHP server without port

I have a typo3 folder in /var/www/my_folder, I want to run php server inside my_folder,
I run php -S localhost:8080 but I have problems later with URLs, I want get rid of :8000.
when I try php -S localhost I get
Invalid address: localhost
How can I manage this ?
You can run the PHP web server on on port 80, which is the default for HTTP, like this:
php -S localhost:80
Then you can visit http://localhost in your web browser.
However, it's common practice to use a different port for local development so that you don't have any conflicts between your dev environment and anything else which may be running on your machine. If you're sure you're not using port 80 anywhere else, feel free to use it.
What you can't do is omit the port number like you have in your second example.
Without explicite port usage, browsers use port 80 for HTTP and 443 for HTTPS. So if you want your URLs you use 'later' to not include the port, start the PHP interpreter with the one you need the URLs for.

Why orientDB connection to localhost of docker is refused

I did build orientDB and appserver on docker. They are running as well. This is list containers on docker:
core#localhost ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS
NAMES
01abef0204a7 fxrialab/appserver:latest /usr/sbin/httpd -D F 30 minute
s ago Up 30 minutes 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
appserver
f6d0631bb092 fxrialab/orient:latest /bin/sh -c cd /opt/o 30 minute
s ago Up 30 minutes 0.0.0.0:2424->2424/tcp, 0.0.0.0:2480->2480/tcp
appserver/db,orient
ebc1386250b9 fxrialab/data:latest /usr/sbin/sshd -D 30 minute
s ago Up 30 minutes 0.0.0.0:2200->22/tcp
data
Also i did create database on orientDB. I think orient is working fine. However when i login to my website. I got errors "Connection refused" like this:
Socket error #111: Connection refused
• vendors/OrientDB/OrientDB.php:254
OrientDBSocket->__construct('localhost','2424',30)
• apps/models/DB.php:11 OrientDB->__construct('localhost','2424')
I dont know what reason althought i did test fine on local when i enabled server.bat file. Ah, i using orientdb-1.7
Thank for advance !
localhost is a synonim for ip address 127.0.0.1
your binds are for 0.0.0.0
I am not familiar with OrientDB, but I believe that what you are facing is ports access problem (server configuration), not software problem (in this case software is OrientDB). Most servers are configured to block all ports, unless specifically allowed - for security reasons, so that noone can connect to the server on those blocked ports.
So, what is probably happening is that your local server is fine with using those ports, and remote server is secured and thus prevents connections.
Docker is running a virtual machine with separate network interface for each containers. Thus if your appserver connects to localhost, it won't see the orientdb.
You need to update your configuration to lookup the proper environment variable for the link (see http://docs.docker.com/userguide/dockerlinks/) or just use the db host. Docker injects a hostname into /etc/hosts for each linked container.
I've found cause of this issue, because im config HOST to connect is wrong. It's not localhost because docker uses its own internal IP's. In ssh type docker inspect orient then use global variables for config to access to IP & Port.

Vagrant server not showing up?

I'm using Vagrant to run a server for me. So I followed this tutorial and when I open up my browser to go check. I get Oops! Google Chrome could not connect to 127.0.0.1. I've also tried localhost and my IP address. Along with a mixture of different ports. The odd part is when I do
curl 'localhost' or curl 'http://127.0.0.1:80' I get
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>
I do know that's normal, but I can only get that to show on the command line. Any ideas?
You must set up port forwarding in your Vagrantfile. See https://docs.vagrantup.com/v2/networking/forwarded_ports.html
Example:
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 80, host: 8080
end
I've had issues using Vagrant and got some help.
Here is my post and how i made it work:
Setting up Vagrant with PuPHET failure

PHP can't connect to localhost XMPP server on port 5222

I've set up an ejabberd install locally on my Windows box, where I also have Apache, PHP and MySQL. I've also confirmed that it works great using Digsby, and have kicked the tires a bit by creating some users, sending some messages, etc. All good.
However, PHP can't open a stream using stream_socket_client to port 5222. Even at its simplest level:
stream_socket_client("tcp://localhost:5222", $errno, $errstr, 30, STREAM_CLIENT_CONNECT);
Returns a timeout error. However, again, connecting with an IM client to localhost on port 5222 works fine. (Using stream_socket_client to open a simple connection to localhost on port 80 also works.)
Any ideas? I'm stuck!
selinux needs to be off, or allow apache to talk to xmpp
Many servers don't listen on the loopback device by default, or only listen on ::1 or 127.0.0.1 and have localhost pointing to the other. Check by doing:
% netstat -an | grep 5222
and checking the output for a LISTEN line that shows where your server is listening.
Finally, try using the IP address of your box explicitly as the connection hostname.
Sometimes you just need to peek on the line to see exactly what is going on. Windump(tcpdump) is your friend in these cases.

Categories