Ruby App setup guide

  1. From the cPanel SOFTWARE section, click the Setup Ruby App icon
  2. Click the CREATE APPLICATION button. For this tutorial, we are going to create a simple application using the SINATRA framework.
    Complete the fields as shown below.
  3. In our example, we will install the application in /home/krystald/myrubyapp, and serve it from http://krystaldemo.co.uk/myrubyapp/, using Ruby version 2.4.
    Click the Setup button to continue.
  4. Once the application has been created, you will be shown the following information, which includes how to switch to the virtual environment for your python application from the command line.
  5. Log into your cPanel account via SSH (we have separate guides on SSH setup for Windows - link opens in a new window and SSH setup for Mac - link opens in a new window). Enter the virtual environment command line above and then change to your application directory (App Directory, above).
    [[email protected] ~]$ source /home/krystald/rubyvenv/myrubyapp/2.4/bin/activate
    (myrubyapp:2.4)[[email protected] ~]$ cd myrubyapp
    (myrubyapp:2.4)[[email protected] myrubyapp]$
    You will notice that the command prompt changes, and is now prefixed with (myrubyapp:2.4). This tells us that our shell environment is now setup for our application's own Ruby installation. List the files in the current directory, which should look like this.
    $ ls -la
    total 28
    drwxrwxr-x 5 krystald krystald 4096 Oct 23 08:21 .
    drwx--x--x 25 krystald krystald 4096 Oct 23 08:07 ..
    -rw-rw-r-- 1 krystald krystald 53 Oct 23 08:19 config.ru
    drwxr-xr-x 2 krystald krystald 4096 Oct 23 08:07 public
    drwxr-xr-x 2 krystald krystald 4096 Oct 23 08:07 tmp
    Currently the config.ru file contains a basic Ruby "Hello World" script, which can be tested by browsing to, in this case, http://krystaldemo.co.uk/myrubyapp - we will see a simple message giving the Ruby version.
    It works!
    Ruby v2.4
  6. Now we need to install the Sinatra framework and create a basic application. Install the Sinatra Gem with the following command.
    $ gem install sinatra
    Fetching: rack-2.0.7.gem (100%)
    Successfully installed rack-2.0.7
    Fetching: tilt-2.0.10.gem (100%)
    Successfully installed tilt-2.0.10
    Fetching: rack-protection-2.0.7.gem (100%)
    Successfully installed rack-protection-2.0.7
    Fetching: mustermann-1.0.3.gem (100%)
    Successfully installed mustermann-1.0.3
    Fetching: sinatra-2.0.7.gem (100%)
    Successfully installed sinatra-2.0.7
    Parsing documentation for rack-2.0.7
    Installing ri documentation for rack-2.0.7
    Parsing documentation for tilt-2.0.10
    Installing ri documentation for tilt-2.0.10
    Parsing documentation for rack-protection-2.0.7
    Installing ri documentation for rack-protection-2.0.7
    Parsing documentation for mustermann-1.0.3
    Installing ri documentation for mustermann-1.0.3
    Parsing documentation for sinatra-2.0.7
    Installing ri documentation for sinatra-2.0.7
    Done installing documentation for rack, tilt, rack-protection, mustermann, sinatra after 87 seconds
    5 gems installed
  7. Once complete, we can now create our basic Sinatra application. We will need to edit/create a few files. In the examples below we will be using the nano text editor.
    First, we will edit the config.ru file
    $ nano config.ru
    ... and edit the contents so they look like this:
    require File.absolute_path("app.rb")
    run HelloApp
    To save the file in Nano, Ctrl-X and then press Y to confirm the changes.
  8. Then create the application file itself:
    $ nano app.rb
    ... and edit the contents so they look like this:
    require 'sinatra/base'

    class HelloApp < Sinatra::Base
    get '/myrubyapp/' do
    erb :index
    end
    end
    use, Ctrl-X and then press Y to confirm the changes.
  9. Now, create a directory called 'views':
    $ mkdir views
    and now create a file called index.erb:
    $ nano views/app.rb
    ... and edit the contents so they look like this:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Hello World</title>
    <style>
    html, body {
    font-family: sans-serif;
    background: #eeeeee;
    margin: 4em;
    }

    .main {
    background: white;
    border: solid 1px #aaaaaa;
    border-radius: 8px;
    padding: 2em;
    }
    </style>
    </head>
    <body>

    <section class="main">
    <h1>Hello world!</h1>
    <p>Congratulations, your app is running!</p>
    </section>
    </body>
    </html>
    use, Ctrl-X and then press Y to confirm the changes.
  10. You should now have a directory structure in /home/krystald/myrubyapp that looks like this:
    myrubyapp
    |-- app.rb
    |-- config.ru
    |-- public
    |-- tmp
    | `-- restart.txt < - this file is generated by the system, ignore
    `-- views
    `-- index.erb
  11. Now, go back to cPanel, and click the Restart button for your application.
  12. Browse to your application URL, in this case http://krystaldemo.co.uk/myrubyapp/, and now you should see the message.

Welcome to your Ruby Sinatra application!


How did we do?


Powered by HelpDocs
© Krystal Hosting Ltd 2003–2019