Upgrade to Pro — share decks privately, control downloads, hide ads and more …

12 Factor Apps using Laravel and PHP

Barry
November 27, 2014

12 Factor Apps using Laravel and PHP

This is a talk I did for the Cape Town Laravel meetup group.

Barry

November 27, 2014
Tweet

Other Decks in Programming

Transcript

  1. Barry working at io as: developer (not so much anymore)

    devops (until I find someone else) and general manager of general things
  2. Why these 12 Factors? Declarative formats for short setup time

    Maximum portability between environments Minimise divergence to enable continuous deployments and... We can *SCALE* significantly
  3. One Repo == One App always a one to one

    correlation between the codebase and the app
  4. Many apps == a distributed system So then too if

    you have more than one repo you have more than one app
  5. But I need to share code ? Boris, you’re my

    brother. So think like it! Get somebody else to manage your code dependencies !
  6. Dependancy Isolation node.js Ruby PERL Python C PHP nodeenv bundle

    exec pl env / Carton Virtualenv Static Linking ?
  7. Dependency isolation in PHP / Laravel I’m not aware of

    any workable standard here https://github.com/phpenv/phpenv (only apache - not 12 factor) https://github.com/virtphp/virtphp (only php-cli - not 12 factor)
  8. Config Examples Credentials to cloud services such as file storage

    or mail delivery Resource handles to backing services like your database, caching server or message queue Specific per deploy information such as canonical names
  9. Use Environment Variables Easy to change across deploys with no

    code change They are language and OS agnostic
  10. Set env vars using deploy tools Set the var in

    bash for display purposes: export DB_URL=postgres://user:[email protected]:5432/db Some common container tools: heroku config:add DB_URL=$DB_URL deis config:set DB_URL=$DB_URL docker run -e DB_URL=$DB_URL
  11. Services such as: Datastores: mysql, orchestrate.io, mongodb, hadoop, cloudant, mantis

    Messaging/Queueing: RabbitMQ, Beanstalkd, IronMQ Binary Assets: CloudFiles, S3 SMTP: Postmark, Mailgun, Mandril Caching: Memcached, IronCache
  12. Build Stage Converts the code repo into an executable bundle

    called a build Fetches and vendors dependencies - composer Compiles binaries and assets - artisan commands or gulp/grunt
  13. Run Stage Runs the app in the execution environment for

    PHP in 12 factor it would use PHP-FPM
  14. Should be able to roll back Releases have unique IDs

    They are immutable - if you want to change you need a new release Release Management
  15. Runtime is automated When a server crashes or is rebooted

    and might happen in the middle of the night Keep this stage simple!
  16. Laravel 5 native support for cloud file systems Laravel 4:

    https://github. com/thephpleague/flysystem
  17. Sessions or not Save in attached storage or use JSON

    auth tokens instead Laravel has built in support for sessions in attached storage
  18. Memory or disk may be used briefly Apps may be

    rebuilt or re-started at whim So even single process apps are not to share
  19. For PHP things are a bit different built in `php

    artisan serve` is meant for development only
  20. PHP-FPM This *is* the language runtime We do need 2

    though: PHP-CLI for composer and artisan commands You will also need nginx in front of it
  21. Needs a routing layer Route public facing hostname to apps

    Deployment platform should handle this In dokuu for instance nginx is setup as the router
  22. Never Daemonize, No PID Rely on the OS process manager

    (upstart or systemd) to manage output streams, crashes and user initiated shutdowns
  23. Processes may handle their own internal multiplexing they may be

    threaded, they may have async event loops
  24. The tools gap Using tools like Vagrant / docker you

    now have the exact same invironments
  25. Use Adapters Database: Eloquent / PDO Queue: Laravel Drivers /

    3rd party Cache: Laravel Drivers / 3rd party
  26. Porting Backing Services Painless You *still* have to use the

    same backing services in all environments
  27. Homestead This is a pre-packaged vagrant box with all you

    need to start development Quick to get going but is most probably *not* the same as your staging / production environments
  28. No concern in routing or storage of output stream Write

    unbuffered stream to `stdout` The execution environment takes care of routing, collation and storage
  29. Run as one-off processes They must run in an identical

    environment as the long running processes
  30. Laravel - Use artisan `php artisan list` for built in

    commands You may also build custom