WebSockets Support for PHP
WebSockets Support for Apache and NGINX PHP
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.
httpd.conf.
nginx.conf.
Create the Environment
1Open the environment wizard
Sign in to the platform dashboard and click New environment.
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.
Wait until the environment is created and appears in the dashboard.
Deploy the Application
Upload and deploy the WebSocket application through Deployment Manager or from a remote Git or SVN repository.
Open Server Configuration
1Open Configuration Manager
Click Config beside the PHP application-server node.
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.
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.
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.
wss:// instead of ws://.
Restart and Test
1Restart the application server
Save all changes and restart the Apache or NGINX application-server node.
2Open the application
Click Open in Browser after the application server returns to the running state.
Send and receive messages to verify that the persistent WebSocket connection is operating correctly.
