How to Install mod_proxy_wstunnel in Apache


how to install mod_proxy_wstunnel in apache

mod_proxy_wstunnel supports tunneling web socket connections to backend web socket servers. It requires mod_proxy for its functioning. Here’s how to install mod_proxy_wstunnel in Apache.

 

How to Install mod_proxy_wstunnel in Apache

Before we proceed, please ensure you have installed Apache server.

Here are the steps to install mod_proxy_wstunnel in Apache.

 

1. Enable mod_proxy_wstunnel and other modules

mod_proxy_wstunnel is installed in Apache but disabled by default. So we enable it with the comands


$ a2enmod proxy
$ a2enmod proxy_http
$ a2enmod proxy_wstunnel

We also need to enable mod_proxy which is required for mod_proxy_wstunnel to work properly.

 

2. Restart Apache Server

Restart Apache web server to apply changes


$ sudo systemctl restart apache2

 

3. (Optional) Configure mod_proxy_wstunnel

For example, if you have Apache running on port 80 of your server (www.example.com) and a websocket server (e.g node.js+socket.io) on port 3000 of same server, you can update <VirtualHost> block of your Apache’s config file like shown below.

Open Apache config file in a text editor


$ sudo vim /etc/apache2/httpd.conf

In some cases, the conf file is also called apache2.conf

 

Look for the <VirtualHost> block and replace the code in it with the following


<VirtualHost *:80>
 ServerName www.example.com

 RewriteEngine On
 RewriteCond %{REQUEST_URI} ^/socket.io [NC]
 RewriteCond %{QUERY_STRING} transport=websocket [NC]
 RewriteRule /(.*) ws://localhost:3000/$1 [P,L]

 ProxyPass / http://localhost:3000/
 ProxyPassReverse / http://localhost:3000/
</VirtualHost>

 

Restart Apache web server to apply changes


$ sudo systemctl restart apache2

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!