6

Raspberry Pi is still on Apache 2.2 (2.2.22-13+deb7u4 right now). To use Apache as Proxy for Websockets ("ProxyPass") Apache Module mod_proxy_wstunnel is required.

Apache Module mod_proxy_wstunnel is available in httpd 2.4.5 and later.

How can I add mod_proxy_wstunnel to Apache2 2.2 on Raspberry Pi (Backport mod_proxy_wstunnel)?

Schelldorfer
  • 301
  • 1
  • 4
  • 7

3 Answers3

8

Download Apache Source, add patch from Vitkin, compile Apache and add module mod_proxy_wstunnel.so to Apache Modules

Details about the patch: https://gist.github.com/vitkin/6661683

Detailed steps:

# Check apache version (should be 2.2.22 as of writing, if not adjust the next step)
dpkg -s apache2

# Checkout apache source
svn checkout http://svn.apache.org/repos/asf/httpd/httpd/tags/2.2.22/ httpd-2.2.22

# Get patch and apply it
wget https://gist.github.com/vitkin/6661683/raw/873dd8b4de4ad1ff69757ffe48fc574374aedc57/apache-2.2-wstunnel.patch
cd httpd-2.2.22
patch -p1 -i ../apache-2.2-wstunnel.patch

# Build Apache 
svn co http://svn.apache.org/repos/asf/apr/apr/branches/1.4.x srclib/apr
svn co http://svn.apache.org/repos/asf/apr/apr-util/branches/1.3.x srclib/apr-util
./buildconf # EDIT: Some commenters noted that buildconf should be run before the configure
./configure --enable-proxy=shared --enable-proxy_wstunnel=shared
make

# Copy the module to apache installation
sudo cp modules/proxy/.libs/mod_proxy_wstunnel.so /usr/lib/apache2/modules

# Create module load file
cd /etc/apache2/mods-available
sudo echo "LoadModule proxy_wstunnel_module /usr/lib/apache2/modules/mod_proxy_wstunnel.so" > proxy_wstunnel.load

# Create symbolic link to load the module
cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/proxy_wstunnel.load proxy_wstunnel.load

# Add ProxyPass to Site config
cd /etc/apache2/sites-available

# e.g. modify default site config with "sudo nano default"
# and add the following line inside the VirtualHost element:
# "ProxyPass /websockets/mywebsocket ws://mywebsocketserver.com/websockets/mywebsocket"

# Restart Apache
sudo /etc/init.d/apache2 restart
Hernán Eche
  • 6,529
  • 12
  • 51
  • 76
Schelldorfer
  • 301
  • 1
  • 4
  • 7
  • When I tried this, I got the following after "sudo service apache2 restart": apache2: Syntax error on line 253 of /etc/apache2/apache2.conf: Syntax error on line 1 of /etc/apache2/mods-enabled/mod_proxy_wstunnel.load: Cannot load /usr/lib/apache2/modules/mod_proxy_wstunnel.so into server: /usr/lib/apache2/modules/mod_proxy_wstunnel.so: undefined symbol: ap_proxy_canonenc – and so action 'configtest' failed. Any ideas? – Alex Schröder Mar 07 '16 at 08:04
  • 1
    The solution was that you have to copy not just mod_proxy_wstunnel but mod_proxy as well, as seen here: https://www.amoss.me.uk/2013/06/apache-2-2-websocket-proxying-ubuntu-mod_proxy_wstunnel/ – Alex Schröder Mar 08 '16 at 07:45
  • In my raspbian apache is version 2.4.10, but the mod_proxy_wstunnel is still not available. according to the apache documentation it should be there. Any idea why? – alexmogavero Dec 25 '16 at 23:54
  • 1
    Typo in echo command: output redirect should be to filename 'proxy_wstunnel.load' not 'mod_proxy_wstunnel.load' – Rondo May 22 '17 at 00:50
  • 1
    @alexmogavero have you tried `a2enmod proxy_wstunnel.load`? – haui Jun 20 '21 at 16:55
4

I followed these steps for CentOS 2.2, assuming for Raspberry Pi it should be on similiar lines. I have invested lot of time figuring this out and there is very little documentation available for this. Let me know if this helps, else I can help you troubleshooting the problem. Also hope this helps to future readers.

To compile mod_proxy_tunnel.so,

  1. yum install httpd-devel

  2. Download the mod_proxy_tunnel.c and compile it using apxs -i -a -c mod_proxy_tunnel.c

Then load the above compiled module in /etc/httpd/modules:

  1. Copy the mod_proxy_wstunnel.so in /etc/httpd/modules (Compiled from above)

  2. In order to load the module while the server starts, use LoadModule directive in the httpd conf file /etc/httpd/conf/httpd.conf

    Add the following line with all other LoadModule line

    LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
    
  3. To restart apache use service httpd restart

  4. To check the loaded modules in apache after restarting use httpd -M

  5. After the module is installed, add these two lines in /etc/httpd/conf/httpd.conf:

    ProxyPass /websockets/mywebsocket ws://mywebsocketserver.com//websockets/mywebsocket retry=4
    ProxyPassReverse /websockets/mywebsocket ws://mywebsocketserver.com//websockets/mywebsocket retry=4
    

Note : Make sure the above lines are added before the default case of /. Also restart apache just to be safe.

Wtower
  • 18,848
  • 11
  • 103
  • 80
3

I needed this for my installation of the Traccar site. I did an apt-update /apt-upgrade. I executed dpkg -s apache2 which showed I was running Version: 2.2.22-13+deb7u7. I started following the instructions in LearningAboutTech's answer above. In the time passed, some of the process changed:

  1. I started with apt-get install apache2-threaded-dev
  2. I then sought out mod_proxy_wstunnel.c, I used the version here, and fetched it with wget
  3. I then used the command apxs2 -i -a -c mod_proxy_wstunnel.c
  4. After checking the configuration files, I saw that the module was already loaded in the mods-enabled folder.
  5. I had already added the ProxyPass and ProxyReverse in the site configuration files. So, the next thing was to service apache2 restart and test.

Testing the site, it performed as expected, I did see warnings in the error file:

[warn] proxy: No protocol handler was valid for the URL /api/socket. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule.

At some point, I will look into this further - it may be related to what I have just done or some other configuration in my setup - but I'm happy my site is working as expected!

Community
  • 1
  • 1
Bryan
  • 295
  • 1
  • 2
  • 9