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.
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:
apt-get
.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).mod_rewrite
option for Apache as it's required for Yii 2 to deal with some URLs.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.
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).
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.
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:
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.
18.119.136.84