Alabhya Jindal

How to deploy a Sinatra application using nginx

This guide will help you deploy a Sinatra web application using nginx. You will use OpenBSD as the operating system and tmux to keep your server process running in the background.

You will have deployed a Sinatra web application at the end of this guide, which you can add on to or replace.

Prerequisites

Create an instance

Deploy a new Vultr server:

  1. Choose the server, CPU, location and other options based on your requirements.
  2. Choose the latest version of OpenBSD as the server image.
  3. Generate and add your ssh keys.
  4. Enter your domain name in both hostname and label.
  5. Click the ‘Deploy Now’ button.

Your server is being created and will be ready in 1-2 minutes.

Update DNS records

Modify your DNS records so your domain name points to your server.

Login to your server using ssh as root

Get your server IP address and root password from your server dashboard.

Press the Enter key after every command you type.

  1. Login to your server as root, type ssh root@<YOUR_SERVER_IP>.
  2. You should see the following prompt:
The authenticity of host '65.20.83.180 (65.20.83.180)' can't be established.
ED25519 key fingerprint is SHA256:fjgos99tg5rutnggo3w9585slskgs.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])?
  1. Type yes.
  2. You are now logged in to your server.

Create a new user

  1. Type adduser.
  2. Follow the instructions to create a new user. Accept defaults for everything except username, Fullname and password.
  3. Once done, you will be prompted to create another user.
Add another user? (y/n):
  1. Type n.
  2. Type user mod -G wheel <YOUR_USERNAME>. This gives you the ability to run commands as the root user.
  3. Type exit to close the ssh session.

Login using ssh with your username

  1. Login back using your new username, type ssh <YOUR_USERNAME>@<YOUR_SERVER_IP>.
  2. Enter the password you created earlier when prompted.
  3. Type su. This will allow you run commands as root user.
  4. You will be prompted for a password. Type the root password here.

Your terminal prompt should have a # character at the end, indicating your root privileges.

Install nginx

Install nginx, type pkg_add nginx.

Add the following to your /etc/rc.conf.local, creating the file if it does not exist:

nginx_flags=""

Visit your domain in your browser. If you see “403 Forbidden nginx” then nginx is installed correctly.

Stop nginx, type rcctl stop nginx.

Install Ruby

Install Ruby by running: pkg_add ruby. You will be prompted to select a version. Choose the latest stable version.

Once installation completes, you will see an output prompting you to create symbolic links:

If you want to use this package as your default system ruby, as root
create symbolic links like so (overwriting any previous default):

 ln -sf /usr/local/bin/ruby32 /usr/local/bin/ruby
 ln -sf /usr/local/bin/bundle32 /usr/local/bin/bundle
 ln -sf /usr/local/bin/bundler32 /usr/local/bin/bundler
 ln -sf /usr/local/bin/erb32 /usr/local/bin/erb
 ln -sf /usr/local/bin/gem32 /usr/local/bin/gem
 ln -sf /usr/local/bin/irb32 /usr/local/bin/irb
 ln -sf /usr/local/bin/racc32 /usr/local/bin/racc
 ln -sf /usr/local/bin/rake32 /usr/local/bin/rake
 ln -sf /usr/local/bin/rbs32 /usr/local/bin/rbs
 ln -sf /usr/local/bin/rdbg32 /usr/local/bin/rdbg
 ln -sf /usr/local/bin/rdoc32 /usr/local/bin/rdoc
 ln -sf /usr/local/bin/ri32 /usr/local/bin/ri
 ln -sf /usr/local/bin/typeprof32 /usr/local/bin/typeprof

Type pkg_info ruby if you didn’t see this output post installation.

  1. Copy all the lines starting with ln.
  2. Paste them in your terminal. The terminal should run each line automatically.
  3. Verify installation, type ruby --version. If you see an output with the version of Ruby then the installation is successful.

Install Ruby Gems

Install Sinatra, type gem install sinatra.
Install Puma, type gem install puma.

Create your web application file

Create the following file in /var/www/htdocs/main.rb:

require 'sinatra'

get '/' do
  '<h1>Hello, World!</h1>'
end

Modify nginx configuration

Locate the following line in your nginx configuration file at /etc/nginx/nginx.conf. It should be nested within configurations for http and server:

root         /var/www/htdocs;

Insert the following code below it:

location / {
  proxy_pass http://127.0.0.1:4567;
}

Run nginx -t. You should see the following output, indicating correct nginx configuration syntax.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If you see an error, check the syntax again. There could be a typo or a missing semicolon in the code added to configuration file.

Start your web application

  1. Start the nginx server, type rcctl start nginx.
  2. Start the Sinatra web application, type ruby /var/www/htdocs/main.rb.

Go to your domain name in your web browser. Refresh the page and you should see the output of your Sinatra web application.

Use a detached terminal

If you log out of your ssh session then your website will stop working. This can be solved by using a detached terminal to run your web application using tmux.

  1. Type tmux.
  2. Start the Sinatra server, type ruby /var/www/htdocs/main.rb.
  3. Press Ctrl + b then type d.

Your server is now running in a detached tmux session. Exit your ssh session:

  1. Type exit to exit the root session.
  2. Type exit again to exit the user session.

Go to your domain name in your web browser. Refresh the page and you should still see your web application’s output, even when you log out of your ssh session.

View your server logs

You can see the logs of your running application by attaching to your tmux session.

  1. Login using ssh with your username.
  2. Type su, then type the root password.
  3. Type tmux attach.

You will be attached your previous tmux session and can see all the logs of your web application. Detach by typing Ctrl + b, followed by d.