Ruby App setup guide
- From the cPanel SOFTWARE section, click the Setup Ruby App icon
- 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. - In our example, we will install the application in
/home/krystald/myrubyapp
, and serve it fromhttp://krystaldemo.co.uk/myrubyapp/
, using Ruby version 2.4.
Click the Setup button to continue. - 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.
- 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).
[krystald@server ~]$ source /home/krystald/rubyvenv/myrubyapp/2.4/bin/activate
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.
(myrubyapp:2.4)[krystald@server ~]$ cd myrubyapp
(myrubyapp:2.4)[krystald@server myrubyapp]$$ ls -la
Currently the
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 tmpconfig.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 - 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 - 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 theconfig.ru
file$ nano config.ru
... and edit the contents so they look like this:require File.absolute_path("app.rb")
To save the file in Nano, Ctrl-X and then press Y to confirm the changes.
run HelloApp - Then create the application file itself:
$ nano app.rb
... and edit the contents so they look like this:require 'sinatra/base'
use, Ctrl-X and then press Y to confirm the changes.
class HelloApp < Sinatra::Base
get '/myrubyapp/' do
erb :index
end
end - Now, create a directory called 'views':
$ mkdir views
and now create a file called index.erb$ nano views/index.erb
... and edit the contents so they look like this:<!DOCTYPE html>
use, Ctrl-X and then press Y to confirm the changes.
<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> - 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 - Now, go back to cPanel, and click the Restart button for your application.
- 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!