Spree Deployment Service


# on mac, preparation
brew install mysql
brew info mysql
# follow the prompt to set root user and start mysql

rails new spree-shop -d mysql
cd spree-shop
vim config/database.yml
# update database password
bundle exec rake db:create
bundle exec rake db:create:all
spree install --edge

# go to spree website and add deployment
# once deployment is set up, update Gemfile

# on local
bundle exec capify .

# on remote run the initialization configuration script from spree, and wait

# on remote
cd /home/spree
mkdir .ssh
vim .ssh/authorized_keys
# paste your local id_rsa.pub into authorized_keys
chown spree:spree .ssh
chmod 700 .ssh
chmod 600 .ssh/authorized_keys

# replace the content of config/deploy.rb with capistrano recipe from spree

# on local
git add .
git commit -am "capified"
git push

# on local
bundle exec cap deploy:check
bundle exec cap deploy:cold

# shop is up now, but no ssl
# on local
vim config/initializers/spree.rb
# in the Spree.config, add:
config.allow_ssl_in_production = false
bundle exec cap deploy

# now you can visit admin without ssl
# however admin user is not present on remote
# on remote
cd /data/spree/current
rake db:seed RAILS_ENV=production

Production Server with Rails and Phusion Passenger

First you need to install RVM, ruby, rubygem, rails, with LAMP already installed too.

rvmsudo passenger-install-apache2-module

# to find out where apache config file is
apachectl -V | grep SERVER_CONFIG_FILE

# load modules in Apache config (this may vary)
LoadModule passenger_module /home/vincent/.rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.7/ext/apache2/mod_passenger.so
PassengerRoot /home/vincent/.rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.7
PassengerRuby /home/vincent/.rvm/wrappers/ruby-1.9.2-p180/ruby

# now this is an exmple of apache virtualhost
<VirtualHost *:80>
        DocumentRoot /home/www-data/depot/public
        ServerName appname.myserver.com
        <Directory /home/www-data/depot/public>
                AllowOverride all
                Options -MultiViews

        # Speeds up spawn time tremendously -- if your app is compatible. 
        # RMagick seems to be incompatible with smart spawning
        RailsSpawnMethod smart

        # Keep the application instances alive longer. Default is 300 (seconds)
        PassengerPoolIdleTime 1000

        # Keep the spawners alive, which speeds up spawning a new Application
        # listener after a period of inactivity at the expense of memory.
        RailsAppSpawnerIdleTime 0

        # Additionally keep a copy of the Rails framework in memory. If you're 
        # using multiple apps on the same version of Rails, this will speed up
        # the creation of new RailsAppSpawners. This isn't necessary if you're
        # only running one or 2 applications, or if your applications use
        # different versions of Rails.
        RailsFrameworkSpawnerIdleTime 0

        # Just in case you're leaking memory, restart a listener 
        # after processing 5000 requests
        PassengerMaxRequests 5000

        # only check for restart.txt et al up to once every 5 seconds, 
        # instead of once per processed request
        PassengerStatThrottleRate 5

Create the database needed for the new app, update config/database.yml content to allow app to connect to database on the production server.

Once the new rails app is uploaded to the server:

# install/update bundle
cd appfolder/
bundle install

# now setup database table and data
cd appfolder/
rake db:setup RAILS_ENV=production

Next you use a2ensite and /etc/init.d/apache restart to reload the new site.

Git, GitHub and Ruby on Rails

Learning those things, will be posting snippets now and then…

# Git

# First-time system setup
git config --global user.name USERNAME
git config --global user.email EMAILADDRESS
git config --global core.editor "vim"

# First-time repository setup
git init
# Initialized empty Git repository in /home/vincent/Documents/aproject/.git/

# About .gitignore
# Pattern stored in this file will be ignored by git

# Comparison to Subversion
svn add *               git add .
svn status              git status
svn commit -m "msg"     git commit -m "msg"
svn log                 git log
svn checkout            git checkout -f (--force)
svn mv a.php b.php      git mv a.php b.pp

# Commit all (still need to "git add ." first for new files)
git commit -a -m "msg"

# Branch

# Create a new branch and switch to the new branch
git checkout -b BRANCHNAME

# Switch to an existing branch "master"
git checkout master

# List existing branches
git branch

# Merge a branch's modification into another branch
# Switch to the target branch
git branch master
# Merge into master
git merge master

# Delete branch
git branch -d TEMPBRANCH


# GitHub

# Set up GitHub account locally
git remote add origin git@github.com:digitalwingx/APPNAME.git

# Push local git repository to GitHuba
git push origin master


# Deployment to Heroku

# Install heroku gem
gem install heroku

# Sync local ssh key to heroku
heroku keys:add

# Create new application at Heroku (require git)
heroku create

# Push from git to heroku
git push heroku master

# Rename Heroku application name and url
heroku rename NEWNAME