Blog

capistrano deployment setup for php projects

capistrano deployment setup for php projects

Do you know these annoying situations where you have a little old PHP project, which was developed years ago and needs little updates from time to time. You don't like to install a ftp-server on your virtual server or any ftp client on your development machine and how was the scp-command again? I had exactly these situations every month again and again and always was to lazy to setup a real deployment-set for such cases. But now I have to move a few of these old projects from an old server to a new one. I have already my setup for deploying rails-applications with capistrano on this new server.

  • installed git on the new server => check
  • installed php on the new server => check
  • created a deployment user => check
  • ssh setup => check
  • capistrano-setup for rails applications => check
  • "real" deployment setup for PHP projects => missing!!

So I decided to create a little deployment setup with a minimal configuration for my PHP projects. It is very simple, but I think it is very very useful - especially for lazy people :-).

1. Gemfile

Add a Gemfile to your project:

source 'https://rubygems.org'

group :deployment do
  gem 'capistrano'
  gem 'capistrano-bundler'
end

Install your bundle:

bundle install

2. Capfile

Add a Capfile with following code lines to your project root:

require 'capistrano/setup'
require 'capistrano/deploy'

3. Create your deploy directory

Open a new terminal and change into your project root directory and generate the folders and files:

mkdir -p config/deploy
cd config
touch deploy.rb
touch deploy/production.rb && touch deploy/staging.rb && touch deploy/your_environment.rb

4. Change deploy.rb to following

# config valid only for Capistrano 3.3.5
lock "3.3.5" # change the version to the capistrano version you are using - I used capistrano version 3.3.5

set :application, "your-application"
set :deploy_user, "your-ssh-deploy-user"

# setup repo details
set :repo_url, "git@example.com:your-application.git"
set :scm, :git

# how many old releases do we want to keep, not much
set :keep_releases, 2

5. Change your environment files to something like following

set :stage, :production # or :staging or :your_environment
set :branch, "master" # or another branch, for example "development"

# used in case we're deploying multiple versions of the same
# app side by side. Also provides quick sanity checks when looking
# at filepaths
set :full_app_name, "#{fetch(:application)}_#{fetch(:stage)}"

server 'your-ip-address', user: 'your-ssh-deploy-user', roles: %w{web app db}, primary: true

set :deploy_to, "/var/www/www.example.com"

# whether we're using ssl or not, used for building nginx
# config file
set :enable_ssl, false

role :app, %w{deploy@your-ip-address}
role :web, %w{deploy@your-ip-address}
role :db,  %w{deploy@your-ip-address}

And you are done. Simple push your changes via your version-control-system and you should be able to deploy any kind of project via the normal capistrano process:

bundle exec cap your_environment deploy

12.01.2015
Matthias Frick
PHP & MySQL, Ruby on Rails
2 Kommentare

Über den Autor

Matthias Frick
Matthias Frick, MSc.

Er ist ein langjähriger Ruby-on-Rails Entwickler und leitet das Unternehmen Frick-Web.

2 Kommentare zu "capistrano deployment setup for php projects"

  1. Pedro Rodriguez
    Pedro Rodriguez 18.02.2015
    Thank you for sharing! Very useful and simple, I like it! :-) All the best, P
  2. Lukas Wohlfahrt
    Lukas Wohlfahrt 13.04.2015
    Danke fürs Teilen!

Kommentar verfassen