PFCongres 2012 - Rock Solid Deployment of PHP Apps

75
Pablo Godel @pgodel - www.pfcongres.com Sep 15th 2012 - Utrecht, NL https://joind.in/7079 Rock Solid Deployment of PHP Applications Saturday, September 15, 2012

Transcript of PFCongres 2012 - Rock Solid Deployment of PHP Apps

Page 1: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Pablo Godel @pgodel - www.pfcongres.comSep 15th 2012 - Utrecht, NL

https://joind.in/7079

Rock Solid Deploymentof PHP Applications

Saturday, September 15, 2012

Page 2: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Who Am I?

⁃ Born in Argentina, living in the US since 1999⁃ PHP & Symfony developer

⁃ Founder of the original PHP mailing list in spanish ⁃ Master of the parrilla

Saturday, September 15, 2012

Page 3: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Saturday, September 15, 2012

Page 4: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Saturday, September 15, 2012

Page 5: PFCongres 2012 - Rock Solid Deployment of PHP Apps

⁃ Founded ServerGrove Networks in 2005

⁃ Provider of web hosting specialized in PHP, Symfony, ZendFramework, MongoDB and others

⁃ Servers in USA and Europe!

ServerGrove!

Saturday, September 15, 2012

Page 6: PFCongres 2012 - Rock Solid Deployment of PHP Apps

⁃ Very active open source supporter through codecontributions and usergroups/conference sponsoring

Community is our teacher

Saturday, September 15, 2012

Page 7: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment

?Saturday, September 15, 2012

Page 8: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment

Software deployment is all of the activities that makea software system available for use.

http://en.wikipedia.org/wiki/Software_deployment

Saturday, September 15, 2012

Page 9: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment

A very important part of the application life-cycle

Saturday, September 15, 2012

Page 10: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment

A very important critical part of the application life-cycle

Saturday, September 15, 2012

Page 11: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment

It should not be an after thought

Saturday, September 15, 2012

Page 12: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment

It should be predictable

Saturday, September 15, 2012

Page 13: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment

The more you do it the better it goes

Saturday, September 15, 2012

Page 14: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Saturday, September 15, 2012

Page 15: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment: Goals

Saturday, September 15, 2012

Page 16: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment: Goals

One-click deploys

Saturday, September 15, 2012

Page 17: PFCongres 2012 - Rock Solid Deployment of PHP Apps

One-click deploysContinuous deploys

Deployment: Goals

Saturday, September 15, 2012

Page 18: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP AppsDeployment

Saturday, September 15, 2012

Page 19: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP AppsDeployment

Saturday, September 15, 2012

Page 20: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP AppsDeployment

Saturday, September 15, 2012

Page 21: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Anytime & Anywhere

Deployment: Goals

Saturday, September 15, 2012

Page 22: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Anyone

Deployment: Goals

Saturday, September 15, 2012

Page 23: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Reliable

Deployment: Goals

Saturday, September 15, 2012

Page 24: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Rollbacks

Deployment: Goals

Saturday, September 15, 2012

Page 25: PFCongres 2012 - Rock Solid Deployment of PHP Apps

No downtime

Deployment: Goals

Saturday, September 15, 2012

Page 26: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Reusable

Deployment: Goals

Saturday, September 15, 2012

Page 27: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Scalable

Deployment: Goals

Saturday, September 15, 2012

Page 28: PFCongres 2012 - Rock Solid Deployment of PHP Apps

• One-click / continuous deploys• Anytime & Anywhere• Anyone • No downtime• Predictable & Reliable• Rollbacks• Reusable• Scalable

Deployment: Goals

Saturday, September 15, 2012

Page 29: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment Facts

Saturday, September 15, 2012

Page 30: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment: Fact #1

Deployment starts with the developer

• Setup development environment to be as close as possible to productions servers

• Setup test/qa/staging servers• Use Vagrant to manage VMs• Use Puppet/Chef to manage OS packages/

configuration

Saturday, September 15, 2012

Page 31: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment: Fact #2

Success linked to server OS setup

• Use Puppet/Chef to manage OS packages/configuration

• Create OS packages for 3rd party software• Setup your own package repositories

Saturday, September 15, 2012

Page 32: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment: Fact #3

Monitoring is uptime

• Use monitoring tools to know what is going on with your servers (Ganglia, Cacti, Zabbix, etc.)

• Add monitoring and metrics to your app (Graphite, StatsD, New Relic)

• Use your logs wisely (Graylog, Logstash, Kibana)

Saturday, September 15, 2012

Page 33: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment Methodologies

Saturday, September 15, 2012

Page 34: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment Methodologies

• VIM-style• FTP uploads• rsync• source control (svn, git)• Build tools (ant, phing)• Specialized tools (capistrano, fabric, etc)• Package based (rpm, deb, etc)

Saturday, September 15, 2012

Page 35: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Web Apps Deployment: Steps overview

Saturday, September 15, 2012

Page 36: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Web Apps Deployment: First time

• Copy files to server(s)• Set server-side configurations• Load DB fixtures• Process and install assets• Warm up cache• “Enable” site

Saturday, September 15, 2012

Page 37: PFCongres 2012 - Rock Solid Deployment of PHP Apps

• Copy files to server(s)• Apply DB updates (migrations)• Process and install assets• Warm up cache• “Enable” site

Web Apps Deployment: Subsequent times

Saturday, September 15, 2012

Page 38: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment: Challenges

Saturday, September 15, 2012

Page 39: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment: Challenges

• rsync• git pull• setup git repo on local network to save

bandwidth and avoid issues if git server is down (i.e. github)

Challenge:Fast & reliable copy of files

Solutions:

Saturday, September 15, 2012

Page 40: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment: Challenges

• use a tool that allows to go from 1 to n servers easily (i.e. capistrano)

• pssh allows to send commands to n servers in parallel

• package your app in OS packages like .rpm/.deb to easily install across n servers

Challenge:Scalable

Solutions:

Saturday, September 15, 2012

Page 41: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment: Challenges

• test!• tag releases• dedicated branches (master for production)• deploy each release in its own directory

Challenge:Rollbacks

Solutions:

Saturday, September 15, 2012

Page 42: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment: Challenges

• use ssh based connections• donʼt store passwords on source control• store sensitive strings (passwords) in server

environment variables

Challenge:Secure

Solutions:

Saturday, September 15, 2012

Page 43: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment: Challenges

Challenge:DB migrations

Solutions:• Doctrine Migrations• Consider document oriented DBs like

MongoDB

“The best migrations are the ones you don’t have to do”

Saturday, September 15, 2012

Page 44: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment: Challenges

Challenge:Static assets

Solutions:• YUICompress shrinks JS and CSS file sizes• Enable web server compression• Add versioning to static assets links (code.js?v=1)• Assetic combines multiple files into one• Run utilities locally or in a staging server, deploy

resultSaturday, September 15, 2012

Page 45: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment: Challenges

Challenge:Caching

Solutions:• Update one server while others handle load• Group servers and update group at a time• execute commands on “finalize” to clear up APC

cache

Saturday, September 15, 2012

Page 46: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Deployment: Challenges

Challenge:File permission conflicts

Solutions:• Run Apache/PHP with same user• Use php-fpm instead of mod_php• Create “deploy” user and add web server to the

group• Use setfacl to give write access to multiple users

Saturday, September 15, 2012

Page 47: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Other common pitfalls

Saturday, September 15, 2012

Page 48: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Other common pitfalls

• Case sensitive filesystems• Configuration differences• Outdated 3rd party software• Github down

$ git daemon --base-path=/git/repo/path/ --export-all

$ git clone git://127.0.0.1/repo

http://ozmm.org/posts/when_github_goes_down.htmlSaturday, September 15, 2012

Page 49: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Examples

Saturday, September 15, 2012

Page 50: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Examples

Simplest continuous deployment ever!

<?php

exec(ʻ/usr/bin/env -i HOME=/var/www git pullʼ);echo “All done!”;

hook.php

screenshot

Saturday, September 15, 2012

Page 51: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Examples

Capistrano

• Ruby based• Very extensible• Large number of extensions• Simple client side installation

$ gem install capistrano

Saturday, September 15, 2012

Page 52: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Examples

Capistrano

set :application, "myapp" # Application nameset :deploy_to, "/var/www/myapp"

set :user, "deployer"set :use_sudo, false # sudo isn't required

set :deploy_via, :remote_cache set :repository, "[email protected]:user/repo.git"

role :web, "server.example.com", “server2.example.com”

Saturday, September 15, 2012

Page 53: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Examples

Capistrano

$ cap deploy$ cap deploy:migrations$ cap deploy:rollback

Saturday, September 15, 2012

Page 54: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Examples

Capifony (Symfony2)

• Extension of Capistrano• Implements lots of needed features

for Symfony• Great documentation

Saturday, September 15, 2012

Page 55: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Examples

Capifony (Symfony2)

$ capifony .

Saturday, September 15, 2012

Page 56: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Examples

Capifony (Symfony2)set :keep_releases, 3

# directories that will be shared between all deploymentsset :shared_children, [ app_path + "/logs", web_path + "/uploads"]

set :update_vendors, true# set :use_composer, true

set :dump_assetic_assets, true

Saturday, September 15, 2012

Page 57: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Examples

Capifony (Symfony2)

$ cap deploy:setup

Saturday, September 15, 2012

Page 58: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Examples

Capifony (Symfony2)

|-- httpdocs| `-- index.php|-- releases`-- shared |-- logs `-- uploads

5 directories, 1 file

Saturday, September 15, 2012

Page 59: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Examples

$ cap deploy$ cap deploy:migrations$ cap deploy:rollback

Capifony (Symfony2)

Saturday, September 15, 2012

Page 60: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment:Other options

• Fabric• WePloy• Phing• Magallanes• Jenkins

Saturday, September 15, 2012

Page 61: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Tools

Saturday, September 15, 2012

Page 62: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Tools

App Metrics: StatsD & Graphite

Saturday, September 15, 2012

Page 63: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Tools

Logging: LogstashShip logs from any source, parse them, get the right timestamp, index them, and search them

Saturday, September 15, 2012

Page 64: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Tools

Logging: Logstash

Configure Apache to log jsonLogFormat "{ \"@timestamp\": \"%{%Y-%m-%dT%H:%M:%S%z}t\", \"@fields\": { \"client\": \"%a\", \"duration_usec\": %D, \"status\": %s, \"request\": \"%U%q\", \"method\": \"%m\", \"referrer\": \"%{Referer}i\" } }" logstash_json

# Write our 'logstash_json' logs to logs/access_json.logCustomLog logs/access_json.log logstash_json

{ "@timestamp": "2012-08-22T14:35:19-0700", "client": "127.0.0.1", "duration_usec": 532, "status": 404, "request": "/favicon.ico", "method": "GET", "referrer": "-" }

Result

Saturday, September 15, 2012

Page 65: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Tools

Logging: Graylog

Saturday, September 15, 2012

Page 66: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Tools

Logging: KibanaKibana is a user friendly way to view, search and visualize your log data

Saturday, September 15, 2012

Page 67: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Tools

Packaging: fpm

https://github.com/jordansissel/fpm

Build packages for multiple platforms (deb, rpm, etc) with great ease and sanity.

fpm -s dir -t rpm -n "myapp" -v 1.0 /var/www/myapp

fpm -s dir -t deb -a all -n myapp -v 1.0 /etc/apache2/conf.d/my.conf /var/www/myapp

Saturday, September 15, 2012

Page 68: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Time for demo

Saturday, September 15, 2012

Page 69: PFCongres 2012 - Rock Solid Deployment of PHP Apps

PHP Apps Deployment: Summary

Saturday, September 15, 2012

Page 70: PFCongres 2012 - Rock Solid Deployment of PHP Apps

•Stop using FTP

Saturday, September 15, 2012

Page 71: PFCongres 2012 - Rock Solid Deployment of PHP Apps

•Stop using FTP•Plan early

Saturday, September 15, 2012

Page 72: PFCongres 2012 - Rock Solid Deployment of PHP Apps

•Stop using FTP•Plan early•Practice

Saturday, September 15, 2012

Page 73: PFCongres 2012 - Rock Solid Deployment of PHP Apps

•Stop using FTP•Plan early•Practice•Monitor

Saturday, September 15, 2012

Page 74: PFCongres 2012 - Rock Solid Deployment of PHP Apps

•Stop using FTP•Plan early•Practice•Monitor•AUTOMATE!

Saturday, September 15, 2012

Page 75: PFCongres 2012 - Rock Solid Deployment of PHP Apps

Thank you!QUESTIONS?

Rate Me Please! https://joind.in/7079Slides: http://slideshare.net/pgodel

Twitter: @pgodelE-mail: [email protected]

Saturday, September 15, 2012