I wrote a php websockets server using the library Ratchet that I call with javascript using the websocket object.
Everything worked perfectly locally but it was impossible to run my project on my Debian server under apache.
To enable websocket connections I read that I have to use mod_proxy_wstunnel module. So I rewrite my apache conf for my subdomain api.domain.com like this:
<VirtualHost *:80>
ServerAdmin admin#admin.admin
DocumentRoot /var/www/api.domain.com
ServerName api.domain.com
# Enable Websocket connections on port 8888
ProxyPass "/wss/homews/" "ws://api.domain.com:8888/"
<Directory /var/www/api.domain.com>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Satisfy all
</Directory>
</VirtualHost>
Then I call my php script with this code insight that start Ratchet websocket server:
// ...
// Some code ...
$app = new Ratchet\App('localhost', 8888);
$app->route('/wss/homews/', $myClass, array('*'));
$app->run();
Then when I try to connect to it on the javascript client side with the url ws://api.domain.com:8888/wss/homews/ I always get Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT.
Do you have any ideas how I could debug this type of error ? Are there any logs on apache showing a bad configuration?
Your above configuration has to be changed at least to:
<VirtualHost *:80>
ServerAdmin admin#admin.admin
DocumentRoot /var/www/api.domain.com
ServerName api.domain.com
# Enable Websocket connections on port 8888
ProxyPass "/wss/homews/" "ws://localhost:8888/wss/homews/"
<Directory /var/www/api.domain.com>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Satisfy all
</Directory>
</VirtualHost>
And on the javascript client side to the url ws://api.domain.com:80/wss/homews/.
Otherwise probably (or lets say hopefully for a server setup) the target from webserver config api.domain.com:8888 tries to reach upstream the websocket on public IP of the server while websocket is bound to localhost. Eighther it's just not listening there (what is fine as You nromally only want to expose Your webservers ports to the public and not directly Your app ports) or even worse it tries to reach a public NAT IP which involves a trip to a firewall where the port 8888 should be blocked, which may not allow this stuff, can be expensive (eg. AWS EC2) and ist at very least inperformant.
On the other hand You will want to direct the client code to the webserver on port 80 which does the tunnelling to Your websocket app so Your client cannot directly access port 8888 of Your server (it's blocked in the public firewall hopefully - and You are listening on localhost only).
Try with Your original setup and tell how the results changed, maybe there is an additional issue (but You should not get a socket timeout anymore at least and a connection from client to websocket app).
First and foremost, try to simplify things, DO NOT edit or touch any of the configuration files from the server, leave it by default as it is, that will do
I will post all the precautions steps needed in order make the socket up and running, please follow steps one by one, they are as follows:
Check whether your server supports Web Socket on shared hosting, otherwise you may need to go for Dedicated hosting service or Virtual private server(VPS) Eg: AWS, GCP.
If your server supports running a web service scripts like phpwebsocket within a web service, then follow next steps
On the terminal, Update the composer to the latest one, then try to execute $composer require cboden/ratchet
So that the required dependency will load efficiently and do not try to upload any archived dependencies.
In my case, "cboden/ratchet": "^0.4.3" was loaded on localhost, But on production site it was "cboden/ratchet": "^0.4.1"
when compared both from the composer.json file
Try to connect to port 8282, In most cases, the server traffic, both from the inbound & outbound are kept open by default, in case, if the port 8080, could not bind to the TCP
At Last, Try to reboot your cloud hosted server instance and execute the websocket script $php <yourWebSocketScript.php>
I had used mod_proxy_wstunnel to make sure the communications goes encrypted, apart from it,
I was able to up & run the Websocket socketo.me on AWS and our private dedicated server smoothly
Thanks for contacting, I do appreciate your efforts, apart from your concerned comment to reply via johannchopin#protonmaildotcom, this is not my part of the business ethics
Edit
$app = new Ratchet\App('localhost', 8888);
$app->route('/wss/homews/', $myClass, array('*'));
$app->run();
Try changing the manually assigned localhost from your <yourWebSocketScript.php> from the above snippet to
$app = IoServer::factory(
new HttpServer(new WsServer(new Chat())),
8282
);
$app->run();
Try binding to the HttpServer, it defaults to localhost or 0.0.0.0 and run (if necessary, eliminate $app->route())
On your code you need to modify
Port Number in yourWebSocketScript.php
Change from localhost to the Server IP or DNS in someChatFilexyz.php
Eg:
let websocket_server = new WebSocket("ws://my.domain.com:8282");
or
let websocket_server = new WebSocket("ws://xxx.xxx.xxx.xxx:8282");
Kill all the previous established socket specific to port 8282 and then run yourWebSocketScript.php
Here is the partial code of someChatFilexyz.php
jQuery(function($){
// Websocket_Controller
var websocket_server = new WebSocket("ws://xxx.xxx.xxx.xxx:8282");
websocket_server.onopen = function(e) {
websocket_server.send(
...
);
};
websocket_server.onerror = function(e) {
// Errorhandling
...
}
websocket_server.onmessage = function(e) {
...
}
}
Setting up the above config, I was able to run on an Amazon Web Service with LAMP stack configured by me and with private dedicated server with ease.
Note: Any minor changes in the code, you need to restart the running websocket script $php <yourWebSocketScript.php> i.e., either by terminating the running process or killing the process specific to port number assigned
Related
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.
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
I'm searching to configure websocket in Apache web server running in php, using Devristo phpws library to run websocket worker.
When I run php file in the server it gives me this string:
Resource id #442015-12-22T16:41:16+00:00 NOTICE (5): phpws listening on ssl://172.31.29.79:12345
In front-end, build on AngularJS, I tried to established connection with:
var dataStream = $websocket('wss://subdomain.domain.com');
Google Chrome browser's console gives me this error:
WebSocket connection to 'wss://subdomain.domain.com/' failed: Error during WebSocket handshake: Unexpected response code: 200
I've an EC2 AWS instance where I've hosted my source code and I've configured AWS Route 53 with a record set that point to the public IP of the instance through a subdomain.
I don't know how configure a correctly reverse proxy to allow communication.
I tried to set Apache server with a Reverse Proxy, but I think I didn't configure it in the right way.
This is the configuration.
I've created a file in site-avaible calls websocket-ssl.conf and linked in site-enabled with this configuration:
<VirtualHost *:80>
ServerName subdomain.domain.com
ProxyPass / ssl://172.31.29.79:12345/
ProxyPassReverse / ssl://172.31.29.79:12345/
</VirtualHost>
Someone can help me in this? If you want others information ask me :)
Thank you very much
I solved with this configuration.
set a record CNAME calls subdomain.domain.com in AWS Route 53 that
point to my ELB dns name;
open port 12345 in ELB listeners;
in front-end I established connection with:
var dataStream = $websocket('wss://subdomain.domain.com:12345');
That's all
I'm trying to create a live web server on my Windows 8.1 computer.
I am connected directly to my modem using ethernet (I do have a wireless router) but I am not connected to it on this computer (desktop).
I have XAMPP working and my website appears at http://localhost/home
However, if I put in my IP from www.whatismyip.com it does not load my web server.
What am I missing?
You need to create a Port Forwarding for Port 80 to your Computers local IP Address. There should be a Admin Panel for your Router (normally the Gateway - check out with Start - Run - cmd -> then insert "ipconfig" and check out the Gateway.
And i guess in XAMPP the Internet Access is blocked. But this is a simple Apache Server, so you need to open your httpd.conf File (Should be: “c:\xampp\apache\conf\extra\httpd-xampp.conf)
Search here for:
There should be a "Deny from all" - add a # in Front to deactivate that rule.
Restart your Xampp and it should work.
Here you find more Information about Port-Forwarding:
https://managewp.com/how-to-access-a-local-website-from-internet-with-port-forwarding
Any maybe you will also need to activate Port 80 on your Firewall (depends on your Configuration)
You need a method of telling request sent to your public ip to be forwarded to the private ip of the web server. Try logging into you device (router etc) and setting this up.
I currently have two apps at AppFog, they are.
http://sru-forums-prod.aws.af.cm/ and http://sru-home-prod.aws.af.cm/
I have haProxy running locally on my computer, this is my current config file.
global
debug
defaults
mode http
timeout connect 500ms
timeout client 50000ms
timeout server 50000ms
backend legacy
server forums sru-forums-prod.aws.af.cm:80
frontend app *:8232
default_backend legacy
The end-goal is that localhost:8232 forwards traffic to sru-home-prod, while localhost:8232/forums/* forwards traffic to sru-forums-prod. However I cant even get a simple proxy up and running.
When I run HAProxy off this config file I receive AppFog 404 Not Found at localhost:8232.
What am I missing, is this even possible?
EDIT:
New config works but now i have a port 60032 coming back in the response.
global
debug
defaults
mode http
timeout connect 500ms
timeout client 50000ms
timeout server 50000ms
backend legacy
option forwardfor
option httpclose
reqirep ^Host: Host:\ sru-forums-prod.aws.af.cm
server forums sru-forums-prod.aws.af.cm:80
frontend app *:8000
default_backend legacy
The reason you are getting an AppFog 404 Not Found is because applications hosted on AppFog are routed by domain name. In order for AppFog to know what app to serve you, the domain name is required to be in the HTTP request. When you go to localhost:8232/forums/ it sends localhost as the domain name which AppFog does not have as a registered app name.
There is a good way to get around this issue
1) Map your application to a second domain name, for example:
af map <appname> sru-forums-prod-proxy.aws.af.cm
2) Edit your /etc/hosts file and add this line:
127.0.0.1 sru-forums-prod-proxy.aws.af.cm
3) Go to http://sru-forums-prod-proxy.aws.af.cm:8232/forums/ which will map to the local machine but will go through your haproxy successfully ending up with the right host name mapped to your app hosted at AppFog.
Here is a working haproxy.conf file that demonstrates how this has worked for us so far using similar methodologies.
defaults
mode http
timeout connect 500ms
timeout client 50000ms
timeout server 50000ms
backend appfog
option httpchk GET /readme.html HTTP/1.1\r\nHost:\ aroundtheworld.appfog.com
option forwardfor
option httpclose
reqirep ^Host: Host:\ aroundtheworld.appfog.com
server pingdom-aws afpingdom.aws.af.cm:80 check
server pingdom-rs afpingdom-rs.rs.af.cm:80 check
server pingdom-hp afpingdom-hp.hp.af.cm:80 check
server pingdom-eu afpingdom-eu.eu01.aws.af.cm:80 check
server pingdom-ap afpingdom-ap.ap01.aws.af.cm:80 check
frontend app *:8000
default_backend appfog
listen stats 0.0.0.0:8080
mode http
stats enable
stats uri /haproxy