Search code examples
phpcpanellampratchetphpwebsocket

How to setup and run a PHP WebSocket service on a cpanel-based shared hosting platform?


I have created a Chat application in Ratchet PHP. It runs fine on local machine using WAMP. I want it to setup on live server.

On my server I have:

  • PHP Support
  • SSH access
  • Port 9000, which is opened for OutBound connections.
  • Sub-domains

What I don't have:

  • No port for InBound connections.
  • No root access in SSH. Say I cannot see/edit iptables

I ran php server.php which gives success message about the server is started and listening at port 9000. But when a HTML page tries to connect it using ws://domain:9000 it runs into error-

Firefox can’t establish a connection to the server at ws://domain.com:9000/

I googled a lot and it appears that what I need is a port for inbound connections.

But according to this answer the thing I need is PHP support for Ratchet to work on shared hostings. Can anyone explains what I really need and Is there any workaround If it's not availble in the list of things I have on my server.


Solution

  • Basically, No

    It is not likely for a shared hosting environment (i.e. Apache with VirtualHost config, PHP, MySQL, and a CPanel interface) to support your websocket application.

    For websocket to work, you need to either:

    1. have a port dedicated to websocket in-bound connections; or
    2. have a HTTP/HTTPS server that knows when to upgrade a connection and proxy pass to your websocket application.

    The first route requires the server to allow in-bound connection to a certain port number. This is a potential security issue for the hosting provider and, thus, is unlikely for your vendor to grant you that.

    The second route requires Apache server have with both mod_proxy and mod_proxy_wstunnel installed and enabled. It also require you to use ProxyPass config, which cannot be overridden by .htaccess configs at all.

    So unless your hosting grants you the permission to touch the Apache main configuration (or would apply such change for you), you're pretty hopeless.

    Suggestion

    To run your own websocket service, you should think about using Virtual Private Server services such as Amazon EC2, DigitalOcean VPS.