Share localhost with SSH & Apache
Oct 23, 2012
3 minute read

There’s been a lot of services released lately that aim to assist developers share their local development environments over the internet. localtunnel, showoff.io and pagekite come to mind, just to name a few. However, you can roll your own forwarding service if you have SSH access to a server running Apache fairly easily.

The Big Picture

First, setup a webserver to listen for connections to demo.example.com. When a user connects to the webserver, it will attempt to proxy their request to port 1337 (demo.example.com port 1337).

Second, a reverse proxy initiated with SSH on your localhost creates a connection to demo.example.com, and then has all traffic on demo.example.com port 1337 forwarded to localhost port 80.

User --> demo.example.com:80 --> demo.example.com:1337 --> localhost:80

Requirements

Server

  • Apache Web Server
  • Apache mod_proxy module
  • OpenSSH

Client

  • Local web application
  • SSH client
  • SSH public/private keys to login to the remote server

Server Setup (Ubuntu)

You should be able to login to your server over SSH using public/private key authentication. How to do that is out of scope for this post, however there are plenty of guides available online that explain in detail how to do this.

Apache: Enable mod_proxy

$ sudo a2enmod proxy_http

Apache: Configure VirtualHost

Setup Apache to use a VirtualHost for URL you want to use for sharing your work:

<VirtualHost *:80>
  ServerName &#8220;demo.example.com&#8221;
  ProxyPass / http://127.0.0.1:1337/
  ProxyPassReverse / http://127.0.0.1:1337/
</VirtualHost>

The ServerName Directive tells Apache to listen for client requests for demo.example.com. ProxyPass and ProxyPassReverse configure Apache to forward all requests to the local port 1337.

Apache: Restart to load VHost

$ sudo apachectl restart

Localhost Setup

Make sure you’re running a local web application

This should be pretty obvious :) It doesn’t matter what port it’s running, you can configure that when you start the SSH reverse proxy.

Create an SSH Reverse Proxy

To actually enable the connection form your public web server to your localhost, you have to create a reverse proxy using SSH. The reverse proxy will create a connection from your localhost to the remote server (demo.example.com), then have all traffic from a designated remote port forwarded to a local port.

$ ssh -NR 1337:localhost:80 demo.example.com
  • -N disables all output keeping things clean. If you omit this, you’ll see SSH connect to your remote server.
  • -R 1337:localhost:80 Tells SSH to create a reverse proxy and that all traffic on the remote port 1337 should be forwarded to localhost’s port 80.
  • demo.example.com is the remote server you want SSH to connect to.
  • -f puts ssh into the background. If you want to be able to easily kill the Proxy later, omit the -f and use ctrl + c.

You may need to change your localhost port depending on what port your local environment is running on. For example: if you’re developing a rails application using the builtin webserver, you might need to use port 3000 instead of port 80.

You should now be able to hit demo.example.com in your browser, and see your application load.