Montag, Oktober 31, 2011

PHP Project deployment via Capistrano under OS-X Lion with git

Installing rvm (ruby version manager):

First you should install the ruby version manager (rvm) for installing ruby 1.9.2.:

>bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

this will install rvm under in /Users/your_username/.rvm/
then copy the bash statement and move it into your users .profile or .bash_profile

>[[ -s "/Users/your_osx_username/.rvm/scripts/rvm" ]] && source "/Users/your_osx_username/.rvm/scripts/rvm"

next step is to restart your terminal or source the profile´s file:

>source .profile

or

>source .bash_profile

Installing ruby 1.9.2 via rvm:

Thats easy! Just type:

>rvm install 1.9.2

This will download and compile ruby 1.9.2. Now you have to tell rvm to use 1.9.2

>rvm --default 1.9.2

if you open a new konsole and run

>ruby -v

This will print out : ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.2.0]
or newer

Installing capistrano:

In the future we will deploy our project in different environments (aka.: test, staging,production...) So you need two gems: capistrano and capistrano-ext

> gem install capistrano capistrano-ext

Now your system is capable to deploy!

Setup your Project:

go into your project root and setup capistrano:

>cd my_project
my_project> capify .

this will create a File named: Capfile and a directory called config with deploy.rb in it.
deploy.rb contains:

my_project> cat config/deploy.rb

set :application, "set your application name here"
set :repository,  "set your repository location here"

set :scm, :subversion
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`

role :web, "your web-server here"                          # Your HTTP server, Apache/etc
role :app, "your app-server here"                          # This may be the same as your `Web` server
role :db,  "your primary db-server here", :primary => true # This is where Rails migrations will run
role :db,  "your slave db-server here"

# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts

# If you are using Passenger mod_rails uncomment this:
# namespace :deploy do
#   task :start do ; end
#   task :stop do ; end
#   task :restart, :roles => :app, :except => { :no_release => true } do
#     run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
#   end
# end
Set the name of your Project, (Capistrano will deploy the project under this name). Your repository, scm and server ip´s.
Addtionally we want to deploy via git as our current systems git user. So we have to forward our git agent and define the servers user name.
An example deploy.rb: (assuming my_project as Projectname, an example git repo and that your server is running a unix system with a user named administrator)
set :application, "my_project"
set :repository,  "git@github.com:example/my_project.git"

set :scm_user, :git
set :deploy_via, :remote_cache
set :scm, :git

set :deploy_to, '/home/administrator'
set :use_sudo, false
set :ssh_options, {:forward_agent => true}
set :branch, "master"

set :user, :administrator

role :web, "ipV4"
role :app, "ipV4"
role :db,  "ipV4", :primary => true


namespace :deploy do
  task :copy_database_yml do
    run "cp #{deploy_to}/shared/connections.php #{release_path}/app/config"
  end
end
after "deploy:finalize_update", "deploy:copy_database_yml"
So you have to put in:
  1. the project name
  2. the git repo
  3. and the ip´s of your application + webserver and your db ip
and maybe modify the servers username and change or disable the copy_database_yml task.
Finally, you have to set up your server, check its dependencies and just do deployment :)

Server Setup

you could check your deploy.rb like so:

my_project> cap deploy:check

If you will be asked about a password, you should setup ssh key login (e.G.: create a keyfile and append it to administrators authorized keys)
* executing `deploy:check'
  * executing "test -d /home/administrator/releases"
    servers: ["ipV4"]
    [ipV4] executing command
    command finished in 170ms
  * executing "test -w /home/administrator"
    servers: ["ipV4"]
    [ipV4] executing command
    command finished in 88ms
  * executing "test -w /home/administrator/releases"
    servers: ["ipV4"]
    [ipV4] executing command
    command finished in 89ms
  * executing "which git"
    servers: ["ipV4"]
    [ipV4] executing command
    command finished in 91ms
  * executing "test -w /home/administrator/shared"
    servers: ["ipV4"]
    [ipV4] executing command
    command finished in 89ms
The following dependencies failed. Please check them and try again:
--> `/home/administrator/releases' does not exist. Please run `cap deploy:setup'. (ipV4)
--> You do not have permissions to write to `/home/administrator/releases'. (ipV4)
--> `/home/administrator/shared' is not writable (ipV4)
So you have to create the directories by running capistranos setup script and activate the servers git ssh:

my_project> cap deploy:setup

Capistrano will create some directories, which are not used by php and you could modify caps deploy and setup tasks for it but, for now: You are able to deploy via capistrano :)

my_project> cap deploy

I will add some more php setup info later. (Especially for lithium, li3 PHP Projects)

Update:

If agent forwarding wont work on osx see:
http://www.schmidp.com/2009/06/23/enable-ssh-agent-key-forwarding-on-snow-leopard/

Keine Kommentare: