How to Develop a Facebook App on Your Local Server using SSH

When you develop for facebook you usually run you app on your developer machine and connect a test app in Facebook with the local installation. Once finished, you put the final app onto some dedicated server and connect the real Facebook app to that. So setting up the local development environment usually includes some network setup, for instance as described here: http://www.insidefacebook.com/2007/07/16/how-to-local-facebook-app-development/ However, I find these router and network setup design troublesome and I also want something that is flexible, especially when I switch from desktop to mobile development.

If you have access to a dedicated, root or v-server on the internet, you can achieve a very easy network setup using ssh port forwarding. You basically use the internet box as the target url for your Facebook app and forward some port from that internet port to your local test web server over the ssh tunnel. By doing so, you are totally mobile, you can open the port on the internet box from anywhere you want.

So, let’s say you configured your facebook app “myapp” like this:

You see in this example that facebook will tell your browser to load the iframe content from the URL http://my-domain.com:9090/ that is on port 9090. So you need to forward this port 9090 to your developer machine.

In order to forward ports on public network interfaces you will mostly need to enable the “GatewayPorts” option in the ssh server of you internet box. This is a setting in the /etc/ssh/sshd_config (Debian):

just add the following line to /etc/ssh/sshd_config:

GatewayPorts yes

and

/etc/init.d/ssh restart

Then you can start forwarding the port to your local machine. Just issue an

ssh -R '*:9090:localhost:9000' me@my-domain.com

It will look like you just only logged into the remote machine but in addition to that, a tunnel has been opened from port 9090 remote to port 9000 local. So if you have something listening on your local machine on port 9000, i.e., your development web server, than you can point the browser to your facebook app:

http://apps.facebook.com/myapp

and it will display the content as retrieved via the tunnel (9090) from your local machine (9000). As soon as you close the ssh connection, the tunnel will also be gone and you can then initiate the same ssh connection from you laptop if you are going outside.

The biggest benefit of this solution is that you will never have to change IP addresses anywhere, not in the facebook app setting, not at dyndns etc. This is especially useful if your IP address changes frequently like on mobile internet.

Pretty simple, and no need to configure multiple apps for multiple environments. Just move and reconnect via ssh. If you want, you can easily do the next step and put a web server like Apache or nginx on the internet box. From there you can reverse proxy from a certain virtual host (e.g., dev.my-domain.com) to your forwarded port on the same machine (9090). In a setup like this, you would not need to enable the “GatewayPorts” option in the ssh server, because then it would be sufficient to listen only on the local interface (127.0.0.1:9090). Such a setup would be a little closer to most production setups with load balancers and such.