Showing posts with label Vagrant. Show all posts
Showing posts with label Vagrant. Show all posts

Wednesday, October 29, 2014

Packaging your Box in Vagrant

  • Set up your virtual machine
    • vagrant up
      
  • List your virtual machines
    • $ VBoxManage list vms
      "tesselact_default_1413389341047_169" {7ca66929-48d7-4e36-9237-011b5113c07b}
      "php-salt-box_default_1414574460667_45040" {a872c748-bf3f-46e1-9cd7-db7b7c1b302d}
      
  • Create the package
    • vagrant package --base php-salt-box_default_1414574460667_45040
      
  • You'll find your packaged box in a package.box file

Thursday, August 21, 2014

Set up a Symfony2 dev environment in 10 minutes with Vagrant

Let's keep it simple. It is just a quick draft how can you set up a Symfony2 development environment in a few minutes.

I'll use Vagrant (you'll need version 1.5 at least) and my dev box to create the desired environment.

The box main components are: Ubuntu14.04, Nginx1.6, Php5.5Fpm, Xdebug, Composer, MySql5.5, NodeJs0.10.30, Redis and it has PhpMyAdmin, Webgrind and RedisCommander.

So the steps are:
  • Create the Vagrantfile
    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    VAGRANTFILE_API_VERSION = "2"
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      config.vm.box = "adammbalogh/lemp"
    
      config.vm.network "private_network", ip: "192.168.33.10"
      config.vm.synced_folder "app/", "/var/www/app/"
    
      config.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--memory", "1512"]
        vb.customize ["modifyvm", :id, "--cpuexecutioncap", "85"]
      end
    end
    
    
  • mkdir app
  • vagrant up
  • vagrant ssh
  • cd /var/www/app
  • composer create-project symfony/framework-standard-edition . '~2.5'
  • nano app/AppKernel.php (and add these two methods)
    public function getCacheDir()
    {
        return '/tmp/symfony/cache/' . $this->environment;
    }
    
    public function getLogDir()
    {
        return '/tmp/symfony/log/' . $this->environment;
    }
    
  • nano web/app_dev.php (and delete these lines)
    if (isset($_SERVER['HTTP_CLIENT_IP'])
        || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
        || !(in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1',$
    ) {
        header('HTTP/1.0 403 Forbidden');
        exit('You are not allowed to access this file. Check '.basename(__FIL$
    }
    
  • app/console cache:clear --env=dev (to test it)

Thursday, August 7, 2014

Connect a Usb device through Vagrant

If you want to list your usb devices do this:
$ VBoxManage list usbhost
Host USB Devices:

<none>
On a pure VirtualBox setup it says nothing. Usb devices won't work in your virtual machine until you don't see something in the list. So, you'll need to do two things to fix this:

  • Install VirtualBox Extension Pack (from here)
  • You have to be (your currently logged in user) in the vboxusers group
    • sudo usermod -a -G vboxusers <username>
    • and you have to logout/login from your session!

When you have done with these steps, run the list usbhost again:
$ VBoxManage list usbhost
Host USB Devices:

UUID:               55d6661d-dd23-487c-8f97-23d4cd4f7378
VendorId:           0x064e (064E)
ProductId:          0x8125 (8125)
Revision:           40.16 (4016)
Port:               4
USB version/speed:  2/2
Manufacturer:       SuYin
Product:            Laptop_Integrated_Webcam_HD
Address:            sysfs:/sys/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.5//device:/dev/vboxusb/001/004
Current State:      Busy

UUID:               e96c6aef-f8a7-4f18-a67e-07776c7d839e
VendorId:           0x0bda (0BDA)
ProductId:          0x0129 (0129)
Revision:           57.96 (5796)
Port:               2
USB version/speed:  2/2
Manufacturer:       Generic
...

In Vagrantfile you have to enable the usb option and add a filter based on the desired Manufacturer and Product:
config.vm.provider "virtualbox" do |vb|
  ...
  vb.customize ["modifyvm", :id, "--usb", "on"]
  vb.customize ["modifyvm", :id, "--usbehci", "on"]
  vb.customize ["usbfilter", "add", "0", 
    "--target", :id, 
    "--name", "This is the identifier",
    "--manufacturer", "SuYin",
    "--product", "Laptop_Integrated_Webcam_HD"]
  ...
end  
You are all set.

Run vagrant up and ssh in your vm with vagrant ssh. In your vm run lsusb and you should see the connected usb device!

Monday, July 7, 2014

Web and Cli Debugging with PhpStorm, XDebug in Vagrant

We've a Vagrant box in a development environment and want to debug our application. Take a quick review of the two main ways, so it can be either a web app or a cli based app.


Web app debugging
First, we have to create a Server configuration:

File -> Settings -> Php -> Servers

Server configuration

Add a new server with its ip address (or hostname) and set the desired port. Check "Use path mappings" and map the vagrant's paths to the local ones.

The next step is creating a Run/Debug configuration:

Run -> Edit Configurations

Run/Debug configuration

Add a "PHP Web Application" config. Give it a name and choose the previously configured server from the select box.

To run our app in debug mode just go to:

Run -> Debug

and the web app will appear on a new tab in debug mode! If you want to restart the debug session again (after the actual one is disconnected) just refresh the page in the browser.

Cli app debugging:
At the start we should able to reach the host machine from our Vagrant box. So, how can we find out the host's ip?
"The IP address of the host is always the same IP address but with the final octet as a 1."
Okay, e.g.: if the vbox's ip is 192.168.33.10 we can ping 192.168.33.1 as our host machine from our guest.

We need the Server configurations as well (the same as the Web app debugging).

On the server side (in Vagrant) we've to create some environment variables:

export PHP_IDE_CONFIG="serverName=phpsaltbox"

export XDEBUG_CONFIG="remote_host=192.168.33.1 idekey=phpsaltbox"

serverName is the name what we typed in the Server configurations.
idekey comes from the xdebug config xdebug.idekey (it can be anything).

To run our cli app in debug mode we'll use the "Start Listen PHP Debug Connections" function in PhpStorm.

"Start Listen PHP Debug Connections" function


As the final step, just run your cli app in Vagrant and you'll see the debug window in PhpStorm in a couple of seconds!


References: