Creating Laravel Dev Project [The Easy Way]

Creating Laravel Dev Project [The Easy Way]

Tuesday, 8th April

Now a days creating a development environment is not that hard, you can either install LAMP stack on your computer, or you can use one of the ready to use LAMP/MAMP/WAMP stack applications.But there is also another way by using Vagrant and Virtualbox. So what is Vagrant ?

Vagrant is an application which help us to create and configure lightweight, reproducible, and portable development environments. There are many ways which you can use to create your development environment using Vagrant and Virtualbox, but also there is a nice script which called Vaprobash (which was created by Chris Fidao) and the goal for this project as stated on its github page :

The goal of this project is to create easy to use bash scripts in order to provision a Vagrant server.
* This targets Ubuntu LTS releases, currently 12.04.*
* This project will give users various popular options such as LAMP, LEMP
* This project will attempt some modularity. For example, users might choose to install a Vim setup, or not.

Getting Started
First of all we need to make sure that we have downloaded and installed:
* Vagrant 1.4.3+ : http://www.vagrantup.com/downloads.html
* Virtualbox : https://www.virtualbox.org/wiki/Downloads

After we have everything installed, let;s create our project folder:

$ mkdir easyEnv
$ cd easyEnv

Then we need to grab the Vaprobash file from this short url which will always point to the master branch : http://bit.ly/vaprobash , we can use either Curl or Wget like this:

#curl

$ curl -L http://bit.ly/vaprobash > Vagrantfile
#wget
$ wget -O Vagrantfile http://bit.ly/vaprobash

Now if we list our directory content we can see this :

$ ls
Vagrantfile

Now all we need to do is to edit our 'Vagrantfile' and uncomment anything we need to have in our development environment - let's see:

(1) First lets change the default hostname on line `49` from `vaprobash.dev` to `tipsyandtumbler.dev`. You can call it whatever you like depending on your project name.

(2) Second i like to use nginx as my web server so lets uncomment the line `108` by just removing the hash (#) sign..

(3) Third we need to have mySql as our database backend, so we uncomment the line `116`.

(4) Fourth step is to have composer and Laravel installed for us, so we uncomment both lines '189' and '192'.

You can have fast review the Vagrantfile and see which option do you like to have in your environment, you are not limited to the options which i have listed up.

Once we are done we can now run the command:

$ vagrant up

Now vagrant will install a Virtual Machine image for us including all the softwares and dependencies which we have asked for (check the attached image).

Now when we finish we just point our browser to http://192.168.33.10.xip.io and you will have the Laravel welcome page.

Just note that this is a virtual image, which mean that you have to boot up the server each time you want to work with your development using the `vagrant up`, you can turn it off by executing `vagrant halt`, Vagrant has a list of options which you can see by executing `vagrant help`.

If you like to use your hostname which you defined in the first step, you can edit your host file and add it like this:

$ nano /etc/hosts
192.168.33.10 tipsyandtumbler.dev

The only problem here is that, the `hosts` files does not support wildcard sub-domains like ( *.tipsyandtumbler.dev ), and thats why we are going to install a small DNS server called `dnsmasq`.

Plus you have to edit `/etc/nginx/sites-avaliable/vagrant` to add your hostname to the server_name.

Installing dnsmasq On OSx

I will assume that you have installed brew on your computer, and you are familiar with it.

The same configuration applies somehow to linux, which you can use `apt-get` if you're on ubuntu to install it or `yum` on fedora.

I first tried to install it by my own, but to be honest i couldn't figure out how to handle it, but again thanks to Chris Fidao who introduce me to an installer bash script which i can use to install and configure it, but sadly it didn't work out of the box for me, so I tweak it and execute it manually, and this is what am going to explain here.

(1) Install dnsmasq using brew

$ brew install dnsmasq

(2) Create a link in your local bin directory to the dnsmasq executable file

$ ln -s cp /usr/local/Cellar/dnsmasq/sbin/dnsmasq /usr/local/bin/dnsmasq

(3) Once its installed you need to copy the configuration file to another directory like this :

$ cp /usr/local/Cellar/dnsmasq/2.68/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf

(4) Add your development TLD to the configuration file

$ echo "address=/.dev/192.168.33.10" >> /usr/local/etc/dnsmasq.conf

(5) Make sure that you have a resolver directory under etc directory

$ sudo mkdir /etc/resolver

(6) Create an empty file, the name must match your development TLD

$ sudo touch /etc/resolver/dev

(7) Add the name server to this file

$ sudo echo "nameserver 127.0.0.1" > /etc/resolver/dev

(8) Load `dnsmasq` automatically every time you reboot your computer

$ sudo cp -fv /usr/local/Cellar/dnsmasq/2.68/*.plist /Library/LaunchDaemons

$ sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
$ sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

(9) Remove the `tipsyandtumbler.dev` from your `hosts` file.

(10) Make sure your dns server is running:

$ sudo dnsmasq

And you're done, whenever you pointed your browser to `tipsyandtumbler.dev` you will get a respond from your vagrant virtual machine.