Local PHP Development Environment Setup with Vagrant
Created by Linh M. Tran
Agenda
Introduction
The perfect development setup flow
Popular setup flows
Intro to Vagrant
Examples using Vagrant for PHP development
Conclusion
Introduction
As a developer, local development environment is literally where you live and what your life depend on!
But setting up it is often the more boring and tedius task that all of us, developers, want to avoid
Will Vagrant change this?
The perfect dev environment setup flow
- Easy and Fast
- Create an environment that's as close as possible to the production environment
- The result environment can be easily recreated and even shared between team member
- Not boring and should not make you hate your job
Some popular ways to setup dev environment
Manually Do Things
I want to do it the hard way
- Installs packages is often quite easy on Linux OS (Ubuntu, Fedor) and is a great experience to set all of them up
# Ubuntu example
$ sudo apt-get install mysql postgresql php5-fpm php5-mcrypt nginx
In MacOS X, Homebrew can be used to install packages (but configuration may not as easy as in Linux Os)
$ brew tap hombrew/php
$ brew install php56 php56-mcrypt composer nginx
And you really don't want to do this in Windows
Do it the hard way
Pros and Cons
Pros
- Work great when your main OS is a Linux one
- A great experience for the adventourous
Cons
- Hell experience for Windows users, not so fun one for Mac lover
- Have to manually do a lot of configuration even in a Linux OS
- Does not work so well with different projects and different requirements
Prepackaged Solutions
I just want to take it easy
Prepackaged Solutions
Pros
- The savior for Windows users (possibly some Mac users)
- Super easy to setup, even for non developers
- Default sane configuration, almost popular apps (wordpress, joomla,...) can run without further configuration
Prepackaged Solutions
Cons
- With non trivial applications, you will still have to manually do things (vhost, php.ini, ...)
-
Things may break when you need somethings that do not come prepackaged
- Just google setup memcached, redis, mongodb... with MAMP
- Or google "Setup boris with MAMP in Mac OSX" (shameless plug)
- Depends on the owners for new updates, fixes
- Too different from production environment
- Does not work so well with big projects with a lot different requirements
Virtual Machine
Almost got it right
Virtual Machine
Pros
- Cross platform, work well with a diverse team (Apple haters, Linux lovers, MS loyalist v..v..)
- Isolated environment, your precious machine will not be polluted with dozen kinds of packages
- Can simulate the production environment for every kind of projects
- VMs can be shared easily
Virtual Machine
Cons
- Performance is not so well for people with weak machine
- VMs may take a not so small amount of your hard disk space
- If install from scratch, normal Linux installation often comes with unnecessary overhead
- Still have to manually do a lot of thing
- Find the iso/VM image. Install/add to your VMs program
- Manually configure network, port forwarding, shared folder...
- Manually install packages, configure them
- ....
Intro To
Beware the coming of the Savior!
What is Vagrant
"Vagrant is a tool for building complete development environments. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases development/production parity, and makes the "works on my machine" excuse a relic of the past."
-- Vagrant Website
Technically, it's a wrapper around virutalization softwares (Virtualbox, VMWare...) and configuration tools like (Chef, Ansible, Puppet..) that help you manage and configure virtual environments automatically
A little taste of Vagrant
Vagrant Basics
Providers
-
They are simply type of virtual machines that Vagrant support
-
Currently, Vagrant default support VirtualBox, Docker, Hyper-V, VMWare (with paid plugin)
-
You can create a plugin to add a custom provider
Vagrant Basics
Box
- The package format for Vagrant Environment (.box file)
- A base box is necessary for Vagrant to build your environment
- To discover public box, go to public Vagrant box catalog
- Example working with box
$ vagrant box add ubuntu/trusty64 # Download the box from Vagrant public catalog
$ vagrant box add homestead/laravel homestead-0.2.1.box # Add local box
$ vagrant box list
laravel/homestead (virtualbox, 0.2.1)
ubuntu/trusty64 (virtualbox, 14.04)
$ vagrant box #See all box related command
Vagrant Basics
Vagrantfile
- Created by running the following command in your project folder
$ vagrant init
- Using Ruby syntax
- Describe the type of machine required for a project, and how to configure and provision these machines
- Provider, base box
- Network: private ip, port forwarding, synced folder...
- Provision method
-
Vagrantfile is per project and should be included in version control
Vagrant Basics
Provisioning
- The process to configure your Vagrant environment using defined configuration in Vagrantfile with the help of provisioners
- And provisioners are just configuration tools that Vagrant support (Chef, Puppet, Ansible... or even Shell (Bash Script))
Vagrant Basics
Basic Workflow
Vagrant Basics
Basic Workflow (cont.)
Vagrant Basics
Basic Workflow (cont.)
Examples using Vagrant for PHP development
Demo time
Using Laravel Homestead
Really thank you, Taylor Otwell
-
Just follow the docs and you will running a PHP development environment in no time
-
You can check ~/.composer/vendor/laravel/homestead/ folders to understand how it work
-
If you want to add your modification script, add it to ~/.homestead/after.sh
Using Bash Script to provision your Environment
Highly dangerous! You have been warned
- Use your bash script skill to configure and install your environment
- Example setup code in Vagrantfile
# Setup PostgreSQL
config.vm.provision 'shell' do |s|
pg_username = 'homestead'
pg_password = 'secret'
pg_db = 'homestead'
s.path = './provisioning/setup_postgres.sh'
s.args = [pg_username, pg_password, pg_db]
end
- Check my github repo for full code
Using Ansible to provision your Environment
Conclusion
Vagrant - Pros
- All of the advantages of virtual machine approach
- Require less system resources than virtual machine
- Automate almost everthing
- Really easy for developers in team to share and recreate the development environment
Conclusion
Vagrant - Cons
- To harness the full power of vagrant, there is a bit of learning required
- Still has some performance issues
- Sometimes, it's not always right to run everything like in a production environment
Grand Conclusion
Vagrant is a great tool and will make your life as a developer much more enjoyable. But all good things come with a cost. Be patient, keep learning and don't break anything when things do not work as expected :D