Friday, 7 November 2014

How to setup a Rails Apache Passenger Ubuntu Server

This post describes how I set-up a Rails environment on Ubuntu. I used Passenger and Apache to serve the application. It details all the commands and configuration changes I did to get it working.

During the installation I used the following application versions

Ubuntu 14.04
Ruby 2.1.4
Rails 4.1.7
Passenger 4.0.53

Step 1 - install ubuntu


For the first step we need to get an OS ready to use for the installation.  I chose to use Ubuntu 14.04 LTS installed on an Amazon EC2 instance.

Step 2 -  update ubuntu


Log onto the server and download the latest package lists and repositories using the following command.


sudo apt-get update


Step 3 - create the application user


create a new user to be used to install the application

sudo adduser httpuser

Use the following command to set the password

sudo passwd httpuser

allow the new user to sudo

sudo visudo

 add the following lines to the file and save

httpuser ALL=(ALL) ALL


Step 4 - install ubuntu dependencies for Ruby


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


Step  5 - install Apache 2


This command will install Apache 2.4

sudo apt-get install apache2


Step 6 - install rvm, Ruby and rails


I choose to use rvm to manage Ruby. The follow method installs it as root.

\curl -sSL https://get.rvm.io | sudo bash -s stable

Note: I got an error when i initially did this command, stating the public key could not be found. I did the suggested command and then reran the command to install rvm.

source the rvm scripts by entering

source "/usr/local/rvm/scripts/rvm"

Login as root and install Ruby using rvm.

rvm install 2.1.4

Change the default version and test the correct version of Ruby is intalled.

rvm use 2.1.4 --default
ruby -v

Update the RubyGems

gem update --system

Install the rails gem

gem install rails


Step 7 - create a Rails test app to be used by Passenger


Login as the application user httpuser.

sudo mkdir /var/www/demoapp

Change the permissions to allow application user to write to directory

cd /var/www/demoapp
sudo chmod 777 .

Create a new rails app in the current directory /var/www/demoapp using the following command

rails new . 

Create a welcome page for the application

rails generate controller welcome index

Amend routes.rb to create a root to the new page
I uncomment root welcome#index


Step 8 - install Passenger


Install the passenger gem

gem install passenger

run the passenger apache2 install script

passenger-install-apache2-module

Note: during the install the script notice a few pre-requisite packages were missing so I installed them.

sudo apt-get install apache2-threaded-dev
sudo apt-get install libapr1-dev
sudo apt-get install libaprutil1-dev

Create a Passenger load configution for Apache

sudo vi /etc/apache2/mods-available/passenger.load

Add the following lines to the file and save it.

LoadModule passenger_module /usr/local/rvm/gems/ruby-2.1.4/gems/passenger-4.0.53/buildout/apache2/mod_passenger.so

Create passenger conf file for Apache

sudo vi /etc/apache2/mods-available/passenger.conf

Add the following lines to file and save it.

<IfModule mod_passenger.c>
  PassengerRoot /usr/local/rvm/gems/ruby-2.1.4/gems/passenger-4.0.53
  PassengerDefaultRuby /usr/local/rvm/gems/ruby-2.1.4/wrappers/ruby
</IfModule>

Enable the new apache passenger module

sudo a2enmod passenger


Step 9 - Configure Apache


Create the apache application virtual host config

sudo vi /etc/apache2/sites-available/demoapp.conf

and add the following  to the conf file

<VirtualHost *:80>
   ServerName localhost
   # !!! Be sure to point DocumentRoot to 'public'!
   DocumentRoot /var/www/demoapp/public       
   <Directory /var/www/demoapp/public>
      # This relaxes Apache security settings.
      AllowOverride all
      # MultiViews must be turned off.
      Options -MultiViews
      # Uncomment this if you're on Apache >= 2.4:
      Require all granted
   </Directory>
</VirtualHost>

Add secret to environment variables to enable the application to work.  Login as httpuser and perfotm the following.

cd /var/www/demoapp
rake secret

Edit the apache2 environment file.

sudo vi /etc/apache2/envvars

Add the following to the file:

export SECRET_KEY_BASE=xxxxxxx

where xxxxxx is the secret key returned from the rake secret command
restart apache to enable the new configuration

sudo service apache2 restart

Step 10 - Test the app


browse to http://localhost/welcome/index to view your app!








No comments:

Post a Comment