Compute Engine Google Cloud

Install Spree Commerce on Google Cloud with Ubuntu 18.04, Passenger and Nginx

Install Spree Commerce on Google Cloud with Ubuntu 18.04, Passenger and Nginx

In this guide, you are going to learn how to install and set up a leading Open Source Platform Spree Commerce built with Ruby on Rails in a production environment

Prerequisites

  1. For setting up Compute Engine, see the Setting up Compute Engine Instance.
  2. Domain name is pointed to your virtual machine.
  3. For setting up Cloud DNS, see the Setting up Google Cloud DNS for your domain.
  4. Google Cloud SQL Setup, see Setup Cloud SQL and connect with Compute Engine.
PlatformRAMSSDCost
Alibaba Cloud512 MB20 GB$2.50/mo
Cloudways – Managed Cloud1 GB25 GB$10/mo
Kinsta – Google Cloud Managed WP1 Website10 GB$30/mo

Install Ruby on Rails with RVM

SSH to your Compute Engine Instance and execute the following commands to install RVM. Replace username with your username

\curl -sSL https://get.rvm.io | bash
source /home/username/.rvm/scripts/rvm

Install Dependencies

rvm requirements

Wait for the dependencies installation to complete

Install Ruby

Once RVM and dependencies are installed successfully you can install Ruby with RVM.

rvm install ruby
rvm --default use ruby
ruby -v

Bundler is a tool that manages gem dependencies for all projects. Install the Bundler gem next as Rails depends on it.

gem install bundler

Install Nodejs, ImageMagick, MySQL Client, and some required packages

sudo apt-get install -y nodejs imagemagick libcurl4-openssl-dev mysql-client libmysqlclient-dev

Install Rails

gem install rails
rails -v

Now install MySQL adapter for Rails

gem install mysql2

Create a new Rails project

rails new myproject -d mysql
cd myproject

Edit the database configuration to connect to Cloud SQL

sudo nano config/database.yml

Replace the username with your username, password with your password, host with your Cloud SQL IP address

Make sure your database.yml looks like this

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: username
  password: password
  host: Cloud_SQL_IP_Address
  database: database_name

development: 
  <<: *default 

test: 
  <<: *default

production:
  <<: *default

Install Spree Commerce

Add Spree to your Gemfile

sudo nano Gemfile

Add the following to the last

gem 'therubyracer', platforms: :ruby
gem 'spree', '~> 3.6.4'
gem 'spree_auth_devise', '~> 3.3'
gem 'spree_gateway', '~> 3.3'
bundle install
rails g spree:install --user_class=Spree::User

Enter your admin credentials when prompted and complete the installation.

Install Passenger and Nginx

Once Spree Commerce is installed you can install Passenger and configure Nginx

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates
sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger bionic main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update
sudo apt-get install -y nginx-extras libnginx-mod-http-passenger
if [ ! -f /etc/nginx/modules-enabled/50-mod-http-passenger.conf ]; then sudo ln -s /usr/share/nginx/modules-available/mod-http-passenger.load /etc/nginx/modules-enabled/50-mod-http-passenger.conf ; fi
sudo ls /etc/nginx/conf.d/mod-http-passenger.conf
sudo service nginx start

Now Passenger and Nginx are installed successfully. You can visit your domain name in your web browser, you can see the Nginx welcome page.

Nginx Configurations for Spree Commerce

Now you need to update the Nginx configuration to point Passenger to the version of Ruby you are using.

sudo nano /etc/nginx/conf.d/mod-http-passenger.conf

Replace

passenger_ruby /usr/bin/passenger_free_ruby;

with

passenger_ruby /home/username/.rvm/wrappers/ruby-2.5.3/ruby;

Remove default Nginx configurations

sudo rm -rf /etc/nginx/sites-available/default
sudo rm -rf /etc/nginx/sites-enabled/default

Create a new configuration for your Ruby application

sudo nano /etc/nginx/sites-available/yourdomainname.com

Paste this new configuration setting and hit Ctrl+X followed by Y to save the file

server {
    listen 80 default_server;
    server_name yourdomainname.com;

    passenger_enabled on;
    passenger_app_env production;

    root /home/username/myproject/public;
}

Enable your new configuration

sudo ln -s /etc/nginx/sites-available/yourdomainname.com /etc/nginx/sites-enabled/

Validate Nginx configuration and restart your Nginx web server.

sudo nginx -t
sudo service nginx restart

Now Spree Commerce is installed and configured with Passenger and Nginx and connected with Cloud SQL on Google Cloud.

Visit your domain name on your browser, you can view the Spree Commerce homepage

Spree Commerce Homepage

Check your Spree Commerce admin page by entering the admin followed by your domain name (yourdomainname.com/admin)

Spree Commerce Admin page

Enjoy your installation of Spree Commerce on Google Cloud with Ubuntu 18.04 LTS, Passenger, Nginx and connected to Cloud SQL.

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.