Fine-tuning the virtual machine

As specified in the Vagrantfile, we are going to have three provisioning scripts to be run in that order. Let's look at them in the same order.

In the code bundle provided with this book, these scripts are in the bootstrap directory exactly as shown in the preceding code example. In this folder, a bunch of other files also exists; these are various configuration files for different programs that Vagrant will place in appropriate places according to our bootstrap scripts.

Preparing the guest OS

We are using Ubuntu 12.04, which does not have PHP 5.4 in its installation repositories. On the other hand, we are in an environment where apt-get is available, so we can install absolutely anything we need relatively easily.

We will not provide the complete content of the 01-prepare-precise64.sh file as it's quite long and boring. You can look at it in the code provided with this book in the bootstrap folder. Here is what this script does in the order in which it appears here:

  1. Adds the special Personal Package Archives (PPA) with the latest PHP versions.
  2. Updates the package database of apt-get.
  3. Installs the latest Apache 2, PHP 5.4, vim, latest MySQL (both server and client), git (it's required to launch the composer inside the virtual machine), and the CURL package for PHP5 (it's required for almost anything related to PHP nowadays).
  4. Installs the X virtual framebuffer, Java runtime, and the Firefox browser so you will be able to run acceptance tests directly inside the Vagrant box.
  5. Removes the default virtual host defined for Apache.
  6. Enables the mod_rewrite option for Apache as it's required for Yii 2 to deal with some URLs.
  7. Makes Apache run under the Vagrant user account, so we'll get the application code base writable by the web server (no need for strict security in this setup).
  8. Does a small additional tweak to suppress the default warning about the server name not being defined.

Essentially, this script is for setting up the platform, not an application. However, as we will, for simplicity, use the root password to access the database, it will leak from the level next to this one.

Note

You should know about the VirtualBox Guest Additions plugin to comfortably work with Vagrant over VirtualBox. Vagrant relies on this plugin to perform the magic code base synchronization, and the catch is that it must be installed both on the host and the guest machines, and their versions must exactly match. Most probably, the VirtualBox Guest Additions plugin on your host machine will be newer than the one installed inside the guest box. As a result, Vagrant will complain and nothing good will happen. To resolve this problem, the Vagrant team has provided us with their own plugin called vagrant-vbguest. You need to call vagrant plugin install vagrant-vbguest, and after that at every call, vagrant up, so that this plugin checks the versions of Guest Additions and installs the correct version into the guest machine if necessary (it can take significant time, though).

Preparing the database and web server

The second level of provisioning is a bridge between a platform and an application. It is quite short:

# Separately specified settings for database
# NOTE that the password was already specified before in previous bootstrap script!
DB_USER=root
DB_PASS=mysqlroot
DB_NAME=crmapp

# Creating database
# NOTE the absence of the space between `-p` flag and the password!
mysql -u ${DB_USER} -p${DB_PASS} -e "create database if not exists `${DB_NAME}` default character set utf8";
mysql -u ${DB_USER} -p${DB_PASS} -e "create database if not exists ${DB_NAME}_test default character set utf8 default collate utf8_unicode_ci";

# Copy the prepared Apache config from codebase to the Apache config folder.
cp -f /vagrant/bootstrap/frontend.apache2.conf /etc/apache2/sites-enabled/
# Restart Apache so new virtual host will be published.
/etc/init.d/apache2 restart

All we do is create the databases for the application and the functional tests and copy the already-prepared Apache configuration from the bootstrap folder inside the code base to the place expected by Apache. This configuration holds the definition of a port-based virtual host, which is the reason why we removed the default configuration at the previous level.

Preparing the application

The last provisioning script is the most semantically complex. Here are its contents, at the final stage of the example application development, after Chapter 13, Collaborative Work:

# Go to the root project folder
cd /vagrant

# Install all prerequisites, including Yii
php composer.phar install --prefer-dist

# Copy the prepared config snippets to the configuration tree
cp bootstrap/local.php config/overrides/

# Copy the prepared config snippet for test database connection to the configuration tree
cp bootstrap/test.php config/

# Initialize the RBAC tables
./yii migrate --migrationPath='@yii/rbac/migrations' --interactive=0

# Initialize the database overall
./yii migrate --interactive=0

Here, we proceed in the following order:

  1. Install all dependencies managed by the composer, including Yii 2 itself.
  2. Copy the configuration for this local deploy target to the location expected by our application (see Chapter 13, Collaborative Work, for details).
  3. Run the built-in migration in Yii to set up tables for the database-backed RBAC manager (see Chapter 6, User Authorization and Access Control, for details).
  4. Run all of our migrations collected over the course of the book.

All three of these scripts are relatively harmless, and as a result, you can launch provisioning manually and safely without breaking anything. This is done by calling vagrant provision. It's useful to quickly reload the Apache configuration, for example.

..................Content has been hidden....................

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