Connecting to a WebSocket PHP server (code 1006 "Abnormal Closure") - php

I cannot establish a connection with the PHP WebSocket server, client closes it with code 1006 "Abnormal Closure".
Client code:
var host = "ws://example.com:1234/test/server.php";
Server Code (test/server.php):
$host = 'tcp://example.com:1234';
I tried different WebSocket PHP libraries.
I tried different hosts ("127.0.0.1", "0.0.0.0", "example.com").
sudo netstat -plnt shows that port is listened.
Appropriate TCP port is opened (firewall settings in ISPConfig 3).
But connection still not working. Any ideas?
Thank you in advance.

Related

WebSocket connection works in local machine but not in server

I am using the Laravel Framework and BrainSocket to open up a websocket service in a port via the following command line:
php artisan brainsocket:start --port=7778
Afterwards, I connect to the websocket on the client-side, like so:
var websocket = new WebSocket('ws://127.0.0.1:7778/');
At first I was developing this application on my local machine and everything was working fine. However, on the production server (CentOS 7 + Nginx), when I run the above line the following error message appears:
WebSocket connection to 'ws://127.0.0.1:7778/' failed: Connection
closed before receiving a handshake response
I tried changing the IP for the server's IP address, I have already checked if the port was being used and I made sure socket support was enabled. But whatever I do the same error message appears.
It worked once I used port 443 instead.

PHP - Can't connect to a remote MySQL database

I have a MySQL database, hosted by me on a Windows server, and I would like to query it from a remote webserver. When I try to connect to the MySQL database using PHP (I know, I should be using mysqli):
$connection = #mysql_connect("203.0.113.0:3306", "username", "password");
With or without specifying the port, after a long time of loading, I get this error with an errorno of 2003:
Can't connect to MySQL server on '203.0.113.0' (4)
Here is what I get when I run netstat -n in command prompt on the server that is hosting the MySQL server: http://pastebin.com/pbRJNqCZ. It filled up the screen so I couldn't copy everything, but I know that everything else was (I saw a couple ports with a value of 3306, which is the MySQL port):
TCP 127.0.0.1:port 127.0.0.1:port ESTABLISHED
When I run netstat -a | find "LISTENING" I get: http://pastebin.com/Lqj2BrQK
Here's what I know so far:
It isn't an error with the MySQL server not being up, because I can connect to it locally.
It isn't an error with the webserver not being able to connect to MySQL databases, because I can connect to other databases
It isn't an authentication error (The username and password are correct, and the remote server has permission)
It isn't a port forwarding error, because the port 3306 is fowarded on both TCP & UDP.
It isn't an error with being able to connect to the machine the server is hosted on, because I can ping it fine.
The server isn't only listening on the localhost interface. skip-networking and bind-address are commented out in my my.cnf file.
How could I edit my connection code, or edit my MySQL server to fix this error?
Summarizing our discussion/chat:
Bind the network address 0.0.0.0 in my.cnf: bind-address = 0.0.0.0 and ensure skip-networking is commented out or not present.
Check netstat -a | find "LISTENING"
According to your pastebin there is a service listening on 3306. Test if the port is reachable on the NIC address from the server itself. This way an external firewall does not take effect. A simple test is to try a telnet connection to that port. More detailed information can be catched by the tool nmap. Nmap has displayed the mysql port as filtered. This adverts to a problem with a local packet filter, in this case the Windows firewall.
Ensure the Windows firewall is configured to allow public access to TCP port 3306 from all or a specific machine. Setup a rule in public profile or, if both servers are controled by the same domain controller, in domain profile. When the access from local machine is successful try the same from the remote web server.
If you fail to properly configure remote access to MySql port, consider to establish a SSH tunnel between the two machines. Once established you can access to MySql as if it were on the local machine. The port is then forwarded via the tunnel and on the database server side you can access the service on localhost loopback IP.

PHP Client can't connect to RabbitMQ server on localhost

OS: CentOS 6.4
I am trying to connect to the RabitMQ server using the php client as follows,
$connection = new AMQPConnection('10.1.150.109', 5672, 'guest', 'guest');
$channel = $connection->channel();
But when I ran the script from the browser, it gives me this,
exception 'PhpAmqpLib\Exception\AMQPRuntimeException' with message 'Error Connecting to server(13): Permission denied ' in /var/www/html/event/vendor/videlalvaro/php-amqplib/PhpAmqpLib/Wire/IO/StreamIO.php:27
netstat show this,
tcp 0 0 :::5672 :::* LISTEN 10776/beam
In this post, this guy gives the answer implicitly, Client can't connect to RabbitMQ server on localhost. But he has not described the procedure which he followed to fix the issue.
I thank you in advanced for anyone who can help me in this regard.
Since I don't like the accepted answer, here's one I think is better.
Disabling SELinux is a hack. It may work but it's probably not a good idea. What isn't immediately obvious from the question (or the other question it references) is HOW the php client is being run. I.e. from the command line or via a browser.
SELinux by default won't allow httpd (i.e. apache) to connect to port 5672.
In my case, running the php script from the command line works - the connection is accepted. However, running it from a browser fails because of this SELinux policy.
I imagine that "reconfiguring the listen address from 0.0.0.0 to 127.0.0.1" is a reference to the hostname parameter, which in my case is set to "localhost" rather than an explicit IP address. (For sure 0.0.0.0 is going to fail!)
You can enable httpd to access port 5672 in SELinux: https://serverfault.com/questions/563872/selinux-allow-httpd-to-connect-to-a-specific-port
what happens if you
telnet 10.1.150.109 5672

Remote MySQL Connection Issues - Lost connection to MySQL server (110)

I'm trying to access a MySQL database on server X from a php script on server Y. Both are dedicated cPanel servers that I have root access to. Here's what I've tried:
On server X I put the IP address of server Y in the "Additional MySQL Access Hosts" feature of WHM.
On server X I logged into the cPanel for the account hosting the database I'm trying to connect to and I entered server Y's IP on the "Remote Database Access Hosts" page.
On server X I whitelisted server Y's IP in the firewall and opened incoming/outgoing port 3306 TCP.
On server X I added server Y's IP address to the /etc/hosts.allow file
Despite all of these things I've tried, whenever I try to run the script on server Y I get the timeout message:
Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 110
This is my PHP code:
$host = '123.456.789.0'; //server X's IP
$db = 'user_test';
$user = 'user_test';
$pass = 'password';
if(mysql_connect($host, $user, $pass)){
mysql_select_db($db);
}
else die(mysql_error());
Thanks!
Not sure how much of a help it will be, but try to open my.cnf and replace the bind-address setting (127.0.0.1 OR localhost) with your live server ip.
Have you contacted your hosting provider to see if they can help?
Is there a firewall on the linux machine you're connecting to?
Did you test your my.cnf file with 127.0.0.1 and localhost?
What version of MySQL are you running on both machines? Can you upgrade them?

Mysql Connect to external/remote server outside the VLAN

I am trying to connect to an external mysql server from a computer which is inside a vlan where my application will run. The remote server is not a member of the vlan. The following is what I have tried so far.
Mysql Port forwarding from my vlan server
ssh -L 3306:my-vlan-server-ip:3306 user-at-external-server#external-server-ip
In this case I get a ssh timeout message.
Tried to do it directly from my php mysql_connect I get mysql error #111 yet I have already edited the my.cnf as:
#skip-networking
bind-address =my-vlan-server-ip
My php db connect script
<?php
$conn=mysql_connect("external-server-ip","user","pass");
if($conn)
{
echo "success";
}
else
{
echo "fail";
}
?>
Kindly someone help.Let me know where I am going wrong. Thanks.
If I understand you correctly you are running the ssh client on the vlan server (=my-vlan-server-ip)?
The local tunnelling (-L) basicly forwards the first port to the given port on the given address, i.e -L 80:someserver:8080 forwards port 80 on the local machine to port 8080 on someserver... so if you want to connect to 3306 on the remote server via ssh you do:
ssh -L 3306:external-server-ip:3306 user-at-external-server#external-server-ip
(or just -L 3306:localhost:3306, localhost will then refer to the server to which you are connecting, ie external-server-ip)
localhost:3306 is then forwarded to external-server-ip:3306
in the php script running on my-vlan-server-ip you then connect to localhost:3306, which is then forwarded to external-server-ip by ssh...

Categories