install ruby on rails and nginx on Amazon Linux AMI

Updating And Preparing The Operating System

In order to install Ruby and the other necessary application (e.g. our servers), we need to first prepare the minimally shipped CentOS droplet and equip it with some development tools we will need along the way.
Run the following command to update the default tools of your CentOS based droplet:
yum -y update

# This command will update all the base applications
# that come with CentOS by default. Which are mostly
# reserved for use by the operating system. 
Install the bundle containing development tools by executing the following command:
yum groupinstall -y 'development tools'

# With more recent versions of CentOS, such as 6.5 in our case,
# you can simply run:
# yum groupinstall -y development
# instead.

# This bundle of applications contains various tools
# Such as: gcc, make, automake, binutils, git etc.
Some of the packages we need for this tutorial (e.g. libyaml-devel, nginx etc.) are not found within the official CentOS repository. To simplify things and not to deal with manually installing them, we will add the EPEL software repository for YUM and other package managers to use.
# Enable EPEL Repository
sudo su -c 'rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm'

# Update everything, once more.
yum -y update
Finally, to get Passenger working with Nginx, which we are going to install in the next sections, we need the curl-devel library and nano text editor. Also for Rails, we are going to need sqlite-devel.
In order to install curl-devel and nano, run the following:
yum install -y curl-devel nano sqlite-devel libyaml-devel

Setting Up Ruby Environment and Rails

Note: This section is a summary of our dedicated article How To Install Ruby 2.1.0 On CentOS 6.5.
We are going to be using Ruby Version Manager (RVM) to download and install a Ruby interpreter (or "rubies", as referred by the RVM).
Run the following two commands to install RVM and create a system environment for Ruby:
curl -L get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
Finally, to finish installing Ruby on our system, let's get RVM to download and install Ruby version 2.1.0:
rvm reload
rvm install 2.1.0
After Ruby, we can use the RubyGems package manager to help us to get rest of the Ruby based tools, such as the Rails framework.
Since Rails needs first and foremost a JavaScript interpreter to work, we will also need to set up Node.js. For this purpose, we will be using the default system package manager YUM.
Run the following to download and install nodejs using yum:
yum install -y nodejs
Execute the following command using RubyGems' gem to download and install rails:
gem install bundler rails

Downloading And Installing The Server Applications

Note: If your VPS has less than 1 GB of RAM, you will need to perform the below simple procedure to prepare a SWAP disk space to be used as a temporary data holder (RAM substitute). Since DigitalOcean servers come with fast SSD disks, this does not really constitute an issue whilst performing the server application installation tasks.
# Create a 1024 MB SWAP space
sudo dd if=/dev/zero of=/swap bs=1M count=1024
sudo mkswap /swap
sudo swapon /swap

Phusion Passenger

Red Hat Linux's default package manager RPM (RPM Package Manager) ships applications contained within .rpm files. Unfortunately, in Passenger's case, they are quite outdated. Therefore, we will be using RubyGem, once again, to download and install the latest available version of Passenger -- version 4.
Use the below command to simply download and install passenger:
gem install passenger

# This command will fetch Passenger v4.0(.35+) for you.
To test Passenger is downloaded and set up correctly, try running passenger.
You should see an output similar to below:
Phusion Passenger Standalone, the easiest way to deploy Ruby web apps.

Available commands:

  passenger start            Start Phusion Passenger Standalone.

..

Nginx

Normally, to download and install Nginx, you could add the EPEL repository and get Nginx via yum. However, to get Nginx working with Passenger, its source must be compiled with the necessary modules. Do not worry, though! Passenger comes with a handy tool to make the process as simple as executing a single command.
Run the following to start compiling Nginx with native Passenger module:
passenger-install-nginx-module
Once you run the command, press Enter and confirm your choice of language(s) (i.e. Ruby, in our case). You can use the arrow keys and space bar to select Ruby alone, if you wish.
Use <space> to select.
If the menu doesn't display correctly, ensure that your terminal supports UTF-8.

 ‣ ⬢  Ruby
   ⬢  Python
   ⬢  Node.js
   ⬡  Meteor
In the next step, choose Item 1:
1. Yes: download, compile and install Nginx for me. (recommended)
    The easiest way to get started. A stock Nginx 1.4.4 with Passenger
    support, but with no other additional third party modules, will be
    installed for you to a directory of your choice.
And press Enter to continue.
Now, Nginx source will be downloaded, compiled, and installed with Passenger support.
Note: This action might take a little while -- probably longer than one would like or expect!

Preparing The Application For Deployment

Note: In this section, we're going to work with a very simple Ruby On Rails application as an example. For the actual deployment of your application, you should upload your codebase and make sure to have all of its dependencies installed.

Creating A Sample Application / Uploading Your Source Code

Let's begin with creating a very basic Rails application inside our home directory to serve with Passenger and Nginx.
Execute the following command to get Rails to create a new application called my_app inside the /var/www directory:
# Create a sample Rails application
cd /var
mkdir www
cd www
rails new my_app

# Enter the application directory
cd my_app

# Create a sample resource
rails generate scaffold Task title:string note:text

# Create a sample database
RAILS_ENV=development rake db:migrate
To test that your application is set correctly and everything is working fine, enter the app directory and run a simple server with rails s:
# Enter the application directory
cd /var/www/my_app

# Run a simple server
rails s

# You should now be able to access it by
# visiting: http://[your droplet's IP]:3000/tasks

# In order to terminate the server process,
# Press CTRL+C
Note: For the actual deployment, when you want to upload your code base to the server, you can use either SFTP or a graphical tool []such as FileZilla] to transfer and manage remote files securely.

Creating The Nginx Management Script

After compiling Nginx, in order to control it with ease, we need to create a simple management script.
Run the following commands to create the script:
nano /etc/rc.d/init.d/nginx
Copy and paste the below contents:
#!/bin/sh
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0

nginx="/opt/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $”Reloading $prog: ”
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
    $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
Press CTRL+X and confirm with Y to save and exit.
Set the mode of this management script as executable:
chmod +x /etc/rc.d/init.d/nginx

Configuring Nginx

In this final step of configuring our servers, we need to create an Nginx server block, which roughly translates to Apache's virtual hosts.
As you might remember seeing during Passenger's Nginx installation, this procedure consists of adding a block of code to Nginx's configuration file nginx.conf. By default, unless you state otherwise, this file can be found under /opt/nginx/conf/nginx.conf.
Type the following command to open up this configuration file to edit it with the text editor nano:
nano /opt/nginx/conf/nginx.conf
As the first step, find the http { node and append the following right after the passenger_root and passenger_ruby directives:
# Only for development purposes.
# For production environment, set it accordingly (i.e. production)
# Remove this line when you upload an actual application.
# For * TESTING * purposes only.
passenger_app_env development;
Scroll down the file and find server { ... Comment out the default location, i.e.:
..

#    location / {
#            root   html;
#            index  index.html index.htm;
#        }

..
And define your default application root:
root /var/www/my_app/public;
passenger_enabled on;
Press CTRL+X and confirm with Y to save and exit.
Run the following to reload the Nginx with the new application configuration:
/etc/init.d/nginx restart
To check the status of Nginx, you can use:
/etc/init.d/nginx status
In order to test your application (and our sample app), you can visit:
http://[Your droplet's IP addr]/tasks

# Listing tasks

# Title    Note 

# New Task
Note: To learn more about Nginx, please refer to How to Configure Nginx Web Server on a VPS.

Comments

Popular Posts

How to pass hash in Postman

nginx: unrecognized service

Bootstrap Select Picker append add new item if search not exist

Reading Excel Sheets using "Roo" gem in ruby on rails

Add CORS to Nginx on AWS Elastic Beanstalk

Enable gzip compression on Elastic Beanstalk with nginx

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Get video duration by URL in Ruby on Rails

site-enables nginx setting in ruby in rails