How to expose your local server to the Internet

Sometimes I need to temporarily expose my local development server to the Internet. For example, I might want to show some work in progress to a client.

Note: If you don’t care about your data going through third-party servers, see Alternatives on the bottom.

Your local development server could be anything but let’s pretend that we are developing a Ruby on Rails application and we want to demo the application to someone who is out of the office. Assuming that you are behind a firewall your best option is to route the traffic through a remote server that has a public ip address.

Below are the steps that you need to take in order to expose your rails application to the Internet. I will assume that the remote server has a public dns entry “remote.company.com”.

  • Setup your remote server to allow tcp forwarding
  • Create an ssh tunnel between you and the remote server

It really is simple as that. Let’s get to work.

Setup your remote server to allow tcp forwarding

Connect to your server and execute the following commands:

sudo echo "AllowTcpForwarding yes" >> /etc/ssh/sshd_config
sudo echo "GatewayPorts yes" >> /etc/ssh/sshd_config
sudo service ssh restart

Please note that you should check if these options are already set in the sshd configuration to avoid duplicate entries.

You are all done and you can move to the next step. For the curious ones, here’s what you did. AllowTcpForwarding enables tcp forwarding. Without this we wouldn’t be able to forward any ports. GatewayPorts enables remote hosts to connect to the forwarded ports. If we don’t specify this no one would be able to connect to the forwarded port on our remote server.

Create an ssh tunnel between you and the remote server

Just run the following command:

ssh -R0.0.0.0:7000:localhost:3000 [email protected]

The above command will tunnel all traffic between remote.company.com:7000 and localhost:3000. Now everyone on Internet can access your local development server at remote.company.com:7000.

Alternatives

These alternatives can be used if you don’t have your own server to route traffic through, or if you need something simpler and don’t care about your data going through a third party server.

ngrok

Ngrok seems to be more stable than localtunnel, you can use custom subdomains if you register for a free account, and it comes with a web interface for inspecting and replaying requests.

I recommend using ngrok if you can.

localtunnel

Note: No longer maintained.

I’ve briefly used localtunnel which generates a unique url you can use to route traffic through. I had some stability issues with the service when I used it.