How to deploy Ruby on Rails application on Google Compute Engine

NOTICE: 
To allow better navigation, all actions to be taken are marked with

Example:
Take this action

Introduction

This is basic set of instructions to deploy Ruby on Rails application on Google Compute Engine.
Google Compute Engine is IaaS (Infrastructure as a Service) - in other words, it's managed Virtual Machine, where you can install your software.

When setting up Google Compute Engine you can choose machine type, operating system and also you can preload some technology stack (including Ruby on Rails).

Step 1: Create Google Cloud Project

Login to your google account




Click on "Create Project"



 Step 2:  Use "Click to Deploy" to deploy with Ruby on Rails stack

Go to "Deploy&Manage -> Click to Deploy"

 

Select from the list "Ruby Stack" and click "Go"

Select your "Machine type" and click "Deploy Ruby Stack"

After this stage please write down your MySQL root password – we will need it later to change MySQL password to new one.


NOTICE:
At this point, cheapest machine type you can choose is n1-standard-1 (1 vCPU, 3.75 GB RAM). Estimated cost for this kind of machine is 25$/month.
You can change it later to f1-micro (vCPUs: shared, RAM 0.6 GB) which is cheaper ~4.60$/month.
For instructions please see Appendix chapter at the end of this article.


Your virtual machine will be preloaded with Ruby components:
  • Ruby on Rails 4.2.0 (Ruby version 2.1.1)
  • Apache HTTP Sever 2.2.22
  • Phusion Passenger 4.0
  • MySQL 5.5

You can get description of your installation directories here:


Click on IP address and allow HTTP/HTTPS traffic



Type IP address of your VM to verify that you can see sample welcome page. You should see page like this one:


Step 3: Login to your VM and push source code

 
Login to your VM using SSH in browser window:

      SSH console will be open in your browser's window:

If you have your repository on GitHub or any other publicly available Git server, you can just clone this repository to your VM.You can use sample RoR application (for simplicity, please clone it to your home directory - next sections assume this):

$ git clone https://github.com/maciej-arkit/rubyonrails_sample_blog

4. Create MySQL database and run db:migrate

Change MySQL password to reflect our application config – i.e. "test123"
$ mysqladmin -u root -p password

NOTICE
You need to provide your current root password (you should have it), or you can find it going to https://console.developers.google.com/project/${YOUR_APP}/compute/instances, and clicking on your instance.
Then plese find "MYSQL_ROOT_PASSWORD" string.

Create database named "blog_dev"
$ mysqladmin -u root -p create blog_dev 

Go to application directory (~/rubyonrails_sample_blog) and ensure Gemfile has following entries:
Gemfile
gem mysql
gem 'therubyracer', platforms: :ruby

Invoke "rake db:migrate" to create tables in database:
$ rake db:migrate

5. Configure apache

Go to /etc/apache2/sites-available and edit 'default' file:
$ sudo nano default


/etc/apache2/sites-available/default
<VirtualHost *:80>
    ServerAdmin webmaster@localhost 
    DocumentRoot /home/${YOUR_USER_NAME}/rubyonrails_sample_blog/public 
    RailsEnv development
    <Directory /home/${YOUR_USER_NAME}/rubyonrails_sample_blog/public>
        Options Indexes FollowSymLinks MultiViews 
        AllowOverride None 
        Order allow,deny 
        allow from all 
    </Directory> 

    ErrorLog ${APACHE_LOG_DIR}/error.log 

    # Possible values include: debug, info, notice, warn, error, crit, 
    # alert, emerg. 
    LogLevel warn 

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Restart apache to reload configuration:
$ sudo service apache2 restart

Open your browser, type IP address of you VM, and you should see sample blog application.

 

Logs

You can access Apache logs in /var/log/apache2/


Appendix: How to change instance type to f1-micro (~4.6$/month)

For pricing information, go to https://cloud.google.com/products/calculator.

In Google Cloud Console (https://console.developers.google.com/), open your porject and navigate to "Compute -> Compute Engine-> VM instances" [1].

Then click on your instance name [2], and uncheck option "Delete boot disk when instance is deleted" [3].




When option is unchecked, go back to "Compute -> Compute Engine-> VM instances" select your instance, and click "delete".


NOTICE:
You have to delete your instance before using it's disk with another one.


Now create new instance, and during creation select f1-micro as machine type, and also select your existing disk – your disk have to be in the same datacenter as your VM.



Komentarze

  1. I wonder what change in App Engine made it possible to deploy Rails stack ..

    OdpowiedzUsuń
    Odpowiedzi
    1. You can deploy pure RoR stack on Google Compute Engine VM (not on Google App Engine). To do it on Google App Engine you will need to use JRuby and deploy it like Java application.
      GoogleAppEngine is Platform as a Service, and to deliver it's scalability, underneath it uses Google Compute Engine VMs (IaaS).
      In this case we are using Compute Engine. Please see this diagram: http://goo.gl/dbRp4e

      Usuń

Prześlij komentarz

Popularne posty z tego bloga

How to Develop Full Stack Spring Boot Application on Google App Engine + Cloud SQL - Step by Step Guide

How to start developing Google App Engine applications