Introduction

This tutorial is for Ruby on Rails developers wishing to host their web apps. We will show you how to install and use Phusion Passenger as your web server. Passenger can be used with Nginx or Apache with the benefits of easy installation, configuration, and maintainance.

Step 1 - Create Server

To create a server, follow the steps in our First Steps wiki for creating a new Cloud Server.

Step 2 - Setup and Secure your Server

After the server is created you will need to create a system user and secure the server.

Follow the steps in our First Steps tutorial for setting up a SSH user and logging in.

Step 3 - Set Up Your Domain

Again this is explained in detail on our First Steps Wiki article.

Step 4 - Install Ruby

For this tutorial we will install Ruby manually from source.

As always, run an update to make sure that the system and all packages are up to date:

sudo apt-get update

Next we need to install dependencies:

sudo apt-get install build-essential libssl-dev libyaml-dev libreadline-dev openssl curl git-core zlib1g-dev bison libxml2-dev libxslt1-dev libcurl4-openssl-dev nodejs libsqlite3-dev sqlite3

Now we must create a temporary folder for Ruby source files:

mkdir ~/ruby

Change directory to the new folder:

cd ~/ruby

We need to download the latest stable Ruby source code. The current latest version is available from the Ruby website. Replace the link below if a newer version is available:

wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.3.tar.gz

Now extract the downloaded file:

tar -xzf ruby-2.1.3.tar.gz

Change to the extracted directory:

cd ruby-2.1.3

Next we need to run the configure script. Enter the following in your terminal:

./configure

Time to build the executable program. Run the ‘make’ utility by entering the following:

make

To install, run the same command with the ‘install’ parameter (Root access is required for this step to write to the directory). Compiled binaries will be copied to the /usr/local/bin folder:

shouldudo make install

There should now be an installation of Ruby on the system. You can check it with the following command:

ruby –v

Don't forget to delete the temporary folder:

rm -rf ~/ruby

Step 5 - Install Passenger and Nginx

In this step we will now install Passenger with APT (Advanced Packaging Tool).

Before we continue, install a PGP key:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7

Now create an APT source file (sudo privileges are needed):

sudo nano /etc/apt/sources.list.d/passenger.list

Insert the following line:

deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main

Exit by pressing CTRL+x to, and to save the file type y . To confirm the file location press ENTER.

Now, change the ownership and permissions for the file:

sudo chown root: /etc/apt/sources.list.d/passenger.list
sudo chmod 600 /etc/apt/sources.list.d/passenger.list

We need to update the APT cache:

sudo apt-get update

Install Passenger with Nginx:

sudo apt-get install nginx-extras passenger

Note: This step may overwrite our Ruby version to an older one. This is resolved by, removing the incorrect Ruby location and creating a new symlink to the correct Ruby file:

sudo rm /usr/bin/ruby
sudo ln -s /usr/local/bin/ruby /usr/bin/ruby

Step 6 - Set Up The Web Server

We need to open and edit the Nginx configuration file:

sudo nano /etc/nginx/nginx.conf

Find the following lines:

# passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
# passenger_ruby /usr/bin/ruby;

Uncomment both lines and update the path in the passenger_ruby line:

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/local/bin/ruby;

Now save and exit. Step - 7 Deployment

Should you have an App you can now deploy it. To deploy an existing app, you can upload your project to /etc/nginx/sites-available/default step.

Alternatively, we will create a new Rails app. We will need the rails gem to create the new app.

Change to your home directory:

cd ~

Install the rails gem (without extra documentation):

sudo gem install --no-rdoc --no-ri rails

We can now create a new app, called railsdemo_app.

rails new railsdemo_app --skip-bundle

Enter the directory:

cd railsdemo_app

We must install a JavaScript execution environment. Install the therubyracer gem.

Open the Gemfile:

nano Gemfile

Find the following line:

# gem 'therubyracer',  platforms: :ruby

Uncomment the line:

gem 'therubyracer',  platforms: :ruby

Save the file, and then run Bundler:

bundle install

Now we need to disable the default Nginx configuration:

sudo nano /etc/nginx/sites-available/default

Find the following lines:

listen 80 default_server;

listen [::]:80 default_server ipv6only=on;

Comment them out:

# listen 80 default_server;

# listen [::]:80 default_server ipv6only=on;

Now save the file.

Next we need to create an Nginx configuration file:

sudo nano /etc/nginx/sites-available/railsdemo_app

Add the following server block.

server {

  listen 80 default_server;

  server_name www.mydomain.com;

  passenger_enabled on;

  passenger_app_env development;

  root /home/rails/railsdemo_app/public;

}

Here we are enabling the following for our new project; listening on port 80, setting your domain name, enabling Passenger, and setting the root to the public directory.

The root line will need to be edited to match the upload location of your Rails app.

It’s not necessary to assign your domain to the app, so you can skip the server_name line, or use your IP address if you wish.

Passenger starts the application in the production environment by default. We want to test the app and see the Rails Welcome Aboard page. This works only if the application is started in the development environment. We need to change this with the passenger_app_env option.

Note: If your app is ready for production you'll want to leave this setting out.

Save the file and next, create a symlink:

symlinkudo ln -s /etc/nginx/sites-available/testapp /etc/nginx/sites-enabled/ railsdemo_app

Restart Nginx:

sudo nginx -s reload

You app should be available and accessible. Test by visiting your domain or IP address:

http://your_server_ip

Thats the end of the tutorial!


This article was last modified: June 3, 2016, 8:37 a.m.

0 Comments

Please log in to leave a comment.

Add or change tags.

A comma-separated list of tags.

Share

Hacker News

Top