How to deploy a Rails app with Passenger and Apache in Ubuntu 14.04

How to deploy a Rails app with Passenger and Apache in Ubuntu 14.04

Introduction

If you are a Ruby on Rails developer, you need a web server to host your web apps. This article shows you how to use Phusion Passenger as your Rails-friendly web server. Passenger is easy to install, configure, and maintain and it can be used with Apache. In this article, we will learn to install Passenger on Ubuntu 14.04.

Step 1 — Set Up Your Domain

In order to ensure that your site will be up and visible, you need to set up your DNS records to point your domain name towards your new server. Alternatively, you can always access your website via an IP address on your local server.

Step 2 — Install RVM

Before you start, an update of your Ubuntu operating system would ensure the upkeep of all the packages to be installed. You can do this with the following command:

      sudo apt-get update

Next step would be to install some libraries and other dependencies. This will make further ruby installations as smooth as possible. Your command for this would be:

     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 libsqlite3-dev sqlite3

The Ruby Version Manager, known more widely as RVM, allows you to easily install multiple, contained versions of Ruby and easily switch between them. To install RVM use the following command:

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

Alternatively, you can install RVM with a stable version of Ruby with the command:

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

The Ruby version installed along with the RVM in the above command can be identified through:

      ruby -v

Once you’ve decided on the Ruby version you wish to install, you can issue the command:

     rvm install ruby_version

Multiple Ruby versions installed on the RVM can be listed with:

     rvm list

You can switch between Ruby versions using:

     rvm use ruby_version

You will now have to install a recent version of Bundler, a package library, specifically, a RubyGem. Use the following command for the same:

     gem install bundler

Step 3 — Install Apache

Apache is the most popular web server available. The Apache HTTP server is a program that runs in the background under an appropriate operating system, which supports multi-tasking, and provides services to other applications that connect to it, such as client web browsers.

To install Apache, key in this command:

     sudo apt-get install apache2

Step 5 — Install Phusion Passenger

Phusion Passenger is a free web and application server with support for Ruby, Python and Node.js. It is designed to integrate into the Apache HTTP Server or the nginx web server, but also has a mode for running standalone without an external web server. Phusion Passenger supports Unix-like operating systems, and is available as a gem package, as a tarball, or as native Linux packages. Passenger allows Apache (and also Nginx!) to serve Ruby apps, almost magically. Passenger’s goal is to make everything Just Work with as little hassle as possible.

The preferred method to install Passenger via Ruby Gems is:

     sudo gem install passenger

     sudo passenger-install-apache2-module

Once the Passenger has been installed, you need to Restart Apache using the command:

     sudo service apache2 restart

Step 6 — Deploy your Rails Application

Now you can deploy your own Rails application if you have one ready. The steps for this are:

Move to your user home directory with the command:

     cd~

You now ready to install the Rails gem. You command for this would be:   

    sudo gem install rails

– OR – If you want to create a new rails app, the steps are as follows. We will use the name ‘demoapp’ in our example.    rails new demoapp The command to enter the directory is:\    cd demoapp You can now install the Bundler using the command:    bundle install We shall now create a virtual host file for our project. We will name the file as ‘demo.conf’. This can be done by copying the default Apache virtual host:      sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/demo.conf Now open the config file with the command:      sudo nano /etc/apache2/sites-available/demo.conf Insert the following text into the file to set the connections:      <VirtualHost *:80>            ServerAlias www.domain.com            ServerAlias domain.com            ServerAdmin [email protected]            DocumentRoot /path/to/raills/app/public            RailsEnv production            ErrorLog ${APACHE_LOG_DIR}/error.log            CustomLog ${APACHE_LOG_DIR}/access.log combined            <Directory “/path/to/raills/app/public”>                 Options FollowSymLinks                 Order allow,deny                 allow from all           </Directory>      </VirtualHost> Basically, this file enables listening to our domain name on port 80, sets an alias for the www subdomain, sets the mail address of our server administrator, sets the root directory for the public directory of our new project, and allows access to our site. To test our setup, we must connect to the Rails Welcome aboard page. However, this works only if the application is started in the development environment. Passenger starts the application in the production environment by default, so we need to change this with the RailsEnv option. If your app is ready for production you’ll want to leave this setting as it is. If you don’t want to assign your domain to this app, you can skip the ServerName and ServerAlias lines in the above script, or use your IP address there.

Access control

In apache2.2, access control based on client hostname, IP address, and other characteristics of client requests was done using the directives Order, Allow, Deny, andSatisfy.In apache2.4, such access control is done in the same way as other authorization checks, using the new module mod_authz_host.
apache2.2 configuration: apache2.4 configuration:
     Order deny,allow Require all denied
     Deny from all

Now save the file with the keyboard shortcut CTRL+X

You are now ready to enable you new site and restart Apache. The commands are:

     sudo a2ensite demo

     sudo service apache2 restart

Your app’s website should be accessible now. Navigate to your Droplet’s domain or IP address:

     http://www.domain.com or IP address

]]>