Intro

If you have followed through the Ruby on Rails tutorial in the previous posts, then you should have a basic Ruby on Rails web application on your local machine. The next step would be to move this onto a Production level web server so that the public can access it. In this article, we will go over two common hosting options that you can choose from to bring your web application to Production: Heroku and Amazon Web Services (AWS)

Heroku

If you are not interested in configuring web servers or setting up environments, then Heroku may be suitable for you. For Ruby on Rails applications, you can simply use their services to host your project for free without having to configure too much. This is great if you just want a simple “push to production” approach and let Heroku do all the server related things.

Pros

  • There is a free tier.
  • Easy and quick to get started.
  • Can scale easily if you pay for an upgrade.

Cons

  • Scaling to a higher tier can be more expensive compared to other services.
  • Free tier has limited hours per month and your application will shut down to save hours if inactive. This means that the first time you visit the website after it is inactive will take longer to load.
  • To add SSL certificates (make your website https instead of http) you will need to upgrade to at least the hobby ($7 a month) plan.

Other

  • To give your application a custom domain name, it is free, but you need to register your credit.
  • Heroku offers a lot more than just hosting, but for our purposes, using it to host is all we will cover.

Getting Started With Heroku

If you choose to go the Heroku way, then you can skip the next 2 articles since they will be focusing on setting up a web server manually. Here are the instructions to host your web application on Heroku:

Go to their website https://www.heroku.com/ and sign up then login. Go into your dashboard (https://dashboard.heroku.com/apps), select New in the top right and create a new app. Follow the instructions on the page to create your application (remember your application name).

Install Heroku CLI

sudo snap install --classic heroku

Make sure Git is installed

sudo apt install git

Go into your application directory and run the following and enter your credentials

Heroku login

Replace replaceme with your app name

git remote add heroku [email protected]:replaceme.git
git add .
git commit -m "First commit. "
git push
git push heroku master

You can visit your website on the Heroku dashboard by pressing on Open at in the top right, and it should open up your project’s page with a URL like https://yourapp.herokuapp.com

Heroku Commands

Every time you want to push to heroku and update your live site, you can just run the commands from “git add” to “git push heroku master. ” You can also tell Heroku to run certain commands on the server by typing something like

heroku rake db:create
heroku rake db:migrate
heroku rake db:seed

You can basically prepend “heroku” to your commands in the terminal then run your usual rails commands to tell it what to do. Running the above rake commands will be necessary most of the times to get your Heroku database up and ready to go.

Note:

Before using Git, remember to configure your username and name. When you run “git commit”, it should tell you to configure these with 2 commands if you haven’t already.

If you run into an error saying “Precompiling assets failed. “, you can try going in in config/application.rb, add/edit line to be

config.assets.initialize_on_precompile = false

If you run into “Could not detect rake tasks. “, you can try running

RAILS_ENV=production bundle exec rake assets:precompile

If all went smoothly, your website is now in Production and can be visited by anyone on the internet! If you now want a custom domain, you will have to enter your credit information with Heroku to verify your account. Then, you can buy a domain name from a company such as GoDaddy or Namecheap and point your domain (“CName” or “A record”) to your app’s URL and run

heroku domains:add www.yourwebsite.com

Remember to replace yourwebsite with your actual domain name. For more information on this, please follow their instructions here: https://devcenter.heroku.com/articles/custom-domains

If you chose the Heroku route, then you are done! (for now). The rest of this article will just go over AWS and how to set it up.

Amazon Web Services (AWS)

The AWS option is basically renting out a remote server from Amazon, and configuring it yourself to run a web application. We will be using EC2 and there is a Free tier for AWS that you can use for a year, there are also very cheap servers that can be around $3 a month.

Pros

  • There is a free trial.
  • Your server doesn’t deactivate when there is no activity.
  • Since you are configuring everything manually, you can find free alternatives for some software/services. For example, the SSL article later on will show you how to obtain an SSL certificate to make your website use https instead of http for free. You can also install your own free server monitoring tools or connect to your server conveniently to make changes.

Cons

  • Can require a bit more knowledge to configure web servers and can take longer than Heroku to set up. However, once you understand what is needed to configure this, you can also write a script to automate the process.
  • The free tier is only for a year.
  • Scaling might not be as straight forward as Heroku.

Other

  • You may need to register your credit card
  • Depending on your plan, you can get unexpected charges if you go over your limits
  • We won’t be using a graphical interface to connect to our server, so you should be comfortable with using the command line to interact with the machine.

Setting up an AWS server

Go to their website and sign up for AWS https://aws.amazon.com/ then login to their AWS console (you can just Google “aws console” if you can’t find the link on their website).

At the top, click on Services > EC2. Then click on Launch Instance. There will now be 7 steps here to launch your instance.

For the first step, you will need to select an AMI, which is basically a machine image that your machine will use. So in this case, we want to pick a Ubuntu 18.04 image. Search for it (maybe in the marketplace or community section) and select it. I selected the Ubuntu 18.04 LTS – Bionic option.

Select the tier that you want to use (there should be a Free tier if you need). For the rest of the steps until 6, you can just stick with the default options.

Security and SSH

In step 6, you will be configuring security settings. Check Create a new security group and name it. Below that, there should be a table with headers that say Type, Protocol, Port range, and Source. Make sure you create a rule here with Type SSH, TCP Protocol, Port 22, Source Anywhere or your IP. This rule would mean that you are allowed to ssh into your server from anywhere or your ip address (sshing into the machine just means that you can connect to the machine to run commands from your terminal as if it were your own local machine. This also means you will not have a graphical interface for our purposes). For more information on ssh, please refer to this article https://www.ssh.com/ssh/protocol/

Once you have set up this rule, click launch and it should bring up a window to create your key pair. Create a new keypair here, name it, and make sure you download it (there is no way to generate this again so don’t lose it). Once you click launch, your machine will start being setup and you can check on its status through the Instances menu option on the left. Once the Instance State says it is running, we can ssh into the server to start setting up our application.

Accessing the Machine

To ssh into this machine, open up your terminal and change directory to the folder that you saved your ssh key in then run (make your you replace your-key-name.pem with your own .pem file’s name)

chmod 400 your-key-name.pem

The above command “chmod” modifies the file’s permissions to allow to you perform the below command properly. For more information on the “chmod” command, take a look at https://isabelcastillo.com/linux-chmod-permissions-cheat-sheet to see a list of different permission combinations.

Now you can connect to the server by running

ssh -i your-key-name.pem [email protected]

Make sure to replace “your-key-name.pem” with the name of your own .pem file and also replace the IP address after the @ sign with your AWS machine’s. You can see this in the Instances tab as well. (it should be the Public IP or IPv4 Public IP column).

Note:

By default, AWS Ubuntu AMIs use the user called “ubuntu”, which is why we put [email protected] to connect to the server.

You should now be connected to your server properly. As you can see, this is just another Ubuntu server that you are able to use now!

Now if you want to point a custom domain to your website, you can buy a domain name from a provider such as GoDaddy or Namecheap and follow the instructions here: https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-to-ec2-instance.html

Congratulation, your AWS server is now up and running! Now our next few articles will go over how to set up these servers to be a proper Production environment for your application.

The next article can be found here. Previous article is here.