Den hälsosamme ekonomisten

Installing Ruby and Sinatra with Nginx as reverse proxy

My usual readers may skip this post. I'm writing this as a guide to solve a problem that I have found no complete comprehensive instruction to tackle on the net - partly for myself as a reference. But by combining a number of sources I have found a good way of installing Ruby, Sinatra and Nginx as a reverse proxy on Ubuntu (17.04). This is basically a long list of commands where the end result is a complete installation for using Sinatra in the wild.

The steps are in turn:

1) Installing Ruby

2) Installing Sinatra

3) Installing Nginx as a reverse proxy, using either Puma or Webbrick.

The first step: Installing Ruby

sudo apt-get update

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list sudo apt-get update sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev nodejs yarn

sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev nodejs
sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm rvm
install 2.4.2
rvm use 2.4.2 --default
ruby -v
gem install bundler

You may have to replace python-software-properties above with another package. There may also be problems with the gpg keyserver. There might be clues in the error messages.

Next step is to install Sinatra. 

sudo apt-get update
sudo apt-get install ruby-dev build-essential
sudo apt-get update
gem install sinatra

Then we begin installing Nginx

sudo apt-get install nginx

To configure Nginx to be used as a reverse proxy you have to change the file /etc/nginx/sites-enabled/default

The block which starts with "location / {" should be changed to

location / {
proxy_pass http://localhost:4567;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

Port 4567 is the default port of Sinatra servers, if this port is set to something else the setting must be changed accordingly. To start the app you just write:

sudo service nginx restart
ruby myapp.rb

This assumes you have created a Ruby app named myapp.rb with something like:

require 'sinatra'
get '/frank-says' do
'Put this in your pipe & smoke it!'
end

If you want to run the app on Google Compute Engine or any other virtual server in production you will need to keep the app running even when the terminal window is closed. To make this possible, install screen if its not already installed. And don't forget to open the relevant ports.

sudo apt-get install screen

You can then start a new screen-session with

screen -S "My App"

And when the new window opens you just write

ruby myapp.rb

to run the app. The window is then closed by pressing ctrl+a and then w.

If you want to stop the app you will have to use "ps aux", look up the process "ruby" and then kill it with "kill -15 <process number>".

You should now have access to the app on the port you have chosen, e g 80. Write http://xx.xx.xx.xx/frank-says to test the setup, where you replace the xx with the ip-adress of the virtual server. The answer should be "Put this in your pipe & smoke it!".

Kommentarer