<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>pankaj &#8211; redpanthers.co</title>
	<atom:link href="/author/pankaj/feed/" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>Red Panthers - Experts in Ruby on Rails, System Design and Vue.js</description>
	<lastBuildDate>Tue, 13 Jun 2017 14:59:33 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.2.7</generator>
	<item>
		<title>Setup your rails  application with one command</title>
		<link>/setup-rails-application-one-command/</link>
				<pubDate>Tue, 13 Jun 2017 14:59:33 +0000</pubDate>
		<dc:creator><![CDATA[pankaj]]></dc:creator>
				<category><![CDATA[Beginners]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=2155</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[Generally setting up a rails app is running some same set of commands, for example installing gem, creating database, loading schema and start rails server.


<pre class="lang:sh decode:true">bundle
bundle exec rails db:create
bundle exec rails s</pre>


We can create a script and run it to setup our application since Rails 4.2 a bin/setup file is generated by default.
Here is the example of setup script.


<pre class="lang:default decode:true">#!/usr/bin/env ruby
require 'pathname'
require 'fileutils'
include FileUtils
# path to your application root.
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
def system!(*args)
  system(*args) || abort("\n== Command #{args} failed ==")
end
chdir APP_ROOT do
  # This script is a starting point to setup your application.
  # Add necessary setup steps to this file.
  puts '== Installing dependencies =='
  system! 'gem install bundler --conservative'
  system('bundle check') || system!('bundle install')
  # puts "\n== Copying sample files =="
  # unless File.exist?('config/database.yml')
  #   cp 'config/database.yml.sample', 'config/database.yml'
  # end
  puts "\n== Preparing database =="
  system! 'bin/rails db:setup'
  puts "\n== Removing old logs and tempfiles =="
  system! 'bin/rails log:clear tmp:clear'
  puts "\n== Restarting application server =="
  system! 'bin/rails restart'
end
</pre>


Setup script sets your rails application for development environment instantly.  This is helpful when a new developer trying to set up your application or when setting up an application on a new machine. We should keep the setup script updated.
Let&#8217;s understand the setup script step by step and modify according to our requirements.
First setup basic required library and path of our application in <em>APP_ROOT</em>


<pre class="lang:default decode:true">#!/usr/bin/env ruby
require 'pathname'
require 'fileutils'
include FileUtils
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
</pre>


Now <em>system! </em>method here execute the commands passed to it and shows the backtrace of error in case of command fails.


<pre class="lang:default decode:true">def system!(*args)
  system(*args) || abort("\n== Command #{args} failed ==")
end
</pre>


<em>system</em> will execute the given rails command and <em>abort</em> will stop script execution and will show us the backtrace of failed command, we can modify it if we don&#8217;t want to stop the execution of the script and get some custom error messages.


<pre class="lang:default decode:true">def system!(*args)
  system(*args) || puts "#{args} -&gt; FAILED TO EXCECUTE"
end
</pre>


In the below snippet of the script, we are going to our application root and performing operations.


<pre class="lang:default decode:true">chdir APP_ROOT do
  puts '== Installing dependencies =='
  system! 'gem install bundler --conservative'
  system('bundle check') || system!('bundle install')
  puts "\n== Preparing database =="
  system! 'bin/rails db:setup'
  puts "\n== Removing old logs and tempfiles =="
  system! 'bin/rails log:clear tmp:clear'
  puts "\n== Restarting application server =="
  system! 'bin/rails restart'
end</pre>


To create config file if not already exist, modify your script as


<pre class="lang:default decode:true">   puts "== Copying sample files =="
   unless File.exist?('config/database.yml')
     cp 'config/database.yml.sample', 'config/database.yml'
   end</pre>

]]&gt;		</p>
]]></content:encoded>
										</item>
		<item>
		<title>Deploying Rails application on AWS, using Nginx, Puma and Mina</title>
		<link>/deploying-rails-application-aws-using-nginx-puma-mina/</link>
				<comments>/deploying-rails-application-aws-using-nginx-puma-mina/#comments</comments>
				<pubDate>Wed, 24 May 2017 07:44:18 +0000</pubDate>
		<dc:creator><![CDATA[pankaj]]></dc:creator>
				<category><![CDATA[DevOps]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=2150</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[In this tutorial we are going to setup an AWS EC2 instance for deployment of rails application.


<h2>Server Setup</h2>


Following is the setup instruction for ubuntu 16.04 using ruby 2.4, Postgres 9.6 and rails 5.


<h3>Installing Ruby</h3>


Setting up dependencies for ruby.


<pre class="wrap:true lang:default decode:true">sudo apt-get update
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 python-software-properties libffi-dev nodejs</pre>


Next step is to install ruby using RVM.


<pre class="lang:default decode:true">sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -sSL https://get.rvm.io/ | bash -s stable
source ~/.rvm/scripts/rvm
rvm install 2.4.0
rvm use 2.4.0 --default</pre>


Last step is to install bundler.


<pre class="lang:default decode:true">gem install bundler</pre>




<h3>Installing Rails</h3>


Installing NodeJS, this lets you use Coffee script and the <a href="http://guides.rubyonrails.org/asset_pipeline.html">Asset Pipeline</a> in Rails which combines and minifies your javascript to provide a faster production environment.


<pre class="lang:default decode:true ">curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs</pre>


Next is to install rails.


<pre class="lang:default decode:true ">gem install rails -v 5.0.1</pre>


You can verify the installation of rails version.


<pre class="lang:default decode:true">rails -v
# Rails 5.0.1</pre>




<h3>Installing PostgreSQL</h3>




<pre class="lang:default decode:true ">sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' &gt; /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-common
sudo apt-get install postgresql-9.5 libpq-dev</pre>


The postgres installation doesn&#8217;t setup a user for you, so you&#8217;ll need to follow these steps to create a user with permission to create databases.


<pre class="lang:default decode:true">sudo -u postgres createuser pankaj -s</pre>




<h3>Install Nginx</h3>




<pre class="lang:default decode:true">sudo apt-get update
sudo apt-get install nginx</pre>


Following are some basic commands to manage Nginx:


<pre class="lang:default decode:true">#Stop Nginx
sudo systemctl stop nginx
#Start Nginx
sudo systemctl start nginx
#Restart Nginx
sudo systemctl restart nginx
</pre>




<h2>Nginx Configuration</h2>


First disable default site by removing the symlink.


<pre class="lang:default decode:true">sudo rm /etc/nginx/sites-enabled/default</pre>


Now create a new virtual host config file.


<pre class="lang:default decode:true">cd /etc/nginx/sites-available/
touch my_app.conf</pre>


edit the <em>my_app.conf</em> file:


<pre class="lang:default decode:true">upstream my_app {
  server unix:///var/run/my_app.sock; #path to puma socket file
}
server {
  listen 80;
  server_name my_app_url.com; # change to match your URL
  root /var/www/my_app/public; # I assume your app is located at that location
  location / {
    proxy_pass http://my_app;/ # match the name of upstream directive which is defined above
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  location ~* ^/assets/ {
    # Per RFC2616 - 1 year maximum expiry
    expires 1y;
    add_header Cache-Control public;
    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }
}</pre>




<h2>Puma Configuration</h2>


Change <em>config/puma.config</em> file according to following example:


<pre class="lang:default decode:true "># Change to match your CPU core count
workers 2
# Min and Max threads per worker
threads 1, 6
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
# Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env
# Set up socket location
bind "unix://#{shared_dir}/sockets/puma.sock"
# Logging
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true
# Set master PID and state locations
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/pids/puma.state"
activate_control_app
on_worker_boot do
  require "active_record"
  ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
  ActiveRecord::Base.establish_connection(YAML.load_file("#{app_dir}/config/database.yml")[rails_env])
end</pre>




<h3>Create puma upstart script</h3>


Let&#8217;s create an Upstart init script so we can easily start and stop Puma.


<pre class="lang:default decode:true ">cd ~
wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/upstart/puma-manager.conf
wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/upstart/puma.conf</pre>


Now open the provided <code>puma.conf</code> file, so we can configure the Puma deployment user.


<pre class="lang:default decode:true ">vi puma.conf</pre>


Look for the two lines that specify <code>setuid</code> and <code>setgid</code>, and replace &#8220;apps&#8221; with the name of your deployment user and group.


<pre class="lang:default decode:true ">setuid ubuntu
setgid ubuntu</pre>


Now copy the scripts to the Upstart services directory.


<pre class="lang:default decode:true ">sudo cp puma.conf puma-manager.conf /etc/init</pre>


The <code>puma-manager.conf</code> script references <code>/etc/puma.conf</code> for the applications that it should manage. Let&#8217;s create and edit that inventory file now.


<pre class="lang:default decode:true ">sudo vi /etc/puma.conf</pre>


Each line in this file should be the path to an application that you want <code>puma-manager</code> to manage. Add the path to your application now. For example.


<pre class="lang:default decode:true">/path_to_application</pre>


Jungle upstart script provides following commands to manage puma app server:


<pre class="lang:default decode:true">#Start Puma
sudo start puma-manager
#Stop Puma
sudo stop puma-manager
#Restart Puma
sudo restart puma-manager</pre>




<h2>Install Mina</h2>


Add in your Gemfile.


<pre class="lang:default decode:true">gem 'mina'</pre>


and run <em>bundle install</em> to install mina.


<h3>Create deployment script</h3>




<pre class="lang:default decode:true">mina init</pre>


It will create <em>config/deploy.rb, </em>let&#8217;s edit it as following:


<pre class="lang:default decode:true">require 'mina/rails'
require 'mina/git'
require 'mina/rvm'
set :user, 'ubuntu' #deploy user
set :application_name, 'my app'
set :domain, 'myapp.com'
set :identity_file, 'myapp.pem' # ec2 instance key file
set :deploy_to, '/var/www/my_app' #path to app
set :app_path, lambda { "#{fetch(:deploy_to)}/#{fetch(:current_path)}" }
set :repository, 'git@github.com:example/myapp.git' #Remote Repo Path
set :branch, 'master'
set :shared_paths, ['log', 'tmp']
set :shared_dirs, fetch(:shared_dirs, []).push('tmp')
task :environment do
  invoke :'rvm:use', 'ruby-2.4.0@default'
end
task :setup do
  #create the folder structure
end
desc "Deploys the current version to the server."
task :deploy do
  deploy do
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'
    invoke :'deploy:cleanup'
    on :launch do
      invoke :'puma:restart'
    end
  end
end
namespace :puma do
  desc "Start the application"
  task :start do
    command 'echo "-----&gt; Start Puma"'
    command "sudo start puma-manager", :pty =&gt; false
  end
  desc "Stop the application"
  task :stop do
    command 'echo "-----&gt; Stop Puma"'
    command "sudo stop puma-manager"
  end
  desc "Restart the application"
  task :restart do
    command 'echo "-----&gt; Restart Puma"'
    command "sudo restart puma-manager"
  end
end
</pre>


Now to setup directory structure run:


<pre class="lang:default decode:true ">mina setup --verbose</pre>


To deploy the application run:


<pre class="lang:default decode:true ">mina deploy --trace</pre>


&nbsp;
REFERENCES:
<a href="https://gorails.com/setup/ubuntu/16.04">https://gorails.com/setup/ubuntu/16.04</a>
<a href="https://github.com/mina-deploy/mina">https://github.com/mina-deploy/mina</a>
<a href="https://github.com/puma/puma">https://github.com/puma/puma</a>
&nbsp;]]&gt;		</p>
]]></content:encoded>
							<wfw:commentRss>/deploying-rails-application-aws-using-nginx-puma-mina/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
							</item>
	</channel>
</rss>
