This project will be done on a Windows machine and deployed to an Ubuntu Server on AWS EC2. We will be making use of a tool called PuTTY to ssh into the instance from our local machine. Let’s get started:
CREATE A SAMPLE APP
We will start first by creating a simple rails blog project via command prompt on our local machine:
rails new testapp
cd testapp
rails g scaffold blog title:string content:text
rake db:create
rake db:migrate
rails s
Go to http://localhost:3000 on your browser and you should see the welcome rails page
Go to testapp directory and edit /config/routes.rb by adding
root 'blogs#index'
Reload localhost:3000 page on your browser and you should be served your blog index page
Ctrl C to stop your server.
OVER TO GITHUB
We will create a repository called testapp
Back to your Command Prompt:
git init
git add .
git commit -am '1st_commit'
git remote add origin https://github.com/ohiodn8/testapp.git
git push -u origin master
Go back to your browser and refresh your github repository and your app files and directory should load up.
TIME FOR AWS EC2
Log into your AWS account and locate Ec2 . On your Ec2 dashboard, click the Launch Instance Button
We’ll be using Ubuntu Server, click on that
I’ll be using the t2.micro (free tier eligble), click Next
Nothing to do on this page… Amazon will use a default VPC that it created for you. If you have no knowledge of how AWS VPC works, please leave it as it is… click next to storage…
I’ll leave it as it is and click Next
On the Add Tag Page, I’ll click Add Tag and put in the value as the image below (the info added is by choice, you could leave it empty)
For the Configure Security Group, I’ll create a new security group and give it an SSH rule (that should be the default setting). Click Next
Right Now You should be on a page that says review instance and launch, click launch.
A key Pair setting should pop up. If you know how key pairs work and have the .pem file on your local machine you can use that but for the sake of this tutorial, I’ll create a new one. I’ll call it remote and then I’ll click the Download Key Pair button before I launch the instance. Once the instance is launched, scroll down and click View Instance
You should see…
(I have an Instance I just terminated, don’t mind that.)
Time for PuTTY and SSH
For Windows we will need a tool called Putty. If you don’t already have it, search for it online and download. (if you’re trying this tutorial from an Ubuntu machine you should use the terminal and check online for the steps. You can also get PuTTY on your Ubuntu machine.)
We’ll use the PuttyGen to convert our .pem to .ppk and we’ll PuTTY to ssh into the instance (I’ll show images to guide you.)
Start your putty Gen and click Load button
import the key pair you saved from creating your instance to PuttyGen
Then click Save Private Key button. We’ll be using the saved key (.ppk file ) to ssh into our AWS server.
Close your PuttyGen.
Now open PuTTY and copy the public IP (IPv4) from your running instance page (on the browser) to the field that says Host Name(or IP address) on PuTTY…
click the SSH tab/dropdown (the plus icon) .
Locate Auth and click on it (Not the plus icon)
On that page, click the Browse button and add the converted key pair(.ppk), then click open at the bottom of the App/Page
You will get pop-up/alert, click on Yes
$ login as: ubuntu
and we’re in.
Now on your Ec2 Instance/Server terminal, patch your Instance by running…
sudo apt-get update && sudo apt-get -y upgrade
Setup a Development Environment for your Rails App
Still on the Ec2 Instance/server…
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 \curl -sSL https://get.rvm.io | bash -s stable
source /home/ubuntu/.rvm/scripts/rvm
rvm install ruby
This will install the most recent version of ruby, but I’ll use…
rvm install ruby-2.2.5
This will install ruby version 2.2.5, and this is because I have ruby version 2.2.5 installed on my local machine.
Just to check the version you’re using, type
rvm list
Install bundler…
gem install bundler --no-ri --no-rdoc
Install node JS
sudo apt-get install -y nodejs
We’ll Install Sqlite3
sudo apt-get install sqlite3
Install Git
sudo apt-get install git
Clone the repository you created on github into the Ec2 Instance/Server
git clone https://github.com/ohiodn8/testapp
ls
(this is to check if the app folder is in there. You should get…)
cd testapp
bundle install
rake db:create
rake db:migrate
To get the app running run
rails s -b 0.0.0.0
You should have a running rails server on your Ec2 Ubuntu server. Now go back to your browser, the Ec2 Instance page, and locate the part that says Security Group and click on it (or open to a New Tab)…
And you should appear on the security group page. Click on Inbound and then Edit button
And you should have this…
Click the Add Rule Button, change the port to 3000 and source 0.0.0.0/0
then click save.Return to your Ec2 Instance (by clicking Instances on the left sidebar) and copy your Public IP (IPv4)
Copy the public IP to a new tab in your browser and set port to 3000.35.161.101.100:3000 <please use your public IP>The page should load.
Making Changes and Re-deploying
Let’s say we want to edit our application on our local machine, push it to github and then update our application on the Ec2 Instance/Server… let’s do that. I’ll be adding bootstrap styling, so back to our local machine.
Add to the Gemfile in your rails app (on your local machine)
gem 'bootstrap-sass'
On the command prompt
bundle install
In your app/assets/stylesheets/application.scss (should be .css change to .scss), add
@import "bootstrap-sprockets"; @import "bootstrap";
save.Still in the stylesheets folder, delete scaffolds.scss In your app/assets/stylesheets/javascripts.js, add
//= require bootstrap-sprockets
…just after //= jquery
Your app/views/layouts/application.html.erb should look like this
<!DOCTYPE html> <html> <head> <title>Testapp</title> <%= csrf_meta_tags %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> </head> <body> <header class="navbar navbar-default" role="navigation"> <div class="navbar-inner"> <div class="container"> <div id="logo" class="navbar-brand"><%= link_to "Test-App", root_path %></div> <nav class="collapse navbar-collapse navbar-ex1-collapse"> <ul class="nav navbar-nav navbar-right"> <li><%= link_to 'New Blog', new_blog_path %></li> </ul> </nav> </div> </div> </header> <div class="container"> <%= yield %> </div> </body> </html>
on your command prompt run
rails s
…to see how it looks on your local machine and then stop it.Let’s push to github
git add .
git commit -am '2nd_commit
git push origin master
Now to your Ec2 Instance/Server…If you have your rails server still running, stop it with Ctrl Cthen type
git pull https://github.com/ohiodn8/testapp
You’ll get an error like the image below
this means git fork worked but git merge didn’t (git pull = git fork + git merge)
Follow the guide on your screen.
I’ll do
git commit -am 'correction'
git pull https://github.com/ohiodn8/testapp
you’ll get the image below
Save by using Ctrl X then Enter
your screen should look like this…
bundle install
Run your server
rails s -b 0.0.0.0
Reload the page in your browser again and you should see the update.
We’re going to stop our server with Ctrl C and this time run the server process in background
Just add -d
rails s -d -b 0.0.0.0
You can go back to your browser and refresh the page and try creating a new blog.
To check this particular process running in background mode run, type
ps aux | grep puma
To stop the process
kill -s SIGTERM 29404
That’s all!
You can type exit to close your instance
and remember to terminate your running instance if you don’t need it.