We are going to setup and configure the Rails server on CentOS with Nginx and Unicorn using a JodoHost VPS account.
• Our VPS system Details
o VPS Operating System: CentOS
o VPS Hostname: server1.example.com
o Server IP Address: 1.1.1.1
o Username: username
• Sign up for VPS
o To configure the Rails server we need a VPS to configure. For easy configuration and
management we shall sign up at JodoHost.
o Choose from the various available plans that best suit your needs.
o Step 1: Set up a domain name for yourself. I shall be using an existing domain in this case. You may also opt for new domain registration from here.
o Step 2: Fill all the details required in the form and set your root password for the server here and
proceed next.
o Step 3: Choose a payment method and complete your order.
o On the next page you shall see the invoice of your order. You should have already received a
copy of the invoice on your registered email and you may also download the copy from this
page also.
o After this you will shortly receive an email at your registered email address for the payment
confirmation and your VPS details.
• Login to VPS
o We shall login to our VPS machine via SSH:
§ ssh [email protected]
o Enter the password when prompted and you shall be logged into your VPS.
• Updating And Preparing The Operating System
o Run the following command to update the default tools for your CentOS VPS:
§ yum -y update
o Install the bundle containing development tools by executing the following command:
§ yum groupinstall -y ‘development tools’
Add the EPEL software repository for the YUM package manager to use. It will install some of the
packages we need for this tutorial (e.g. libyaml-devel, nginx etc.)
o Enable EPEL Repository
§ su -c ‘rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm’
o Update again to update the packages from the newly added repository.
§ yum -y update
o Get curl-devel and several other tools and libraries for this tutorial (e.g. Rails needs sqlitedevel).
§ yum install -y curl-devel nano sqlite-devel libyaml-devel
• Setting Up the Ruby Environment and Rails
o 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
o Get RVM to download and install Ruby version 2.1.0:
§ rvm reload
§ rvm install 2.1.0
o Rails needs first and foremost a JavaScript interpreter to work – we will also need to set up
Node.js. Run the following to download and install Node.js:
§ yum install -y nodejs
o Execute the following command to download and install Rails using the bundler gem:
§ gem install bundler rails
• Installing Nginx
o Run the following to download and install Nginx
§ yum install -y nginx
• Installing Unicorn
o Since it is an application-related dependency, the most logical way is to use RubyGems. Run the
following to download and install Unicorn:
§ gem install unicorn
• Preparing Rails Applications For Deployment
o We shall be creating a sample application to test our server. First we are going to create a
directory to keep our Rails applications and then a folder for our test application.
§ cd /var
§ mkdir www
§ cd www
§ rails new sample_app
o Enter the application directory
§ cd sample_app
o Create a sample resource
§ rails generate scaffold User name:string age:integer
o Create a sample database
§ RAILS_ENV=development rake db:migrate
§ RAILS_ENV=production rake db:migrate
o Create a directory to hold the PID files
§ mkdir pids
o To test that your application is set correctly and everything is working fine, enter the application
directory and start a simple server using the following command:
§ rails s
• Configuring the web and app servers
o First create a shared directory to store logs, etc
§ mkdir /var/www/shared
Unicorn Configuration
o Let’s configure Unicorn to work with Nginx and set the paths for logs, etc. Here we shall set the
configuration details, like the root path of our application. As this file resides inside our application this configuration will be application-specific. We are going to set up a basic configuration here. There are a lot of options available to configure with more controls. Edit the Unicorn file inside the ‘config’ folder at the root of your application:
§ nano config/unicorn.rb
Sample Configuration for ‘ config/unicorn.rb’:
root_path = “/var/www/sample_app”
shared_path = “/var/www/shared”
working_directory root_path
pid “#{shared_path}/tmp/pids/unicorn.pid”
stderr_path “#{shared_path}/log/unicorn.log”
stdout_path “#{shared_path}/log/unicorn.log”
listen 3000
worker_processes 2
timeout 30
Nginx Configuration
o Edit the Nginx configuration file to set it working with our Unicorn app server
§ nano /etc/nginx/conf.d/default.conf
Sample Configuration for ‘/etc/nginx/conf.d/default.conf ’:
upstream app {
# Path to Unicorn SOCK file, as defined previously
server 0.0.0.0:3000;
}
server {
listen 80;
server_name localhost;
# Application root, as defined previously
root /root/sample_app/public;
try_files $uri/index.html $uri @app;
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
• Start the servers
o Run the Unicorn server
§ unicorn_rails -c config/unicorn.rb –D
o Restart the Nginx server
§ service nginx restart
We have successfully set up the Rails server, testing it with our sample app, on the JodoHost VPS server and our newly configured Rails server is ready to use.
]]>