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.
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 (how).
Prerequisite
Go to https://mg.n0c.com/en/.
Access to Ruby in N0C
- In the Sidebar menu, select Languages > Ruby.
- 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:
- 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!”.
- You then need to program it (in our example, to display “Hello World!”).
- Then, call the application environment.
- Finally, it must be activated in the Ruby environment.
Step #1 – Create the Ruby Application
Make sure the coderuby directory is created (how).
- Click on the Create button :
- Choose the desired Ruby VERSION:
- Indicate in which APP DIRECTORY you want the application to be created (in our example, we would write coderuby).
- Indicate from which APPLICATION DOMAIN / URL it should be accessible (in our example, we would write coderuby)1.
- Click on the CREATE button.
- Confirm the creation of config.ru by opening the coderuby directory in the File Manager (how).
- Test the Ruby application by typing https://DOMAIN/coderuby in your browser, where DOMAIN should be replaced by your domain name.
- 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 you wish. The application must be started again to see the modifications in the browser when it is exectued.
Here are the steps to program:
- In the File Manager, open the directory (here, coderuby) and edit the code config.ru (how).
- Program config.ru. In our example, copy the following code in the programming window of the File Manager (how), 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
- After having saved the code, close and open N0C again to apply the changes.
- Test the Ruby application by typing https://DOMAIN/coderuby in your browser, where DOMAIN should be replaced by your domain name.
- 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.
To see the command, you must edit the application (please refer to the paragraph Ruby Application Settings Modification).
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.
- Click on the Restart icon next to the application to activate (here, coderuby):
- The message Successfully executed command must be displayed to confirm the activation 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
Restart a Ruby Application
Sometimes it is necessary to restart an application as, for example, when you modify the code.
- Click on the Restart icon next to the application:
- A message shall be displayed to confirm the successful execution of the command.
Ruby Application Version Modification
- Click on the Change version icon next to the application to modify:
- Select the desired VERSION RUBY:
- Click on the EDIT button.
Ruby Application Settings Modification
- Click on the Edit icon next to the application to modify:
- Change the desired settings:
- Click on the EDIT button to save your changes.
- Execute the command to save the application environment.
Delete a Ruby Application
- Identify the application and click on the Delete icon:
- 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:
- Connect in SSH.
- Find the availble Ruby versions.
- Create the functional base application at the CloudLinux level.
Step # 1 – Connect in SSH
Connect in SSH (how).
For example, if you are using the MacOSX environment of Apple Computers, you can enter:
ssh < USER >@ < DOMAIN > -p PORT
Where :
- USER: Your username.
- DOMAIN: The domain name (make sure it points to the hosting account).
- PORT: Port to be used for hosting account (unless otherwise specified, use 5022).
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.