Deploy ruby on rails on AWS

Snehacynixit
8 min readJun 13, 2020

In some situations you’ll have to decide at some point where to host your Ruby on rails application. Choosing among dozens of excellent hosting providers may not be as easy as each provider has its own price, flexibility in deployment or configuration, and scalability to name a few.

You can make the right choice about where to host your application depending on your application requirements and budget. In this article let us use Aws for your deployment. ruby on rails training helps you to learn more effectively.

AMAZON Web services on ruby on rails

Amazon Web Services ( AWS) supports the automation of deployment of a ruby on rails application via Amazon Elastic Beanstalk, a service that manages and manages capacity provisioning, load balancing, application dependencies, scaling and health monitoring automatically.

AWS EC2 instance for ruby on rails application

Running a virtual EC2 instance and having S3 disk storage are the key prerequisites for launching your ruby on rails application. Amazon lets you select the most affordable options ( e.g. t2.micro with one CPU core and 0.5 GB of memory) and later scale up your application. You can configure and deploy your ruby on rails app automatically using Amazon Elastic Beanstalk which comes free after you set up your cloud environment. As an additional benefit, you may be eligible for one-year use of free-tier AWS services including instances, storage, elastic IPs, and more, if you are a new user. Today I’ll tell you how to install the Amazon Ec2 instances web application on rails.

FIRST create an AWS account.

You will get a screen like this after creating an account, its aws console.

Deploy ruby on rails application in AWS

So let’s start deploying the Amazon Web Services, Rails app

First thing we need to do is set up an instance in which to deploy a rails app. Only follow the steps, then:

•From the console Click on the EC2 option that will take us to the EC2 Dashboard where the latest Amazon EC2 resource status is displayed

  • Click on the launch Instance option underneath.

Then we go to a page that asks for the Amazon Machine Image, which means what base operating system we want in our servers, so let us use ubuntu 16.04. Different images have different packages and libraries of configurations installed thereon. Then you can deploy ruby on rails on AWS EC2.

Implementing integration with Code Deploy AWS EC2 for a Ruby on Rails application

Some of the integration steps are general for all types of applications incorporated with CodeDeploy. I’m not going to delve into them as there are AWS resources in this post’s references section, so you can get more information. Such as follows.

  • Using AWS CLI or AWS Management Console you should create an application on AWS CodeDeploy.
  • Selecting your Autoscaling Group would add a deployment group to your CodeDeploy program.
  • AWS CodeDeploy agent should be mounted on the instances of your EC2, which are included in this deployment.

Let us assume that Passenger-Nginx is installed on your instances and that your web application is served from the folder deployment where pisirpaylas is our application name. With your application name, you can rename your application folder, or use Apache instead of Nginx. You would then need to appropriately configure appspec.yml and the scripts below. But to the rest, the theory is the same.

After you have configured these settings, you can add files and directories together with other program code to the root of your program repository. The most important one is appspec.yml which specifies the steps for deployment.

And appspec.yml

Every application with CodeDeploy should have this file at the root of its code repository, In which step it defines which scripts to run. To put such scripts, you usually define a folder called deployment scripts under the root. Renaming this folder is free but appspec.yml is a must.

Our appspec.yml file looks like this:

Variante: 0.0

Linux: boot.

Documents:

— Source: /Source

Destination: /var / www / pisir paylas / setup

Authorisations:

— Object: /var / www / pisir paylas / setup

Property owner: nginx

Band: nginx

Profile: “*”

Fashion: 775

Type: Type:

— Datei file

— Répertoire

Hooks:-Hooks:

BeforeInstallation:

— Location: file scripts / arrange-deploy-folders.sh

Runas: Rotaries

AfterInstallation:

— Location: install scripts / db-config.sh

Nginx: runas

— Location: scripts / install-gems.sh

Nginx: runas

— Location: install scripts / compile-assets.sh

Nginx: runas

— Location: scripts / run-db-migrations.sh

Nginx: runas

StartupApplication:

— Location: upload scripts / finish-install.sh

Runas: Rotaries

The first two are regular ones. The AWS CodeDeploy Version is unique and not relevant to your program. This should be 0.0.0. I deploy to instances on Linux so that attribute os defines this.

The attribute to files is a directory where your program is installed. We have but one destination for this example.

Source describes which folder in the code repository contains an application. We deploy the folder as it is for Ruby on Rails so it is root, /.

Destination is the place where the instance application should be deployed. As I have already mentioned, I assume our application is loaded from /var / www / pisir paylas / deployment.

The permission section defines which OS user and group should have folder and file level permissions after the installation. This is about all the mounted files and directories under /var / www / pisir paylas / deployment.

Part hooks is where the magic takes place. Here you list the specific lifecycle events in CodeDeploy and the order of the scripts therein. You can also define which OS user should be running the script, as I have. It should be a user which has no password. Let us suggest that you run a command that allows installations using a non-root user to avoid potential security issues. Follow these steps through the ruby on rails application on AWS. Ruby on rails course for more skills and techniques.

STEP 1:

Pick the 16.04 LTS (HVM) Ubuntu Server, SSD Volume Form.

The next step tells us about the type of machine and the configurations required in the image, such as CPUs Memory etc.

STEP 2:

Click Next and Configure Instance Details, let it remain as it is, you can check if you want to know about the other prices of configurations etc.

STEP 3:

Allow as it is, and press Next: Add Store

Now in this step we will reduce the storage space we need by default to its 8 GB, but we can adjust it. AWS gives new customers a one-year free trial for installations, and storage up to 30 GB in a picture. So in the previous step we can increase it up to 30Gb and it won’t cost us, 8Gb is also enough.

STEP 4:

Click Next: Don’t apply Tags here, forget it

STEP 5:

Click Next: Security community configure.

Now that part is relevant

Our machine is deployed somewhere on Amazon’s cloud by default and to gain access to it we need to open permissions on their machine through a security group that will allow us to access that device on our host machine(my laptop).

Step 6:

You should then edit SSH(secure shell) rule that source choice should be anywhere

And custom TCP should be anywhere with post 80 and even source choice.

STEP 7:

Then click Review and Launch on the final step.

It will ask the key pair to create a new key pair, and will download it automatically. Add key pair name to suppose “demo”

The downloaded file will resemble demo.pem

To get access to the system, KEY PAIR is needed to connect from our local computer. It’s a proprietary access key and we don’t have to share it with others.

So when we go back to our console and open the Running instances, we have the instances running.

The secret IPv4 Web IP can be seen here.

CONNECTING THE TERMINAL of the MACHINE

Open terminal and go to the Downloads directory where you download the Key pair

Download 400 demo.pem with $ sudo chmod

Connecting from terminal

Downloads the public IP $ ssh -i demo.pem ubuntu@IPv4

You are now connected to our hosting system, we need to install rails and other dependencies

FILL out the deployment commands

Apt-get update ubuntu@ip-172–31–17–122: sudo

Install the apt-get dependencies rbenv and ruby:

$sudo apt-get install the libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software libffi-dev assets

You are now ready to rbenv install. The easiest way to do this is to run these commands, as the user using Ruby will

(With bash_profile changes to bashrc)

Git:/github.com/sstephenson/rbenv.git.rbenv $git clone

$echo ‘PATH export=’$HOME/.rbenv/bin:$PATH”>>~/.bashrc

$eval ‘$(rbenv init -)” >>~/.bashrc

Git:/github.com/sstephenson/ruby-build.git $git clone ~/.rbenv / plugins / ruby-build

$echo ‘PATH export=’$HOME/.rbenv/plugins/ruby-build/bin:$PATH”>>~/.bashrc

~/.bashrc $source

Update Ruby

Install $rbenv -version 2.2.3

European 2.2.3.$rbenv

You probably won’t want Rubygems to generate local documentation for every gem you ‘re installing, as this process can be lengthy. To disable this instruction, execute the following.

$echo “gem” > ~/.gemrc: — no-document

Rails install

Install rails $gem

Rehash $rbenv

Verify that Rails has been properly installed by printing its version, using this command:

$railing -v

Enable Runtime in Javascript

A few characteristics of Rails, such as the Asset Pipeline, rely on a runtime of Javascript. To provide the functionality, we must install Node.js.

To apt-get add the node.js PPA.

Ppa: chris-lea / node.js $sudo add-apt-repository

Then update apt-get, and install the package Node.js:

Update $sudo apt-get

The $sudo apt-get nodejs update

Now, Ruby on Rails is built on your AWS machine.

Conclusion

I hope you reach a conclusion about Ruby on rails for AWS. You can learn more through Ruby on rails online training.

--

--