skip to Main Content

I have developed a web app which shows information in real time from certain actions carried out by different users. For this I use websockets, built in PHP, in a local environment (WAMP) and works fine, but I need this to also work on an external server (web hosting service), which I only have access to through the CPanel and FTP.

Locally I make the websocket work executing next code line through Windows’ CMD:

C:wamp64binphpphp7.2.10php.exe -q C:wamp64wwwmyappwebsocket_daemon.php

My question is, how can I achieve the same result in CPanel, or maybe there is another way?

2

Answers


  1. 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:

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

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

    Login or Signup to reply.
  2. For that purpose you will need to have CLI (Command-Line Interface) access to the (Linux) server involved. Assuming that you have such access, running the WS service would look something like

    ./websocket_daemon.php
    

    The small script assumes that you are in the appropriate folder. However, you need to resolve a few things before you get there:

    Step 1: SSH support on your machine

    You will need to ensure that your OS supports SSH. Your OS appears to be Windows, so you will need to either install Putty of Git Bash. Read about these technologies

    Step 2 Generate an SSH key

    In your CPanel, you will need to generate SSH keys:

    • Click on Manage SSH Keys
    • Click on Generate a New Key
    • Use the settings you prefer in order to generate a key, don’t worry, you can remove the SSH keys at any time and recreate them if you realize that you prefer a different way to generate them

    Read more here: https://docs.cpanel.net/cpanel/security/ssh-access/

    SSH keys are composite keys, that is, it consists of a private and a public key. You can share your public key with anyone, but never ever send your private key to anyone. It should be on your computer and possibly saved to backups. Read more about SSH keys here: https://sectigo.com/resource-library/what-is-an-ssh-key

    Step 3: Ensure that your computer uses the SSH keys you have generated for CPanel

    You will need to tell your OS where the SSH key-pair is located, luckily this is not a new problem, see an exhausting discussion about this topic here: https://serverfault.com/questions/194567/how-do-i-tell-git-for-windows-where-to-find-my-private-rsa-key

    Step 4: Test your SSH

    Run the following command into your CLI that supports SSH:

    ssh <username>@path
    

    If there is no error, then you have successfully tested SSH and you are almost ready to proceed further

    Step 5: Upload your Websocket script

    You can do this via FTP, as you already know, but you can also do it via SCP. scp would not only use your newly created SSH connection and having fun with it, but it’s also secure. Syntax:

    scp file.txt [email protected]:/remote/directory
    

    Step 6: SSH to the server

    See Step #4.

    Step 7: Navigate to your file’s location

    Step 8: Ensure that you have the rights to run it

    See more here: https://alligator.io/workflow/command-line-basics-file-permissions/

    Step 9:: Execute the file

    Run

    ./websocket_daemon.php
    

    If this succeeded, then the job is basically done, you will need some script to run it upon startup and to manage it, but this is not strictly related to the question.

    https://oracle-base.com/articles/linux/linux-scripts-running-in-the-background
    https://smallbusiness.chron.com/run-command-startup-linux-27796.html

    However, if the issue is not resolved yet, read further

    Step 10: Ensuring WS support on server

    You will need to set up your own WS support. Since you have managed to do so locally on your Windows, hopefully your know-how will work on the remote Linux as well. If not, read more here:

    PHP Websocket server in Linux hosting

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search