"Spree Commerce is the automated enterprise solution that manages all aspects of your back-end operations, so you can focus on serving customers and growing your business." -- quotation from spreecommerce.com.
A few years ago, online shopping started to grow very quickly. Nowadays we can buy almost everything without leaving our home.
In this article I will show how to deploy the Spree Commerce, which allows us to build online store in a fast and easy way.
As of this writing, the latest stable version of Spree is 2.1.4 and
of Rails is 4.0.2. You will also need
shelly gem for deploying the
$ gem install spree rails shelly Successfully installed spree-2.1.4 Successfully installed rails-4.0.2 Successfully installed shelly-0.4.21 3 gems installed
Spree also requires the ImageMagick library for operations such as creating product image thumbnails or auto resizing images. You can get the latest version for your operating system from the official website.
Creating new project
Creating a Ruby on Rails application
First you need to create a new Rails application by running
$ rails new my-spree-store $ cd my-spree-store
Adding Spree to application
jquery-rails gem in version
3.0.0, so you need to specify
it in your
Gemfile by replacing:
gem 'jquery-rails', '3.0.0'
Then you can init a spree project inside the application directory
$ spree install --auto-accept gemfile spree gemfile spree_gateway gemfile spree_auth_devise run bundle install from "." [WARNING] You are not setting Devise.secret_key within your application! You must set this in config/initializers/devise.rb. Here's an example: [...] Spree has been installed successfully. You're all ready to go! Enjoy!
--auto-accept option, we will not be asked about installing
the default authentication system, loading the data etc. You can omit
this option and check the output for more details.
Using Dotenv for storing sensitive data
As you can see in the output, we need to set
security reasons we should also change the method of storing Rails secret key.
One of the solutions is to use Dotenv
gem which allows us to create an
.env file for each environment. In these
files we can store variables and then use them in our application as environment
dotenv-rails gem to your
Gemfile and run
bundle install. Then create
.env.development file with the following content:
DEVISE_SECRET_KEY: your_devise_secret_key SECRET_KEY_BASE: your_secret_key_base
You can generate these values by running
The last step is to replace constant with environment variable in
config/initializers/secret_token.rb and add the
file with the following content:
Devise.secret_key = ENV['DEVISE_SECRET_KEY']
Using PostgreSQL as a database
sqlite3 as a default database engine. This is a sufficent
solution for small projects, but we should probably use a more advanced
tool if we are thinking about production.
sqlite3 gem with
pg in your
-# Use sqlite3 as the database for Active Record -gem 'sqlite3' +# Use PostreSQL as the database for Active Record +gem 'pg'
Then replace sample configuration in
config/database.yml with the following:
development: &default adapter: postgresql encoding: unicode database: pool: 5 username: password: test: <<: *default production: <<: *default
As you can see, the application will use environment variables for storing
database credentials. We will use the
.env files again. Add the following content
POSTGRESQL_DATABASE: your_postgresql_database # spree-example-app POSTGRESQL_USER: your_postgresql_user # postgres POSTGRESQL_PASSWORD: your_postgresql_password # postgres POSTGRESQL_HOST: your_postgresql_host # localhost
Finally you can initialize the database using
$ rake db:create $ rake db:migrate $ rake db:seed $ rake spree_sample:load
rake db:seed will prompt you for login and password for admin user.
You can just type
email@example.com as a login and
spree123 as a
password for test. Make sure that you added the
to the repository
If everything went okay, you can run the application in development:
$ rails server
Deploying to the cloud
First you need to create an account and login:
$ shelly login
Shelly Cloud is a git-based cloud hosting, so it assumes that your application have a git initialized.
It also needs to use
puma as an application server,
serve static assets and compile assets on request. Don't worry, to fill them
up you just need to add
shelly-dependencies gem to your
gem 'spree_auth_devise', :git => 'https://github.com/spree/spree_auth_devise.git', :branch => '2-1-stable' gem 'dotenv-rails' +gem 'shelly-dependencies'
More information about Shelly Cloud requirements can be found in documentation.
bundle install and add
Gemfile.lock to your git repository.
Creating new cloud
Create new cloud for your application by running
$ shelly add
As you can see the Cloudfile
appeared. It defines your cloud structure (number of application servers,
size of virtual servers, domains etc). You need to add it to your git repository
and push it to Shelly Cloud. By default
shelly add creates a
which points to our git server, so all you need to do is:
$ git push shelly master
Before starting the cloud we need to set environment variables for production.
Setup secret keys
environment variables, so we need to add these values on Shelly Cloud.
Create a configuration file
$ shelly config create .env.production
with the following lines:
DEVISE_SECRET_KEY: devise_secret_key_for_production SECRET_KEY_BASE: secret_key_base_for_production
There is no need to set PostgreSQL credentials, because they are already set.
The last thing is configuring persistent storage for products's images.
There is no need to use any external service like S3 (so no additional
costs). Shelly Cloud provides
local file storage
which is shared between all virtual servers. The only thing
that you have to do is to create
hook with the
set -e mkdir -p disk/spree ln -s ../disk/spree public/spree
Add this file to your git repository and push to
Now you can start your cloud by running
I created a github repository with this sample Spree application. Feel free to clone this repository as a starting point of a new project.
How can I load sample Spree data?
You can load sample Spree data by running
shelly rake 'spree_sample:load'
shelly rake 'db:seed' for basic seed (among others creating an admin
Can I import my existing database?
Yes, you can import your database dump using
shelly backup import command.
You should also take a look on our blog post about migrating database using
Can I have SSL on Shelly Cloud?
Yes. It is better to secure transmission between an application and the user if you are building an online store. More information about SSL support on Shelly Cloud can be found in documentation.
Can I use cache?
Shelly Cloud uses Varnish for all applications so the only thing that you
need to do is to set
Cache-Control header to
public. It can be a huge
boost for the application, therefore make sure to do it. Additionaly each
application's virtual server with deployed application has is's own, local
instance of Memcache server. Take a look at the documentation
for more details.