NodeJS and Ruby on Rails

NodeJS and Ruby on Rails

Alpha Hosting is excited to announce that our environment now is NodeJS and Ruby on Rails capable! You can now install a Ruby on Rails or NodeJS passenger app on your shared hosting or cloud environment! You can also install regular NodeJS modules and Ruby gems into your hosting environment. To get started, simply add the following to your .bash_profile file:

NodeJS:

# EA NodeJS
PATH=$PATH:/opt/cpanel/ea-nodejs10/bin:$HOME/node_modules/.bin

Ruby and Rails:

# EA Ruby
. /opt/cpanel/ea-ruby24/enable

Once you have saved the .bash_profile file, you can then run source .bash_profile or log out and back into your SSH or cPanel terminal session to apply the changes made. From this point, you can use the ruby, rails, nodejs, and npm commands.

 

Now that you have the environment setup, you can setup your first app.

Creating a NodeJS App

This section will provide you with the basics to create a NodeJS application. These instructions are designed for use in SSH or under the cPanel terminal.

Make the director for your application and change directory to the directory created.

$ mkdir -p nodejs_apps/nodejs_test
$ cd nodejs_apps/nodejs_test/

Create a file named app.js with your favorite text editor and add the following app code.

$ nano app.js
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World! NodeJS\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

In cPanel, visit the Application Manager.

In the Application Manager, register the application as follows.

Contact us to have the deployed application made active.

Making changes to your application live

After you make changes to your application, you will have to restart the application. You can do this with the following command.

passenger-config restart-app

Creating a Ruby on Rails App

This section will provide you with the basics to create a Ruby on Rails application. These instructions are designed for use in SSH or under the cPanel terminal.

Create a ruby_apps directory to store all ruby applications that you will be working with.

$ mkdir ruby_apps

Change directory into the newly created directory to use rails to create the new application.

$ cd ruby_apps/

Use rails to create your application.

$ rails new ruby_test
      create
      create  README.md
      create  Rakefile
  ……………...
Using web-console 3.7.0
Bundle complete! 18 Gemfile dependencies, 78 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
         run  bundle exec spring binstub --all
* bin/rake: Spring inserted
* bin/rails: Spring inserted

Change directory into the new application, add the therubyracer gem to the application and install it with bundle. This is to allow the JavaScript system in Rails to work without configuration.

$ cd ruby_test
$ echo "gem 'therubyracer'" >> Gemfile
$ bundle install
Using therubyracer 0.12.3
Bundle complete! 19 Gemfile dependencies, 81 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

Create the welcome index view.

$ rails generate controller Welcome index
Running via Spring preloader in process 526
      create  app/controllers/welcome_controller.rb
       route  get 'welcome/index'
      invoke  erb
      create    app/views/welcome
      create    app/views/welcome/index.html.erb
      invoke  test_unit
      create    test/controllers/welcome_controller_test.rb
      invoke  helper
      create    app/helpers/welcome_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/welcome.coffee
      invoke    scss
      create      app/assets/stylesheets/welcome.scss

Precompile the assets, such as CSS, for the Ruby on Rails application.

$ rails assets:precompile
Yarn executable was not detected in the system.
Download Yarn at https://yarnpkg.com/en/docs/install
I, [2019-11-15T14:52:08.280897 #489]  INFO -- : Writing public/assets/application-9dcbxxxxx69.js
I, [2019-11-15T14:52:08.281208 #489]  INFO -- : Writing public/assets/application-9dcxxxx069.js.gz
I, [2019-11-15T14:52:08.285647 #489]  INFO -- : Writing public/assets/application-f0d7xxxxx5794.css
I, [2019-11-15T14:52:08.285786 #489]  INFO -- : Writing public/assets/application-f0d704xxxx794.css.gz

Replace the welcome view index with our hello world message.

$ echo '<h1>Hello, World! Ruby on Rails</h1>' > app/views/welcome/index.html.erb

Edit the file config/routes.rb with your favorite text editor to add the following before the end of the draw routes.

root 'welcome#index'

Final file should look like

Rails.application.routes.draw do
  get 'welcome/index'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root 'welcome#index'
end

In cPanel, visit the Application Manager.

In the Application Manager, register the application as follows.

Contact us to have the deployed application made active.

Making changes to your application live

After you make changes to your application, you will have to restart the application. You can do this with the following command.

passenger-config restart-app

If you have any questions about this process, feel free to contact us!



Questions? Contact our sales team for help!