Whether you are working in multiple projects which requires complete isolation of data, codes and credentials or just merely wanted to separate your "personal" dev environment into your "work" dev environment, Vagrant is very useful in this process.
Usually, the first thing you are doing when you have a new pc/mac are these steps:
- Install WAMP/LAMP/MAMP/XAMP
- Setup any other PHP/Apache Extensions like cURL or Mcrypt
- install Composer
- install Git
- Setup other software (Memcached, Redis, Beanstalkd etc)
- Setup your Git Credentials like SSH keys etc
- Git clone your projects to your local machine
- Run the migrations to the database (or more often, import it manually to your database)
- Only then you can start doing the "real" work
Very simple process isn't it, but there are fundamental issues in that seemingly perfect work environment:
- Most likely, your production machines is *nix variant. When you are developing your projects, it is very important that you able to duplicate how the production server will behave to avoid OS-related issues down the line.
- What if you are working in a very sensitive project that you wanted to make the data isolated to a dedicated virtual machine
- For some reason, you have multiple Git account for multiple companies. I personally have 2 accounts - one personal account for my personal projects and the other for company projects). And you want to use different SSH key for each machine (since entering your github credentials every push sucks)
- You wanted to simulate a weird behavior of a project in its own isolated environment
- You wanted a portable dev environment (you dont want to go through on installing all the stuffs again when you had reformatted your hard drive or moved into another pc)
- You are working in a team and you wanted to make sure everyone is using the same setup as you ( prevents "works in my machine but not in yours" issues)
- You wanted to use *nix command line tools in your dev environment but still wanted to use your beloved IDE ( probably Sublime Text)
- You don't want to mess up your entire configuration when experimenting with something
- You want a fresh copy always of LAMP Stack
The list goes on and on and there is no reason for not having a portable, secure and conflict-free development environment. Before, we are doing it by either setting up any Virtual Box, VMware or VirtualPC, Wubi (Ubuntu) in our workstation, but the process is so tedious that most of the developers rather not do it, since installation of *nix alone, the LAMP stack among others is way too tedious and for some dev, it takes too long to setup and requires skills that is more of system administrator's stuffs for a simple purpose.
We are not System Administrators, we are developers, we are supposedly developing things, not setting up things...
But gone are those days... enter Puppet and Vagrant
Puppet is basically an System Administrator's toy, it lets someone to write automated scripts to automate tasks like installing and updating *nix packages, setting up cron, configuring apache, php etc.
Vagrant is basically a command that you can run in your command line to easily setup a new virtual machine, automatically installing everything you need to start your "real work", it handles the headache of setting up the Linux OS, installing apache, PHP, MySQL etc (with the help of Puppet scripts, though not required).
The killer combination of the two makes it very very easy and cheap to create a new work environment that you wont mind setting up seperate virtual machines per project!
Are you ready? let's get started on setting it up!
Ok, so let me give you the things you will need first:
- Install either VMWare or VirtualBox (recommended)
- install Vagrant
- Optionally obtain a copy of Putty
- PHP >= 5.3.* (for CLI)
- Composer
Once you got those stuffs installed, in your command line, run:
The command basically downloads a pre-made Ubuntu box (called base box), ready to be used at the time you had downloaded it. That will require sometime though. So please give it some patience (cmon, it downloads a whole virtual machine!).
Don't worry, you only need to do that only once though, once the base box is there, it will be used as a starting point of any virtual machine you will be creating. The original Ubuntu Box will not be changed when you are creating new VMs-- its always there waiting to be copied and reused.
Once the base box is downloaded, its time to create your LAMP machine!
This will download the repo which contains the necessary scripts, it will probably clone the files into puppet-lamp-stack folder, lets rename that to something more intuitive:
One important thing you need to do as well is, in vagrant, there is always a shared folder between your PC with the virtual machine, that is called vagrant folder. And what is that vagrant folder is mapped into you asked?
in that "lamp" folder you just had renamed!
This makes it possible to edit the files using your favorite IDE in windows, and its automatically reflected on the virtual machine.
Now, since this is a lamp stack, where is the www directory will be pointing into?
Nope you cant access the /var/www folder directly, it was not shared, that is why you need to create a folder that will be used as the document root of the apache. Its as simple as:
Whatever you will put into the webroot folder, that is the one will appear when you open up the http://localhost:8888
so placing a file in lamp/webroot/hello.php can be accessed by http://localhost:8888/hello.php
pretty neat eh!
But don't open up that url in your browser yet, you impatient bastard!, we haven't booted up the machine yet.
With the things already setup, now we are ready to boot our machine! are you ready?
That will probably output stuffs here and there, but at the end will say the box is ready.
place any script in the lamp/webroot/ folder then access it by visiting http://localhost:8888/yourfile
It now should work like a normal Ubuntu Box with Apache, MySQL and PHP installed w/o any effort in your end.
You can verify that by logging to the machine by executing
That will log you into the machine via ssh. You can now interact with the box like how you usually do in normal Ubuntu box. Here are the few things that you might always be using when interacting with the box:
If you want to use Putty as SSH client (or you wanted to connect to someone else's vagrant box across the network), instead of using the vagrant ssh command. You can do so by using localhost as host, 2222 as port, then "vagrant" as both username and password.
Ok, so that completes the LAMP part of the process. Now lets go with installing Composer and Git
Install Git
Install Composer
Now, you would probably wanted to associate this box into a particular Github account, so that you don't have to provide your github username and password everytime you push your commits. To do this:
While seems caching the user/pass is easier, I still recommend you use SSH key for authentication. This is important when you are trying to play with private repos
Now, you are ready to go! you have an isolated LAMP stack with Git and Composer setup!
Did you find this useful?
I'm always happy to help! You can show your support and appreciation by Buying me a coffee (I love coffee!).