Magento Example

Josh J edited this page Jan 3, 2014 · 9 revisions

Deploying Magento

These examples are useful if your project will be hosted on a dedicated or virtual server, probably running Nginx. In the root directory of your project, run the following commands:

capify .
rm -Rf config/deploy.rb; mkdir -p config/deploy
touch config/deploy/staging.rb config/deploy/production.rb

After you have edited your Capfile and <environment>.rb files, you will need to run the following command only once:

cap <environment> deploy:setup

The above command will also create a few necessary files for the deployment process. Make sure you change any specifics and commit the following files before attempting a full deploy.

  • htaccess.dist
  • app/etc/local.xml.staging
  • app/etc/local.xml.production

Capfile

Edit your Capfile to look similar to this:

# Deploy Magento site using Capistrano
#
# Usage:
# cap <environment> deploy:setup
# cap <environment> deploy

# Required gems/libraries
require 'ash/magento'

# --------------------------------------------
# Server Variables/Defaults
#
#    Alternative Server(s) Configuration:
#      role :web, "domain.com"  # can also use an IP address or alternative domain name
#      role :db, "domain.com"   # can also use an IP address or alternative domain name
# --------------------------------------------
server "domain.com", :web, :db

set :user, "augash"
set :application, "domain.com"
set(:deploy_to) { "/var/www/sites/#{application}/#{stage}" }

# --------------------------------------------
# Git/SVN Variables
# 
#    Example SVN configuration:
#      set :repository, "https://svn.augustash.com/<PATH/TO/REPO>/trunk"
#      set :scm_username, "<SVN_USER>"
#
#    Example Git configuration:
#      set :repository, "git@github.com:augustash/<REPO_NAME>.git"
#      set :scm, "git"
#      #set :branch, "master" # define which branch 
#                             # should be used for deployments
# --------------------------------------------
set :repository, "git@github.com:augustash/<REPO_NAME>.git"
set :scm, "git"
# uncomment the line below if your project uses submodules (updates submodules)
#set :git_enable_submodules, 1

# --------------------------------------------
# Database/Backup Variables
# --------------------------------------------
set :keep_backups, 3 # only keep 3 backups (default is 10)
set :dbuser, "my_db_user"

# --------------------------------------------
# Compile Sass stylesheets and upload to servers
#
# ONLY RUNS IF YOU HAVE SET :compass_watched_dirs
#
# :compass_watched_dirs can either be a string or an array
#
# Example:
#   # Use the array syntax if you compass watch several directories:
#   set :compass_watched_dirs, ["skin/frontend/enterprise/ash", "skin/frontend/enterprise/my_custom_theme"]
#   # Use the string syntax if you only compass watch one directory:
#   set :compass_watched_dirs, "skin/frontend/enterprise/my_custom_theme"
#
# If you name your public stylesheets directory something
# other than "stylesheets" than you can specify the name of 
# the directory with the :stylesheets_dir_name variable
#
# Examples (both assume your compiled stylesheets exist within your :compass_watched_dirs):
#   # If your Sass compiles to your assets/css directory 
#   # (i.e., `skin/frontend/enterprise/my_custom_theme/assets/css`):
#   set :stylesheets_dir_name, "assets/css"
#   # If your Sass compiles to a separate directory 
#   # (i.e., `skin/frontend/enterprise/my_custom_theme/styles`):
#   set :stylesheets_dir_name, "styles"
# --------------------------------------------
set :skip_compass_compile, false # change to true if you want to skip all compass-related tasks
set :compass_watched_dirs, "skin/frontend/enterprise/my_custom_theme"

# --------------------------------------------
# NGINX/PHP-FPM configuration
#
# If your web server is using NGINX with PHP-FPM
# you can use the below variables and tasks to
# start/stop/restart/status nginx and php-fpm 
# processes. This is especially necessary if you
# are using something like APC to cache your code.
#
# If you are going use these commands it is highly
# suggested that you setup your SSH user to run
# common deploy-related commands (i.e., rm, chmod, 
# rsync, etc.) as sudo w/o
# requiring a password.
#
# For example, in your `sudoers` file:
# ```bash
# # ....stuff...
#
# # Command Aliases
# Cmnd_Alias DEPLOYMENT = /usr/bin/rsync, /bin/chmod, /bin/rm, /etc/init.d/nginx, /etc/init.d/php5-fpm
# # ...more stuff...
#
# # The COMMANDS section may have other options added to it.
# #
# # Allow root to run any commands anywhere
# root    ALL=(ALL)       ALL
# <SSH_USER>  ALL=NOPASSWD:   DEPLOYMENT
# ```
#
# ---------
# Options:
# ---------
# :nginx_init_command (Default: `/etc/init.d/nginx`)
#   The path to your nginx control script. You could also use 
#   `service nginx` but it's experimental at this point.
#
# :phpfpm_init_command (Default: `/etc/init.d/php5-fpm`)
#    The path to your php-fpm control script. You could also use 
#   `service php5-fpm` but it's experimental at this point. 
#
# --------------------------------------------
#after "deploy", "nginx:restart"

# --------------------------------------------
# Task chains
# --------------------------------------------
before "deploy:update_code", "backup"

Production Environment

Edit your config/deploy/production.rb file to look similar to this:

# Deploy Magento site using Capistrano
#
# Usage:
# cap production deploy:setup
# cap production deploy

# --------------------------------------------
# Server Variables (Overridden)
#
# If your production store is located on a different
# or multiple servers consider using this syntax:
#
#    role :web, "www2.domain.com"
#    role :web, "www3.domain.com"
#    role :db, "master.domain.com", :primary => true
#    role :db, "slave1.domain.com"
#    role :db, "slave2.domain.com"
# --------------------------------------------
server "anotherdomain.com", :web, :db

# --------------------------------------------
# Git/git-flow configuration
#
# if you are using Git or the git-flow workflow
# you should specify the :branch your environment
# deploys from. 
# 
# For example: 
#   + the staging environment should use the `develop` branch
#   + the production environment should use the `master` branch
# --------------------------------------------
set :branch, "master"

# --------------------------------------------
# Database/Backup Variables (Overridden)
#
# If your production database uses a different
# user you will need to define that here using
# this example:
#
#    set :dbuser, "my_production_db_user"
# --------------------------------------------
set :dbname, "my_production_database"

Staging Environment

Edit your config/deploy/staging.rb file to look similar to this:

# Deploy Magento site using Capistrano
#
# Usage:
# cap staging deploy:setup
# cap staging deploy

# --------------------------------------------
# Git/git-flow configuration
#
# if you are using Git or the git-flow workflow
# you should specify the :branch your environment
# deploys from. 
# 
# For example: 
#   + the staging environment should use the `develop` branch
#   + the production environment should use the `master` branch
# --------------------------------------------
set :branch, "develop"

# --------------------------------------------
# Database/Backup Variables (Overridden)
#
# If your staging database uses a different
# user you will need to define that here using
# this example:
#
#    set :dbuser, "my_staging_db_user"
# --------------------------------------------
set :dbname, "my_staging_database"