3
votes

First thing, I could see out there has already solved questions around the problem.

PHP web socket unable to bind socket

Issue in setting the socket server

socket_bind(): unable to bind address [99] (Ubuntu on Amazon EC2)

and many other resources, all of them said that problem is in-used port or external IP, etc.

But I could not get over it. My server is running on CentOS 6.3 and using CPanel (it is the HP server https://www.hpcloud.com/).

The web socket demo source code what I get is on https://code.google.com/p/phpwebsocket/

My setting is

$master  = WebSocket("127.0.0.1",4444); // even I tried with localhost, they worked same

Here is the step what I tried and the corresponding errors:

1/. I tried following commands to ensure a port which I was using is available to use

$ lsof|grep 4444

$ netstat -a -p -n

But I always got the error although this port 4444 is free

Warning: socket_bind(): unable to bind address [98]: Address already in use in /home/myapp/public_html/websocket/server.php on line 60

2/. The strange thing is when I checked the port 835 which was using by

 crond      1621      root  DEL       REG              252,1               39835 /lib64/libpam.so.0.82.2.#prelink#.B1eyGx

atd        1633      root  DEL       REG              252,1               39835 /lib64/libpam.so.0.82.2.#prelink#.B1eyGx 

The funny thing happened:

Then I ran the server.php on my terminal command shell

 php /home/myapp/public_html/websocket/server.php

the error has gone, instead of it is

Server Started : 2013-08-16 03:19:54

Master socket : Resource id #4

Listening on : 127.0.0.1 port 835

I thought that it started working, but actually it did not, because when I open server.php by the browser with URL

http://mysite/server.php

The error is "permission denined"

Warning: socket_bind() [function.socket-bind]: unable to bind address [13]: Permission denied in /home/myapp/public_html/websocket/server.php on line 60 socket_bind() failed

Edited: About permission, I could open the client.html in same level directory with server.php without problem. The folder code permission is 755 and user is not root. All of my commands was implemented under root privilege

Any help is greatly appreciated :(

1
This sounds like a permissions issue. What user is httpd running as and what user are you running as when you run it from the command line? - Bad Wolf
I have two users: root & myuser, and folder owner is currently myuser. I could run the client.html in same level directory with server.php absolutely, I think that was not the cause - Telvin Nguyen
What I'm suggesting is that when you run it from the command line because the process is running with your privileges it may have access to some resource/file which it needs to execute whereas the user that your webserver (httpd) is running under does not have access to hence the failure. I can't specifically put my finger on what that file/resource is though. - Bad Wolf
I ran command line under root user. In case of port 4444, on client.html I have set setting var host = "ws://localhost:4444/websocket/server.php"; but browser notified that it could not connect to my server, so then I guess my server.php did not work. - Telvin Nguyen

1 Answers

0
votes

I would assume that the problem is that you are attempting to load the Websocket server in your webbrowser. Most likely, there are security settings for the apache user that does not allow your server to listen on uncommon ports.

CentOS usually ships with SELinux enabled by default. Part of CentOS Linux's security policies for Apache are that by default, Apache will only allow services access to recognized ports associated with HTTP (ie, port 80, 8080 etc).

With SELinux enabled, you could allow Apache to listen on tcp port 4444 by adding a rule to allow that inside SELinux using the 'semanage' command:

~$ semanage port -a -t http_port_t -p tcp 4444