last day (23 days later) » 

10:42 PM
0
Q: websockets connections fail with apache and Ratchet

johannchopinI 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_wst...

 
ERR_CONNECTION_TIMED_OUT usually points to a firewall or host resolver error. Can you change new Ratchet\App('localhost', 8888); to be new Ratchet\App('api.domain.com', 8888);? Also, can you check your firewall with iptables -L -n and make sure port 8888 is opened
 
@Raul Ok first error is that the port is not open :sad:. I open it and try again
@Raul Its done adding Listen 8888 to ports.conf but now Ratchtet can't no more listen to it since apache uses this port -> Failed to listen on "tcp://127.0.0.1:8888"
 
Ok, so obviously, you can't have both on the same IP and port. So, if both are on the same server, change the webserver to be on 8888 and the websocket server to 8889 for example.
 
@Raul Ok but then I'm going to be in the same situation as before where I have a port that is not listen by apache but used by Ratchet
 
I hope i'm not mistaken here, but the Ratchet app runs by running it in CLI, no? like php bin/something.php? That has nothing to do with the apache port where your requests are coming in. My apologies if I'm misunderstanding your setup
 
10:43 PM
Yes I call my script in CLI
 
Perfect, meaning that can have any port: 8889 for ex
Do me a favor, change the Ratchet app port to 8889
Then make sure you can access that port via telnet api.domain.com 8889
 
Sadly I don't have telnet on my debian :( I'm trying to do as fast as I can.
 
apt install telnet
 
When I ping to my subdomain it sends me back to my domain
Is that normal behavior already?
 
What do you mean by that?
 
10:54 PM
I get
telnet api.domain.com
Trying IP.IP.IIP.IIP...
Connected to domain.com.
 
hmm, let me check something really quick, not sure about the output
 
And then he ask me user name for my server on ssh
 
Uhm, i thought you're already connected via SSH
Ok, are you on a mac or windows?
 
windows
 
open the cmd, type run.
 
10:57 PM
Thats true does I have to close my other ssh connections?
 
no, you should not
ok, so
Press Windows + R type cmd and Enter
then type: telnet API.DOMAIN.COM PORT
 
Yep and then ?
 
replace port with your ratchet port
 
I made myself misunderstood I run the telnet command from my vps under debian
When I tried again with the port 8889 I got the error => Unable to connect to remote host: Connection refused
 
Ok, so your port is blocked, or not accessible from outside
do me a favor, paste the output of:
iptables -L -n
 
11:04 PM
You're looking for what exactly I'm not going to give you the full output of this command for security reasons.
 
Ok, i wanted to see if your firewall has any blocked ports
But i assume it does
do you know what firewall are you running?
Do you know how to open that port?
The easiest way would be for you to disable the firewall, see if everything works
Then, once everything works, you know the only issue left is firewall, which is always easy to fix
What OS are you running on that server?
Debian?
 
Yep Debian
But I'm not sure if I have a firewall
:)
 
Ok, iptables -L -n ... does that contain just a few lines?
like ...
this is a server with no firewall
 
Ok so I don't have one :)
Is that bad ?
 
heh, well, on production servers, yes
i do recommand to only have ports opened that you need
and filter ssh and other ports that can be used to hack your server
But that is for another day
 
11:11 PM
Allright :p)
 
Now, because you do not have a firewall, that telnet should work
so you changed your Ratchet app to 8889?
Did you restart your php process?
to take the new settings?
yougetsignal.com/tools/open-ports => you can use this site as well to test your server IP and port
 
Mmmh seems that the port is closed I don't understand
 
Then your php app is not running somehow on that port
there's really no other way
If you want to screenshare, that would be great, as things are getting hard to debug without seeing it. It should be very simple though
apache runs on 8888
ratchet app on 8889
and then all you have to do is make sure that you update the correct hostname in all the calls
and the ports opened (which you said firewall is off)
So, all this is very very basic simple setup
 
Mmh my apache (still) run on 80
 
where is this VPS hosted? Do you happen to have any other firewalls on top of that VPN?
heh, restart apache?
run this:netstat -tunalp | grep -i listen
do you see 8888 and 8889 in that list?
 
11:20 PM
Yes both
 
ok, that's good
 
One is just :::8888
 
just to confirm, 8888, last column, you see a number and apache or httpd next to it
and for 8889, you see ID/php ?
hold on, only :::8888 ?
 
Yes I have apache2 for 8888 and php for 8889
 
that's not enough, as that is tcp6
you should see:
 
11:22 PM
but 8889 is on localhost is that right ?
 
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN
 
I just have tcp6 :::8888 LISTEN
 
well, 8889 depends. If you use websockets internally only, so meaning, the clients connected to api.domain.com do not connect to the websocket, then yes. But i don't think that's what you have or want
ok, so let's start there
apache2 does not listen on your port
on tcp
only on tcp6
 
Mmmh ok
Do you now how to fix that ?
 
ok, i think it's time to restart :)
just not to get confused again
So, correc tme if i'm wrong
 
11:24 PM
ok :)
 
You want to go to api.domain.com in the browser and serve a page, right?
on that page, you might have a socket connection, correct?
 
mmh almost the connection should be made on a url of type 'api.domain.com/wss/'
api.domain.com will not really be a webpage
It actually never be called like this
It will*
 
Gotcha. ok, so yeah, i kind of missunderstood a bit
Le tme put togheter a config file for apache
8889, yes, that should be localhost
 
Why not keep 80 ?
 
80 will be needed for api.domain.com
and another port will still be required for your ratchet app
 
11:31 PM
Mmmh ok right :)
 
Wich, damn it, you had it right i think :(
 
What ? :(
 
<VirtualHost *:80>
ServerAdmin admin@admin.admin
DocumentRoot /var/www/api.domain.com
ServerName api.domain.com

# Enable Websocket connections on port 8888
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
ProxyPass /wss/homews/ ws://localhost:8888/

<Directory /var/www/api.domain.com>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Satisfy all
</Directory>
</VirtualHost>
do me a favor, try this config, restart apache
and change your Ratchet back to localhost:8888
the only thing that i think you were missing is the http upgrade headers
which are needed for sockets
 
I use certbot for https is that a problem for the future ?
 
ProxyPass /wss/homews/ ws://localhost:8888/
ProxyPass /wss2/homews/ wss://localhost:8888/
wss:// will be for SSL
 
11:37 PM
But I don't have done anything with ssl for websockets
Does it need more configurations for ssl with ws
?
 
you'll need, but damn, my apache config skills are bad :)
i only use nginx since forever ;)
do try that config for now, see if the regular http and ws:// works
and we can figure out the SSL after
which is wss:// - but i need to look it up, as i really can't remember anything from apache2 :( again, been over 5 years since i used apache at all
nginx for this is better, syntax more elegant, most likely will be faster too :)
oh, and btw, once you set my config, you need to connect to ws://api.domain.com/wss/homews/
without the port
let me know when you deployed the apache config, restarted ratchet
and do try ws://api.domain.com/wss/homews/
cause it should work.
 
And have done the config
set ratchet to port 8888
But I get the previous error -> Failed to listen on "tcp://127.0.0.1:8888":
 
hmm, something else listening on port 8888?
who's giving you that error?
apache???
netstat -tunalp | grep 8888
 
I call ratchet like : new Ratchet\App('api.domain.com', 8888);
Yes apache2 :(
on tcp6
 
ok, this is weird.
1. stop php (ratchet)
2. stop apache2
3. edit ratchet, do new Ratchet\App('localhost', 8888);
4. make sure in httpd conf your virtualhost is set to *:80
5. start apache2
6. start php ratchet app
maybe, after step 2, do
netstat -tunalp | grep 8888
and netstat -tunalp | grep 80
make sure nothing is returned
 
11:49 PM
I have now php listen on 8888
And nothing on 80
 
perfect. that's good
ok
so now start apache
if you get any errors, then you might have some left overs in the httpd config
man, this would have been solved soooo much faster via screenshare :|
 
I got error
Address 8888 already in use:
 
ok, grep -rli '8888' /etc/apache2 ?
/etc/apache2 is your folder for httpd confs?
grep -rli 8888 /etc/httpd/*
or whatever the path is, i am not sure
that will tell you a list of files that contain 8888
then, open those files, and search for 8888
 
OOOh f*ck
 
and it better not be in a VirtualHost :8888 :D
 
11:54 PM
I still have a LISTEN 8888 in ports.conf
sorry
 
all good man
all good
ok, remove that one, restart apache2
we're very close after that, there's only 2 more things we can try
 
Ok now I only have my php that is listenning port 8888
 
perfect! restart apache2
and tell me you have 80 for apache :D
 
Yes on tcp6 :::80
 
only?
Has to be more than that
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN ?
nothing like this?
 
11:58 PM
Nope :(
port 443 is the same
But is like the begin actually
all my websites work fine
 
let me spin up an apache2 server quick
 

  last day (23 days later) »