SSH Tunneling for Ruby on Rails app

It is quite annoying to develop and test an app which is likely to receive data/notification from a third party site if your development computer isn’t globally reachable. You have to deploy to a server and test it and do this each time until it completely works. Examples of such cases include:

  • Facebook app development
  • PayPal or any other payment gateway integration

While there are probably several options, SSH Tunneling is the simplest, at least to me. It needs less than 2 minutes to configure. Let’s do it.

I’m assuming:

  • You’ve a dedicated server/VPS with root access or somehow access to sshd_config file (we’re going to call this remote server)
  • Your domain is dev.awesomeapp.com and which points to the above server with IP 198.198.115.47
  • You wanted the remote server to listen to 80 port
  • You’re running your rails server at port 3000 in your development computer (we gonna call this local server)

(You may have to change the values marked in italic font as your own settings)

Now, do the following in your remote server.

  • Edit sshd_config (possibly  /etc/ssh/sshd_config) and add the following lines (if they already exists just make sure values are same).
    GatewayPorts true
    PermitRootLogin true
    
  • Restart the ssh by issuing:
    /etc/init.d/ssh restart

In your local server, activate the tunneling by executing the following commands:

  • Start your rails server  (rails s from the rails app directory)
  • ssh user@198.198.115.47 -NR *:80/0.0.0.0/3000

(You should also replace user with the correct username. It may ask you for password)

That’s it. Now any requests coming to dev.awesomeapp.com will be responded from your local computer. Easy and sweet!

Credit