In this tutorial we'll show you how to deploy a Solidus eCommerce application to Heroku, the most famous cloud platform.
Here are our requirements:
- AWS S3 Account with access_key and secret_key
- Heroku account
- Installed and configured Heroku CLI
- Rails (version 5.0 was used to write this article)
- Git
Install and configure your eCommerce locally
First of all, like you did a thousand times, create your new Rails application:
rails new my_ecommerce --database=postgresql
cd my_ecommerce
Initialize your project and make your first commit in Git:
git init
git add .
git commit -m 'Initial project'
In order to turn your simple application into an eCommerce, you now need to install the Solidus gem. Simply add the following lines to your Gemfile:
gem 'solidus', '~> 2.1.0'
gem 'solidus_auth_devise'
gem 'aws-sdk', '< 2.0'
Note that we are also specifying the aws-sdk
gem; this is needed if we want to
use S3.
Edit your config/database.yml
and add the database configuration:
default: &default
adapter: postgresql
pool: 5
development:
<<: *default
database: my_ecommerce_dev
test:
<<: *default
database: my_ecommerce_test
Install and run migrations on Solidus:
bundle
bundle exec rake db:create
bundle exec rails g spree:install
bundle exec rails g solidus:auth:install
bundle exec rake railties:install:migrations
bundle exec rake db:migrate
Run bundle exec rails s
then go to
http://localhost:3000 and make sure you correctly
visualize the homepage.
Configure Heroku
Ok, let's start running some commands on Heroku. Make sure you are already
logged in Heroku with the heroku login
command. Then you can create your new
Heroku application with the following:
heroku apps:create my_ecommerce
This command creates a new application called my_ecommerce
on Heroku. A new
remote repository named heroku (pointing to the new application) will also be
added to your git config.
You can check this with:
git remote -v
Configure the production environment
Add the heroku-postgresql
add-on to your Heroku application:
heroku addons:create heroku-postgresql
After adding the add-on you can see the new configuration environment with the command:
heroku config
The above command should return something like this:
=== my_ecommerce Config Vars
DATABASE_URL: postgres://XXXX:yyyyy@ec2-1-2-3-4.compute.amazonaws.com:5432/zzzzz
The DATABASE_URL
variable stores the location of the database. You can now add
the following lines to your config/database.yml
under the production
environment settings:
production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
Saving files on S3
Heroku uses an Ephemeral filesystem, which means that it doesn't provide any sort of persistent storage for data. Your images would therefore be removed after each deploy. In order to bypass this limitation you'll need to use an external service (like Amazon S3) to store your images. Run the following commands to prepare the Heroku environment:
heroku config:add AWS_ACCESS_KEY_ID='YOUR_ACCESS_KEY'
heroku config:add AWS_SECRET_ACCESS_KEY='YOUR_SECRET_KEY'
heroku config:add S3_BUCKET_NAME='YOUR_BUCKET_NAME'
Add this configuration in config/initializers/spree.rb
:
if Rails.env.production?
attachment_config = {
s3_credentials: {
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
bucket: ENV['S3_BUCKET_NAME']
},
storage: :s3,
s3_headers: { 'Cache-Control' => 'max-age=31557600' },
s3_protocol: 'https',
bucket: ENV['S3_BUCKET_NAME'],
url: ':s3_domain_url',
styles: {
mini: '48x48>',
small: '100x100>',
product: '240x240>',
large: '600x600>'
},
path: '/:class/:id/:style/:basename.:extension',
default_url: 'noimage/:style.png',
default_style: 'product'
}
attachment_config.each do |key, value|
Spree::Image.attachment_definitions[:attachment][key.to_sym] = value
end
end
Deploy Solidus to Heroku
Now commit and push your code to the Heroku repository:
git add .
git commit -m 'Configure Heroku Environment'
git push heroku master
If the push
command runs without errors you'll see:
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/my_ecommerce.git
* [new branch] master -> master
Populate your remote database
Last but not least, you can now put your data in the database.
Heroku CLI helps you with the command heroku run
, that runs rake tasks
on the deployed application.
Migrate the database and run the seed data:
heroku run rake db:migrate
heroku run rake db:seed
The seed process creates the default admin user and the basic configuration needed to bootstrap your eCommerce and start selling on the Internet.
If you want to add sample data you can run:
heroku run rake spree_sample:load
Now you can open your browser and enjoy your Solidus eCommerce by visiting https://my_ecommerce.herokuapp.com or using the following Heroku CLI command:
heroku open
You can find the code used for this tutorial on this repo.