1670879794aed7c23742e5b1f759be94?s=60
Socat and ssh Tips for Ruby-on-Rails
by tomgdow

Allow Public Access to a Remote Ruby-on-Rails Development App with Socat

A Ruby-on-Rails development app running under localhost on a remote server may be exposed to to public access as follows:
Start a Rails app on the remote server:
rails s
On the remote server execute the following  socat  command:
socat TCP-LISTEN:8090,fork TCP:localhost:3000
Now the Rails application will be available publicly at:
yourserver.com:8090
For example, if the server is at tomgdow.com, the app will be available at www.johndow.com:8090.
Notes
Netcat may be used to check that all is well
 netcat -z -v localhost 8090  
 » Connection to localhost 8090 port [tcp/*] succeeded!
Alternative method with ssh tunnel
After starting the Rails server, open an ssh tunnel on the remote server:
ssh -R 4000:localhost:3000  janedow@johndow.com -N
In a separate teminal window, use socat to open a port on the server to public access:
socat TCP-LISTEN:8090,fork,reuseaddr TCP-CONNECT:127.0.0.1:4000 &
As for the previous method, the Rails development app is available publicly at yourserver.com:8090.
Notes
janedow@johndow.com  are the user credentials on the remote machine

Securely Access a Remote Ruby-on-Rails Development App with ssh

A Ruby-on-Rails development app running under localhost on a remote server may be securely accessed from a local machine as follows:
Start a Rails app on the remote server (port 3000)
rails s
On the local machine execute:
 ssh -N -f -L localhost:4000:localhost:3000 janedow@johndow.com
The Rails application will now be accessible from a local machine at:
localhost:4000
Notes
A password will be required for ssh access to the remote machine
janedow@johndow.com  are the user credentials on the remote machine

Other ssh Tips

Find the hostname of the remote machine
ssh -t janedow@johndow.com -- hostname
List the home directory of the remote machine
ssh -t janedow@johndow.com -- ls

Other Useful Commands

lsof -i  # lsof: list open files 
lsof -wni tcp:3000
ps -aux | grep ruby # ps snapshot of current processes
netstat -antlp |grep "LISTEN" | grep 'ruby'

Log for the Creation of a Simple Rails App

This Rails app was created on a Digital Ocean Droplet with Ubuntu as OS
Some preliminaries. (remote server command line)
uname -ro; rails -v; ruby -v; nodejs -v; lynx -version
» 3.13.0-57-generic GNU/Linux
» Rails 4.2.5
» ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]
» v0.10.25
» Lynx Version 2.8.8pre.4 (04 Feb 2014)
The Rails project was created as follows
mkdir railsProjects && cd $_
rails new myrailsapp && cd $_
bundle install
rails s        
# Check that all is well:
curl localhost:3000 | grep -i 'Welcome'
» <h1>:Welcome aboard</h1>  # (abridged) 
# Generate a scaffold
rails generate scaffold Product name:string description:text price:float
rake db:migrate
# Set the routes
sed -i "s/'welcome#index'/'products#index'/" config/routes.rb
sed -i 's/#\{1,2\}[[:space:]]\{0,10\}root/\ root/' config/routes.rb
# Open a remote port to public access
socat TCP-LISTEN:8090,fork TCP:localhost:3000
# Check that all is well
netcat -z -v localhost 8090
» Connection to localhost 8090 port [tcp/*] succeeded!
# On browser:
johndow.com:8090 # (ie server-name.com:port-number)
# From the browser, added some products
# The final result was something like the following:
Railsproducts
Rails Development App on Remote Server
Notes
Lynx, a text web browser, may be used instead of curl to check that all is well
Sed Commands
  • The sed commands used to programatically change text in config/routes.rb  are specific to Ubuntu,and may not work as expected on the Mac OS, as the GNU and BSD versions of sed behave differently
  • The first command merely changes the default root 'welcome#index' to root 'products#index' in config/routes.rb
  • The second command uncomments the line (removes the '#') containing the word root  in config.rb.
  • These changes may be easily made with any text editor or IDE, instead of using sed
Local access over ssh
  • Instead of using socat to open a port to public access,  the Rails app was securely accessed over ssh as follows:
  • # On server:
    rails s 
    # On local machine:
    # (here a Windows 10 computer, using Powershell to execute ssh command.  Putty will work equally well)
    ssh -N -f -L localhost:4000:localhost:3000  janedow@johndow.com
    # enter password
    # On local browser (here Firefox running on Windows 10):
    localhost:3000
Generate a new Rails project with railspg