1. Home
  2. Languages
  3. Ruby Applications Management

Ruby Applications Management

Introduction

Ruby is essentially code executed on our servers to host, create and control web applications using this type of language.

In this article, we will focus on information for beginners. We will start by explaining how to access Ruby in N0C and provide a detailed example of how to launch and modify an application using this interface.

Then, for more advanced users, we will explain how to do the same operations in command line.

Note : The Glossary of N0C contains explanations on multiple topics and can be consulted to clarify certain terms.

Documentation

Documentation on Ruby can be found on the Web, in particular at: https://www.ruby-lang.org/en/documentation/.

You can also refer to this site to get more information about the commands of the Ruby Selector: https://docs.cloudlinux.com/command-line_tools/#ruby-selector.

Debugging an Application

When an error occurs, a page like the one below is displayed:

This page is not very explanatory. Activating the development mode allows you to investigate. For more details, see the article Debugging a Node.js, Ruby or Python application with N0C (only available in French for the present moment).

Prerequisite

Go to https://mg.n0c.com/en/.

Access to Ruby in N0C

  1. Go to Languages > Ruby:
  1. Applications already set-up are displayed in the interface.

Implementation of a New Ruby Application with N0C

To explain the approach more easily, we will proceed with an example. 

Let’s say you want to create an application called coderuby that displays the words “Hello World!“. The following steps are necessary:

  1. Beforehand, you need to create an application in the directory intended to contain it (we will call this directory “coderuby” as an example, but the user can give it the name they desire). When it is executed, the application displays “It works!”.
  2. You then need to program it (in our example, to display “Hello World!”).
  3. Then, call the application environment.
  4. Finally, it must be activated in the Ruby environment.

Step #1 – Create the Ruby Application

Make sure the coderuby directory is created (see the article N0C FileManager if you need to know the procedure).

  1. Click on the Create button :
  1. Choose the desired Ruby VERSION:
  1. Indicate in which APP DIRECTORY you want the application to be created (in our example, we would write coderuby).
  2. Indicate from which APPLICATION DOMAIN/URL it should be accessible (in our example, we would write coderuby)1.
  3. Click on the SAVE button.
  4. Confirm the creation of config.ru by opening the coderuby directory in the File Manager:
  1. Test the Ruby application by typing https://DOMAIN/coderuby in your browser, where DOMAIN should be replaced by your domain name
  2. As expected, the result shall be “It works!”.

Step #2 – Program the Application

Now that the Ruby application is installed and functional in the directory to be used, you can program it as we wish. The application must be started again to see the modifications in the browser when it is exectued.

Here are the steps to program:

  1. Go to Files > File Manager.
  1. Open the directory (here, coderuby):

3. To edit the code, right-click on config.ru and click on Edit:

  1. Program config.ru. In our example, copy the following code, so that the words “Hello World!” are displayed when you run coderuby on your domain:
app = proc do |env|
    message = "Hello world!\n"
    version = "Ruby %s\n" % RUBY_VERSION
    response = [message, version].join("\n")
    [200, {"Content-Type" => "text/plain"}, [response]]
end


run app
  1. Click on the Save button to save the code:
  1. Click on the Close button to exit.
  2. Close and open N0C again to apply the changes.
  3. Test the Ruby application by typing https://DOMAIN/coderuby in your browser, where DOMAIN should be replaced by your domain name. 
  4. The result shall now be “Hello World!”.

Step #3 – Call the Environment of the Application

Before performing any other command (e.g., gem install or rake), you must call the application environment by running the following command at the root of your application, which will set the environment variables and allow you to configure your application:

The command must be copied into the shell of the account in question. It is possible to connect via SSH.

In our example, the command is:

source /home/fbkfdayw/rubyvenv/coderuby/2.6/bin/activate

Where:

  • « fbkfdayw » must be replaced by your Username;
  • « coderuby » must be replaced by the name of your application; and
  • « 2.6 » must be replaced by the Ruby version number.

Step #4 – Activate the Application in the Ruby Environment

You have to make sure that your application has the correct plugins versions.

  1. Click on the Restart icon next to the application to activate (here, coderuby):
  1. A message is displayed when the command is successful.

You can consult the documentation available at https://www.ruby-lang.org/en/documentation/ to help you debug your code.

Actions on Application with N0C

Ruby Application Settings Modification

  1. Click on the Modify icon next to the application to modify:
  1. Change the desired settings:
  1. Click on the Save button to save your changes.
  2. Execute the command to save the application environment.

Restart a Ruby Application

Sometimes it is necessary to restart an application as, for example, when you modify the code.

  1. Click on the Restart icon next to the application:
  1. A message shall be displayed to confirm the successful execution of the command.

Delete a Ruby Application

  1. Identify the application and click on the Delete icon:
  1. At the command prompt, confirm the deletion.

Implementation of a New Ruby Application in Command Line

For more advanced users, the command line approach can be used.

The following steps have to be followed in order:

  1. Connect in SSH.
  2. Find the availble Ruby versions.
  3. Create the functional base application at the CloudLinux level.

Step # 1 – Connect in SSH

If required, see the article  How to Create an SSH Key and Connect to an Account.

For example, if you are using the MacOSX environment of Apple Computers, you can enter:

ssh < USER >@ < DOMAIN > -p PORT

Where :

  • USER: Your World username.
  • DOMAIN: The domain name (make sure it points to the hosting account).
  • PORT: Port to be used for World hosting account (use 5022; use 2908 for VPS/HybridCloud hosting).

At the command prompt, enter your password.

Step # 2 – Find the Available Ruby Versions

This command will return the Ruby versions available on the server:

$~ /usr/bin/selectorctl --json --interpreter=ruby --list | python -m json.tool

Step # 3 – Creation of the Basic Functional Ruby Application at CloudLinux Level

The following command creates a functional database:

/usr/bin/selectorctl --interpreter=ruby --version=VERSION --domain DOMAIN —user=USAGER --print-summary --json --create-webapp DIRECTORY ‘/’ APP_URI

Where:

  • VERSION: An available version (according to the return of the command we saw before). Example: 2.6.
  • DOMAIN: The domain on which you want to run your application. Example: domain.com
  • USER: Your World username. Example: user11.
  • DIRECTORY: The directory in which you want to install your application. Example: directoryruby2.
  • APP_URI : The path of your application after your domain. For example, coderuby2.

Thus, in our example, you would program: 

/usr/bin/selectorctl --interpreter=ruby --version=2.6 --domain domain.com --user=user11 --print-summary --json --create-webapp directoryruby2 ‘/’coderuby2

You now have a working basic Ruby application if “status=Ok” is displayed.

Otherwise, an error message would be displayed and you would have to correct the command accordingly.

Actions on Application in Command Line

It is possible to stop or restart an application, to name just a few possible actions. Simply enter the appropriate command. 

For example, you can enter this command :

selectorctl --interpreter ruby --user USER --domain DOMAIN --COMMAND-webapp APP_URI

Where COMMAND must be replaced by:

  • stop to stop the application.
  • restart to restart the application.
  • destroy to delete the application.

Thus, to restart, you would write:

selectorctl --interpreter ruby --user USER --domain DOMAIN --restart-webapp APP_URI

Command Line Logout

To log out, enter the following command:

exit

1The URL can remain empty. In this case, the application will be accessible only via the domain.

Updated on April 6, 2022

Related Articles