{"id":26912,"date":"2016-06-17T11:59:22","date_gmt":"2016-06-17T06:29:22","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=26912"},"modified":"2016-06-17T13:20:08","modified_gmt":"2016-06-17T07:50:08","slug":"deploying-rails-app-using-passenger-with-nginx-on-ec2","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/deploying-rails-app-using-passenger-with-nginx-on-ec2\/","title":{"rendered":"Setup Rails with Nginx using Passenger"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35294\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/09\/nginx_passenger_eyecatcher.png\" alt=\"nginx_passenger_eyecatcher\" width=\"752\" height=\"302\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The demo aims at running rails application behind Nginx using Passenger. Nginx is a high performance webserver. Passenger is a free web server\/application server with support for Rails, Python, <a title=\"Nodejs development\" href=\"http:\/\/www.tothenew.com\/mean-node-js-development-consulting\">Node.js<\/a>. Passenger is highly stable and fast already service over 350,000 websites.<\/p>\n<h2>1. Installing the deployment tools<\/h2>\n<p>Installing Development tools:-<\/p>\n<p>[js]yum install &quot;Development Tools&quot; -y[\/js]<\/p>\n<p>Installing EPEL repository for installing Nginx:-<\/p>\n<p>[js]<br \/>\n# Enable EPEL Repository<br \/>\nsudo su -c &#8216;rpm -Uvh http:\/\/dl.fedoraproject.org\/pub\/epel\/6\/x86_64\/epel-release-6-8.noarch.rpm&#8217;<\/p>\n<p># Update everything, once more.<br \/>\nyum -y update<br \/>\n[\/js]<\/p>\n<p>&nbsp;<\/p>\n<h2>2. Setting up Ruby and Rails<\/h2>\n<p>RVM is a perfect tool for installing and managing Ruby\/Rails.<\/p>\n<p>[js]curl -L get.rvm.io | bash -s stable<br \/>\nsource \/etc\/profile.d\/rvm.sh[\/js]<\/p>\n<p>After the RVM installed, Install Ruby using RVM:-<\/p>\n<p>[js]rvm install 2.1.0[\/js]<\/p>\n<p>Nodejs is also required as rails requires a javascript interpreter.<\/p>\n<p>[js] yum install nodejs npm &#8211;enablerepo=epel<br \/>\ngem install bundler rails[\/js]<\/p>\n<p>&nbsp;<\/p>\n<h2>3. Installing Server applications<\/h2>\n<h3>3.1 Installing Passenger<\/h3>\n<p>We will be using gem to install passenger instead of the yum repository for the latest gem installation.<\/p>\n<p>[js]gem install passenger[\/js]<\/p>\n<p>Run the command <code>passenger<\/code> to verify the passenger installation:-<\/p>\n<p>[js]Phusion Passenger Standalone, the easiest way to run web apps.<\/p>\n<p>Available commands:<\/p>\n<p>  passenger start      Start Phusion Passenger Standalone.<br \/>\n  passenger stop       Stop a Phusion Passenger instance.<br \/>\n  passenger status     Show the status of a running Phusion Passenger instance.<\/p>\n<p>Run &#8216;passenger &lt;COMMAND&gt; &#8211;help&#8217; for more information about each command.<br \/>\n[\/js]<\/p>\n<h3>3.2 Setting up Nginx<\/h3>\n<p>Nginx must be compiled with Passenger for compatibility. This method is more preferred in comparision of installation from default repository. Installation can be done via Passenger application itself.<\/p>\n<p>Compiling and installing Nginx with Passenger compatibility:-<\/p>\n<p>[js]passenger-install-nginx-module[\/js]<\/p>\n<p>Confirm the choice of language (in our case Ruby). Usually it detects and selects the languages already installed on the server.<\/p>\n<p>[js]<br \/>\nUse &lt;space&gt; to select.<br \/>\nIf the menu doesn&#8217;t display correctly, ensure that your terminal supports UTF-8.<\/p>\n<p> \u2023 \u2b22  Ruby<br \/>\n   \u2b22  Python<br \/>\n   \u2b22  Node.js<br \/>\n   \u2b21  Meteor<br \/>\n[\/js]<\/p>\n<p>In next step, select 1st option i.e.<\/p>\n<h4>&#8220;Yes download and install Nginx for me<br \/>\nPress enter to continue.&#8221;<\/h4>\n<p>&nbsp;<\/p>\n<h2>4. Creating a sample Rails app\/upload the source code<\/h2>\n<p>Perform the below steps to create a sample application:-<\/p>\n<p>[js]<br \/>\n# Create a sample Rails app<br \/>\ncd \/usr\/local\/src<br \/>\nmkdir app<br \/>\ncd app<br \/>\nrails new testapp<br \/>\n\t#This will create the basic directory and file structure for rails app<br \/>\n\t# app &#8211;&gt; application<br \/>\n\t# bin  &#8211;&gt; any binaries\/executables<br \/>\n\t# config &#8211;&gt; config dir<br \/>\n\t# config.ru<br \/>\n\t# db &#8211;&gt; DB (SQLLite)<br \/>\n\t# Gemfile<br \/>\n\t# Gemfile.lock<br \/>\n\t# lib &#8211;&gt; library dir<br \/>\n\t# log &#8211;&gt; logs<br \/>\n\t# public<br \/>\n\t# Rakefile<br \/>\n\t# README.rdoc<br \/>\n\t# test<br \/>\n\t# tmp<br \/>\n\t# vendor<\/p>\n<p>cd testapp<\/p>\n<p>#create controllers and pages to be used as home page in our app<br \/>\nrails generate controller pages<br \/>\n[\/js]<\/p>\n<p>Edit the file <code> app\/controllers\/pages_controller.rb<\/code> and add the following lines to create the home action<\/p>\n<p>[js]class PagesController &lt; ApplicationController<br \/>\ndef home<br \/>\n @response= &quot;Hello World!&quot;<br \/>\nend<br \/>\nend [\/js]<\/p>\n<p>Create the home route in file <code>config\/routes.rb<\/code><\/p>\n<p>[js] root to: &#8216;pages#home'[\/js]<\/p>\n<p>Edit the file <code>app\/views\/pages\/home.html.erb <\/code><\/p>\n<p>[js]&lt;%= @response %&gt; [\/js]<\/p>\n<p>In the above app we have created a variable <code> response<\/code> in <code> pages_controller.rb<\/code> which has the value <code>\"Hello World!<\/code>. This variable is used to display the <code>Hello World<\/code> from the file <code>app\/views\/pages\/home.html.erb <\/code><\/p>\n<p>Execute the following command to set the <code> RAILS_ENV<\/code>.<\/p>\n<p>[js]RAILS_ENV=development<\/p>\n<p>[\/js]<\/p>\n<p>Test your app :<\/p>\n<p>[js]cd \/usr\/local\/src\/app\/testapp<br \/>\n  rails s<br \/>\n #visit the url http:\/\/yourip:3000<br \/>\n[\/js]<\/p>\n<p>&nbsp;<\/p>\n<h2>5. Configuring Nginx<\/h2>\n<p>Edit \/opt\/nginx\/conf\/nginx.conf and add the following directive after the <code>passenger_root <\/code> and <code>passenger_ruby<\/code> directives inside <code>http {.....}<\/code>:-<\/p>\n<p>[js]passenger_app_env development;[\/js]<\/p>\n<p>Further delete\/comment the below section in <code>server {....}<\/code><\/p>\n<p>[js]# location \/ {<br \/>\n#            root   html;<br \/>\n#            index  index.html index.htm;<br \/>\n#        }<br \/>\n[\/js]<\/p>\n<p>and add the below lines:-<\/p>\n<p>[js]root \/usr\/local\/src\/app\/testapp\/public;<br \/>\npassenger_enabled on;<br \/>\n[\/js]<\/p>\n<p>Start the Nginx server:-<\/p>\n<p>[js] \/opt\/nginx\/sbin\/nginx [\/js]<\/p>\n<p>&nbsp;<\/p>\n<p>Now, visit the ip of you server <code>http:\/\/yourserverip<\/code>. The Rails default welcome page should be up now. Hope this blog was useful in setting up Rails with Nginx.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; The demo aims at running rails application behind Nginx using Passenger. Nginx is a high performance webserver. Passenger is a free web server\/application server with support for Rails, Python, Node.js. Passenger is highly stable and fast already service over 350,000 websites. 1. Installing the deployment tools Installing Development tools:- [js]yum install &quot;Development Tools&quot; -y[\/js] [&hellip;]<\/p>\n","protected":false},"author":563,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[2348,1],"tags":[3438,1892,3437,1336,3436,3435,3439],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/26912"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/563"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=26912"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/26912\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=26912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=26912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=26912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}