Welcome Guest, Not a member yet? Register   Sign In
[Tutorial] Codeigniter on Vagrant
#1

Welcome to my guide on how to setup and configure Vagrant with CodeIgniter


First of all, what is Vagrant?

Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility of you and your team.

To achieve its magic, Vagrant stands on the shoulders of giants. Machines are provisioned on top of VirtualBox, VMware, AWS, or any other provider. Then, industry-standard provisioning tools such as shell scripts, Chef, or Puppet, can be used to automatically install and configure software on the machine.

How do i setup Vagrant?

The procedure to install vagrant is very simple. In this tutorial we are going to see how to setup the box and adding the correct environment variables to your Windows machine.


Installation

Installing Vagrant itself

Download the installer from the official website
In my case i need the windows version since im using windows 8.1

After the installation is completed restart your computer (required).

Now we need to check if Vagrant is added to our ENVIROMENT PATH. In order to check this open the CMD or the powershell and hit 'vagrant'


You should see something like this. If not check the troubleshooting section below.

Now the vagrant core is installed but we need something to virtualize our box. In this case we're using VirtualBox. It's a free solution provided by Oracle and its mostly sucks but what you see is what you get. The other solution is to buy VMWare (the leading solution in virtualaization) but it costs 79$ for each licence.

Installing VirtualBox

Download the installation file from the website and install it (troubleshooting below).

After the installation is completed make sure that 'vboxmanage' is accessible through the command line. 



If it's not you have to manually add it.

Editing the 'ENVIROMENT VARIABLES'

Windows are using something called Enviroment Path. When you include something on this path it can be accessed from the command line.
This is critical for vagrant in order to start the VirtualBox service.
Open the 'System properties' and click 'Advanced'. At the bottom of the window click locate the 'Enviroment Variables' button and click it.
Find the 'Path' variable, select it and click Edit. At the end of the line insert a semicolon and the complete path from your VirtualBox installation. It should look like this: ';D:\Program Files\Oracle\VirtualBox\'
In order to check if everything is working open the CMD and hit 'vboxmanage'. You should see something like this:


Creating a Vagrant box

First of all you need to create the home folder for the box. In my case the folder is called just 'Vagrant'. Now open a new cmd window and navigate in to your new folder. Once you're in hit 'vagrant init'. This initializes the box. You shouls see 2 new files!
Open the 'Vagrantfile' with your editor of choice (Sublime Text 3) and set the syntax to Ruby. This is actually Ruby code but you dont really need to know how to write Ruby in order to make this work.

Delete everything inside the file! Start fresh.

On the first line lets create a variable with the Config syntax version. The only option is 2. Dont touch it.


Code:
VAGRANTFILE_API_VERSION = "2"


Now lets make an object. Do:


Code:
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

end


Inside the object we need to set few parameters.

Lets start with the name:


Code:
config.vm.box = "codeigniter"


Its time to get the OS aka the .box file. Usually you pull it from vagrantbox.es. Im using ubuntu 14 64 bits without LEMP stack.
config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
This file will be downloader automaticaly upon the first boot.

Synced folders! The most important part. vagrant gives you the ability to set synced folders. This is how you edit the files from your computer.
In my case i'm having 3 folders. One is the app, the second is nginx's config folder and the third is the log folder.

Code:
config.vm.synced_folder "./app", "/var/www/", create:true
config.vm.synced_folder "./nginx", "/etc/nginx/sites-available/", create:true
config.vm.synced_folder "./nlog", "/var/log/nginx/", create:true

The syntax goes like this: config.vm.synced_folder "<the local folder>", "<server-side folder>", create:true

How do you access your awesome box?
For this step we need to add networking.
config.vm.network :private_network, ip: "192.168.66.66"
Feel free to use any ip you like. If the box is unreachable check your DHCP settings.

Configuring the specs.

Code:
config.vm.provider "virtualbox" do |vb|
 vb.memory = 1024
 vb.cpus = 1
end

This step is pretty self explenatory.

The final result should look like this:


Code:
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
 #The name of the box
 config.vm.box = "codeigniter"
 #Box file from vagrantbox.es
 config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
 #Synced foler
 config.vm.synced_folder "./app", "/var/www/", create:true
 config.vm.synced_folder "./nginx", "/etc/nginx/sites-available/", create:true
 config.vm.synced_folder "./nlog", "/var/log/nginx/", create:true
 #Box ip
 config.vm.network :private_network, ip: "192.168.66.66"
 #Box setup script
 config.vm.provision :shell, :path => "setup.sh"
 #Box config
 config.vm.provider "virtualbox" do |vb|
 vb.memory = 1024
 vb.cpus = 1
 end
end



Congrats! Go ahead and open a CMD on the same folder and hit "vagrant up" and wait for the magic to happen. It might take a while to download the box file.

Few more tweaks:
Configure codeigniter correctly
Use any webserver you want
Change the www directory if you need

After the download and the initialization is completed type "vagrant ssh" in order to ssh in to the machine.


Yes but why?
At this point you might wonder why whould you want vagrant?
In order to see where Vagrant shines lets learn about the awesome trics Vagrant does.

1) Realtime shareing without ports and ip.
Create a new account over at vagrantcloud.com and then type "vagrant login" inside the CMD (or terminal. its pretty much the same across all platforms)
Enter your credentials and hit vagrant share while the box is active (on)
A unique link from the Vagrant cloud will apear. You can share it with anyone.


2) PACKAGE
Once your box is ready you can pack it in to .box file with the "vagrant package" command.

Usefull commands
  • vagrant init - Initialize the box
  • vagrant up - Boot the virtual machine
  • vagrant halt - Shutdown the machine
  • vagrant login - Connect your cloud account
  • vagrant share - Create a public link
  • vagrant package - Pack your box
More info can be found here: https://docs.vagrantup.com/v2/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB