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

1

6

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.

Chiquitachirico answered 18/9, 2019 at 9:27 Comment(0)
U
2

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.

Uhf answered 18/9, 2019 at 10:18 Comment(4)
Hey! This answer is pretty old, but I did want to provide some guidance here. You can get this done if you can get your host to cooperate. Either just run a systemd unit and open the port, use a cron with the port open, or you can use Include files to enable ProxyPass directives for one VirtualHost docs.cpanel.net/ea4/apache/…Furfuran
@PixelPerfect: Thanks for chipping in your thoughts. I think this answer still holds up. Your suggestion It's technically possible but is practically improbable. Either opening up TCP port (route 1) or allowing customer to touch a common server's config (route 2) would potentially generate security risk for their server. So unless you're a really important customer or a really good friend, it's not very likely that they would simply grant you that.Uhf
The intent of the include files is to isolate changes to a particular user. Also, most cPanel that I've ever logged into are either not behind a firewall, or running CSF, which allows for quickly opening ports right from WHM. By default, a service should be fine to just grab a port, provided you're not on AWS, GCP, MSAzure, or another VHost that has a default firewall set.Furfuran
Officially, not supported by the cPanel product, correct, but there are workarounds. They would require that the host cooperate, and, yeah, many won't, but if you're using a shared server from a smaller host, it's actually more likely than you'd think.Furfuran

© 2022 - 2024 — McMap. All rights reserved.