WebSockets Support for PHP

WebSockets Support for Apache and NGINX PHP

WebSockets provide a continuous full-duplex TCP connection between a server and a client’s browser. This allows instant bidirectional messaging with low latency while supporting traffic through proxies and firewalls.

The platform supports WebSockets through the Shared Load Balancer, NGINX load balancer, Apache application server, and NGINX application server. Apache uses the built-in proxy_wstunnel_module, while NGINX uses its native WebSocket proxy capabilities.

Apache PHP Configure a WebSocket proxy location in httpd.conf.
NGINX PHP Enable the prepared WebSocket upstream and location settings in nginx.conf.

Create the Environment

1Open the environment wizard

Sign in to the platform dashboard and click New environment.

New environment button
Start creating the PHP environment from the dashboard.

2Select the PHP server

Open the PHP tab, choose Apache or NGINX, configure the Cloudlet limits, enter the environment name, and click Create. The source example uses Apache and the name apache-websockets.

Apache PHP environment wizard
Create the PHP environment and allocate its resources.

Wait until the environment is created and appears in the dashboard.

PHP WebSockets environment created
The newly created PHP environment is running.

Deploy the Application

Upload and deploy the WebSocket application through Deployment Manager or from a remote Git or SVN repository.

PHP WebSocket application deployed
The deployed WebSocket project appears in the environment panel.

Open Server Configuration

1Open Configuration Manager

Click Config beside the PHP application-server node.

Apache Config button
Open the server configuration files.

Follow the corresponding section below according to the selected PHP application server.

Configure Apache

Open conf/httpd.conf and locate the prepared WebSocket-support block near the end of the file. Uncomment it and configure the application listener port:

<Location /ws>
    ProxyPass ws://127.0.0.1:<PORT>
    ProxyPassReverse ws://127.0.0.1:<PORT>
</Location>
  • Replace both <PORT> placeholders with the port used by the WebSocket application.
  • The source example uses port 9000.
  • Save the configuration after making the changes.
Apache WebSocket proxy configuration
Configure the Apache WebSocket proxy path and listener port.
Apache WebSocket proxying is provided through the proxy_wstunnel_module included in the default server build.

Configure NGINX

Open conf/nginx.conf, find the #Websocket support section, and uncomment the prepared upstream and location configuration:

upstream websocket {
    server 127.0.0.1:<PORT>;
}

...

location /ws {
    proxy_pass http://websocket;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
}
  • Replace <PORT> with the port used by the WebSocket application.
  • Save the configuration file after the changes.
NGINX WebSocket configuration
Enable the prepared NGINX WebSocket upstream and proxy location.

Configure the Application WebSocket Path

Open the application file that defines the WebSocket connection and update the URI using the following format:

ws://{env_domain}{path_to_ws_file}
  • {env_domain} — environment domain shown below the environment name in the dashboard.
  • {path_to_ws_file} — path used by the application when establishing the WebSocket connection.
Application WebSocket URI
Update the WebSocket URI with the environment domain and connection path.
Secure applications: When the application is served over HTTPS, use wss:// instead of ws://.

Restart and Test

1Restart the application server

Save all changes and restart the Apache or NGINX application-server node.

Restart Apache node
Restart the application server to apply the WebSocket settings.

2Open the application

Click Open in Browser after the application server returns to the running state.

Open application in browser
Launch the deployed WebSocket application.

Send and receive messages to verify that the persistent WebSocket connection is operating correctly.

PHP WebSocket chat application
The example PHP chat exchanges messages instantly through WebSockets.

What’s next?