Chapter 2. Setting Up the Environment

In this chapter, we will focus on the environment setup to be able to carry on with the next chapters. With this environment, you'll be able to put into practice all the examples provided in this book and try out everything.

By the end of this chapter, your computer will be ready to execute all the source code that we will develop in this book and will allow you to interact with the social network.

Using the provided virtual machine

Every computer is different, not only the hardware but also the software, operating system, user preferences, and so on. When you are the only developer in the team or you are developing solo, you usually only care about the differences between your machine and the server. This is completely different when you are in a team with more than one developer, because you share the code with people and you have to somehow ensure that your code executes exactly the same way as in the machine of another developer in the team.

A lot of techniques or approaches have been used to try to solve this issue and one of them is virtual machines (VM). A virtual machine is a simulation of a real machine using virtual components that mimics the computer architecture and allows you to run an operating system inside it. This also removes the issue of different operating systems across developers, such as Linux, Mac OS, or Windows.

This approach allows us to have the exact same copy of the machine and that means the same configuration, architecture, operating system, and software. Although we are using the same copy of the machine, anyone can make changes on the virtual machine, which would make our systems different again. To minimize this issue, we are going to use a tool called Vagrant.

Vagrant allows you to create and configure reproducible and portable virtual machines in an automated way. This tool relies on another piece of software called VirtualBox, which is basically the one that is going to give us the ability to virtualize our development environment and run the exact same copy.

Installing the required software

There are a few steps we should follow to get the environment up and running. For the installation instructions of the software, we will follow the guidelines on the websites, because if the software gets an update and the installation method changes, our instructions will be outdated. Following the ones found on the official website, we'll be assured that we have the latest instructions for the software that we are using.

The first step is to install VirtualBox. To do that, follow the instructions at https://www.virtualbox.org/wiki/Downloads.

Once VirtualBox is up and running, we can jump to the installing of Vagrant. We will again refer to the website for instructions on installation, which are available at http://docs.vagrantup.com/v1/docs/getting-started/index.html#install_vagrant.

The final piece of software we need to install is Git. You can find the installation instructions for your system at http://git-scm.com/downloads.

Getting a copy of the files

When you have VirtualBox, Vagrant, and Git installed on your system, we can jump to setup and configure the VM we are going to use. Let's see the following steps that are involved in the process:

  1. Create a new folder to store the code from the book, this can be in your Documents folder, home folder, or even on the Desktop.
  2. After that, open terminal if you're on Mac/Linux; open git bash if you're on Windows.
  3. Navigate to the folder you just created using the cd command.
  4. Get a copy of the Vagrant scripts by executing the following command:
    git clone https://github.com/christophervalles/ZF2-Vagrant.git 
    
  5. After getting a copy of the files, we need to execute a command inside the folder we just cloned. To do that, first enter that folder using the cd command. In my case, it looks like the following command:
    cd ZF2-Vagrant
    
  6. We need to initialize the sub modules by executing the following command:
    git submodule update --init --recursive
    
  7. After initializing the submodules, open the contents of the folder with your favorite text editor.
  8. We need to adapt the configuration values of Vagrantfile to suit your system. The instructions can be found in the following section.

Vagrantfile

This is an overview of all the sections inside Vagrantfile, which will help you understand every piece of configuration code and how to tweak it to suit your system.

The first section looks like the following code snippet:

config.vm.synced_folder "~/source-api", "/var/source-api"
config.vm.synced_folder "~/source-client", "/var/source-client"

These two lines allow us to share a folder between our host computer and the VirtualBox. This means that we will edit the source code on the host machine and it will be executed on the VM. The line is composed of the command itself and two parameters: the first one indicating the path to the folder we want to share on our host machine and the second one the path of the shared folder inside the VM.

We need to adapt the first parameter to point the VM to the right folder on our host machine. In my case, the API code is located at ~/source-api and the client code will be at ~/source-client. If you stored your code on the desktop or somewhere else, adjust these two lines to point to the right folders on your host machine.

The next section on the configuration file looks like the following code snippet:

config.vm.box = "ubuntu-12.04"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.network :private_network, ip: "192.168.56.2"

The first line specifies the name we want for the virtual machine; in our case, we don't need to change it.

The second line is a URL to download the base image; this is used by Vagrant to download the base operating system in case you don't have it. This parameter should not be changed.

The third line is the one you would probably want to tweak. In this line, we are defining the IP that the VM will have on our subnetwork. If you already have multiple VMs in the same subnetwork, you must adapt the given value to assign a new IP.

config.vm.provider :virtualbox do |vb|
  vb.customize ["modifyvm", :id, "--memory", "1024"]
end

These lines of code manage the amount of RAM memory we assign from our host to the VM. The default value is 1024 MB, but for our purpose, this can be changed to 512 MB or less; however, if you have plenty of RAM memory, just leave this value as it is.

VirtualBox has a few options for network adapters; the most common ones are Bridge, NAT, and host-only. In our setup, we will use NAT provided by default with Vagrant and host-only.

The NAT interface will connect the virtual machine to the Internet, using our computer as a router.

The host-only interface will connect our computer and the virtual machine using a subnetwork; this will allow us to have a static IP for the virtual machine without having to rely on our router and without consuming an IP of our main network.

Vagrantfile

Now we are going to take a look at the next section of code. The first line is to specify the folder where we have the recipes stored and the rest of the code is to add the recipes we want in the order we need them to be executed. In your case, you don't need to change anything.

Vagrant is using a piece of software called Chef to make the changes on the target computer, in our case a virtual machine. We use recipes to describe to Chef how we want certain configurations on the computer. Also, the recipes explain to Chef how the software gets installed on the system.

chef.cookbooks_path = "./cookbooks"

chef.add_recipe "apt"
chef.add_recipe "mysql::server"
chef.add_recipe "nginx"
chef.add_recipe "php-fpm"
chef.add_recipe "php::module_apc"
chef.add_recipe "php::module_curl"
chef.add_recipe "php::module_gd"
chef.add_recipe "php::module_mysql" 
chef.add_recipe "custom::db"
chef.add_recipe "custom::hosts"
chef.add_recipe "custom::vhosts"

The last piece of configuration is a JSON structure that will set up a few configuration variables for Chef. These variables will be accessed from the recipes and will allow us to inject some data from Vagrantfile to customize the recipes and the final output of the Chef execution. This can be illustrated from the following code snippet:

chef.json = {
  :mysql => {
    :server_root_password => "root",
    :server_repl_password => "repl",
    :server_debian_password => "debian"
  },
  :nginx => {
    :default_site_enabled => false
  }
}

As you can see, we are setting up a password for MYSQL users and also disabling the default virtualhost nginx file. Nothing needs to be changed on these two blocks of configuration for our setup.

VirtualBox subnet

In order to set up the host-only adapter, we should first set up a subnet between our computer and the virtual machine. The following is the step-by-step procedure, which shows how everything can be done:

  1. Open the VirtualBox preference panel and click on Network. Your panel may differ from the one shown in the following screenshot, but you should be able to find the network section pretty easily:
    VirtualBox subnet
  2. Click on the Add button to add a new subnet if the list is empty. Otherwise, jump to the next step.
    VirtualBox subnet
  3. Click on the Edit button to review if the settings are as we need them. Notice that the name of the network can differ, but that will not affect the system.
    VirtualBox subnet
  4. After clicking on the Edit button, a new section will appear showing an IP address and a network mask. Make sure that the values are similar to the ones shown on the following screenshot. Also, make sure that DHCP Server is unchecked by clicking on the DHCP Server tab.
    VirtualBox subnet
  5. Save all the changes.

ZF2 skeleton

The last step is to download the Zend Framework 2 Skeleton application from the official Zend GitHub and put it on the two folders we are sharing with the VM. Then, we should install the dependencies of the framework using the composer.

To download the code, you can clone it using git or just download a ZIP file. For this example, we will download the ZIP file from https://github.com/zendframework/ZendSkeletonApplication/archive/master.zip.

After downloading the ZIP file, we need to uncompress the file in the two folders we share with the VM; in my case these folders are located in my home folder at ~/source-api and ~/source-client. The folders should look like the following screenshot when uncompressed:

ZF2 skeleton

Notice that you need to put two ZF2 installations, one on each folder.

After that, we should install all the dependencies using the composer. To achieve that, we need to first launch the virtual machine.

Hosts file

The last change we have to do in our host machine is point the hostname of the virtual host to the actual IP. This is an easy step and can be accomplished in two different ways depending on whether you are working on *nix or Windows. For *nix systems, execute the following two command lines:

sudo echo '192.168.56.2 zf2-api' >> /etc/hosts
sudo echo '192.168.56.2 zf2-client' >> /etc/hosts

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

  1. If you are working on Windows, you should run the Notepad as Administrator. To do that, go to the Start menu, click on All Programs, then go to Accessories, and finally, right-click on Notepad. On the contextual menu, you should see an option called Run as Administrator as shown in the following screenshot:
    Hosts file
  2. Now that Notepad is open, you should go to File | Open and select the option All Files in the file type drop-down list.
    Hosts file
  3. After this, you should type the following path on the top bar to open the file: C:WindowsSystem32driversetc
  4. When you type the path, you should see a window like the following screenshot:
    Hosts file
  5. Now you should open the hosts file shown in the list.
  6. Once the file is open, you should manually add the following two code lines at the end:
    192.168.56.2 zf2-api
    192.168.56.2 zf2-client
  7. The file after the change will look like the following screenshot:
    Hosts file
  8. Now, save the file and you are ready for the next steps.

    Note

    When editing the hosts file, the IP you specify should match the IP of the virtual machine. If you have changed the IP on the Vagrantfile for any reason, make sure that the hosts file is pointing to the right IP.

Vagrant commands

We are now closer to having the environment ready to see the default project on our browser. We need to tell Vagrant to build the virtual machine based on the recipes and configurations we have and then install the dependencies of the framework. To do that, we have to open a terminal window or a command prompt depending on your operating system, and navigate to the folder where we have Vagrantfile. When we are there, we should run the following command:

vagrant up

After issuing the command, Vagrant will start building the virtual machine based on the configuration. If you don't get any errors in the build phase, you will have a VM up and running.

Note

In some machines with Windows, when issuing the vagrant up command, an error appears. If that happens, try to start the virtual machine from the VirtualBox GUI and the error will disappear. The most common one says VT-x features locked or unavailable in MSR. If that's the case, you will need to enable the virtualization features on the BIOS of your computer. To enable that, refer to the manual of your computer.

As a reference, take a look at the Vagrant commands you can find here as you will need to use them while working on the project.

Command

Description

vagrant up

This will create the virtual machine if doesn't exist. Otherwise, it will just start the virtual machine if it stops.

vagrant destroy

This will completely destroy the virtual machine. Sounds bad, but we can recreate it with the help of the recipes we have inside the cookbooks folder.

vagrant suspend

This command will suspend the virtual machine, will pause it so we can continue from the point we left it.

vagrant resume

This will resume the virtual machine from the point we left it after we issued the vagrant suspend command.

vagrant halt

This will completely stop the virtual machine and is similar to powering off the system.

Now that we have the virtual machine running, we should install the dependencies of ZF2. To do this, we need to enter the virtual machine. To accomplish that, we need to launch the git bash program or terminal, depending on your operating system. Navigate to the folder where we have Vagrantfile and execute the following command:

Vagrant ssh

This command will connect the git bash program or terminal to the virtual machine and allow us to execute commands inside the virtual machine.

When executed, you will see a new prompt with a line like the following code line:

vagrant@precise64:~$

This means we are now inside the virtual machine and we can proceed to install the dependencies. In order to do that, we need to execute the following commands:

cd /var/source-api
php composer.phar self-update
php composer.phar install
cd /var/source-client
php composer.phar self-update
php composer.phar install

If you followed all the steps and didn't get any nasty error message, you should be able to open the browser, go to http://zf2-client or http://zf2/api, and see something like the following screenshot:

Vagrant commands
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset