Book Image

Creating Development Environments with Vagrant

By : Michael Peacock
Book Image

Creating Development Environments with Vagrant

By: Michael Peacock

Overview of this book

<p>Vagrant is a tool for building complete development environments. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases development/production parity, and makes the "it works on my machine" excuse a thing of the past.</p> <p>"Creating Development Environments with Vagrant" is a practical, hands-on guide that walks you through the functionality of Vagrant, Puppet, and Chef to create powerful and flexible virtual development environments. Create your own virtualization environments containing configurations for different projects so that you can simulate complicated environments that can be easily shared with colleagues to get your projects up and running quickly and effortlessly.</p> <p>"Creating Development Environments with Vagrant" starts with an introduction to virtualization and the concepts behind it, why it’s useful, and an overview of the architecture of Vagrant. We will learn to install Vagrant and get to know its prerequisites. Covering provisioning scripts with Puppet and Chef, learning to use them independently as well as with Vagrant to create a powerful combination.</p> <p>If you want to locally test your projects, juggle multiple projects running on different versions of software, easily share technology requirement changes with colleagues, and, most importantly, want to perform all these tasks efficiently, then this book is for you.</p> <p>"Creating Development Environments with Vagrant" will take you from a virtualization novice to running all of your projects across your team in robust, isolated virtual development environments.</p>
Table of Contents (15 chapters)
Creating Development Environments with Vagrant
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Creating the Puppet manifests


Let's start by creating some folders for our Puppet modules and manifests by executing the following commands:

mkdir provision
cd provision
mkdir modules
mkdir manifests

For each of the modules we want to create, we need to create a folder within the provision/modules folder for the module. Within this folder, we need to create a manifests folder, and within this our Puppet manifest file, init.pp. Structurally, this looks something, as follows:

|-- provision
|   |-- manifests
|   |    -- init.pp
|    -- modules
 -- Vagrantfile

Installing Apache

Let's look at what is involved in installing Apache (this would be in the file, provision/modules/apache/init.pp). First, we need to ensure the Apache2 package is installed:

class apache {

  package {"apache2":
    ensure => present
  }

Tip

Note that we have not closed the curly bracket for the apache class. That is because this is just the first snippet of the file; we will close it at the end.

Next, we should, within the default Apache web root (/var/www), create a symlink which points a src folder to our projects src folder (this is within the shared folder that Vagrant automatically creates). This needs to be done once Apache is installed (to ensure the /var/www folder is present), as follows:

  file { '/var/www/src':
      ensure => 'link',
      target => '/vagrant/src',
      require => Package['apache2']
  }

Because we want to change our default Apache configuration file, we should update the contents of the Apache configuration file with one of our own (this will need to be placed in the provision/modules/apache/files folder in a file called default).

  file { '/etc/apache2/sites-available/default':
    source => 'puppet:///modules/apache/default',
    owner => 'root',
    group => 'root',
    require => Package['apache2']
  }

Because we want to reload Apache once some of the PHP packages have been installed, we can tell the Apache service to subscribe to these packages. Once they are installed, Apache will restart. The following code will subscribe the Apache service to these other packages and trigger the restart when they are installed:

  service { "apache2":
    require => Package["apache2"],
    subscribe => [File['/etc/apache2/sites-available/default'],
      Package['php5', 'php5-mysql', 'php5-dev', 'php5-curl',
        'php5-gd', 'php5-imagick', 'php5-mcrypt', 'php5-memcache',
          'php5-mhash', 'php5-pspell', 'php5-snmp', 'php5-xmlrpc',
            'php5-xsl', 'php-pear', 'libapache2-mod-php5']]
  }

We might also want to support file uploads within our project, so let's create an uploads folder which is owned by the Apache user (www-data) and can be written to (chmod: 0777):

  file{ "/var/www/uploads":
    ensure => "directory",
    owner  => "www-data",
    group  => "www-data",
    mode   => 777,
    require => Package['apache2']
  }

}

Two changes we need to make to the Apache configuration are as follows:

  • Create an alias folder which points to our uploads folder (we need to create an alias, because this folder is outside of the web project files)

  • Set the document root to the symlink we created earlier

    This is the file we need to call default and save within the provision/modules/apache2/files folder:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        Alias /uploads /var/www/uploads

        DocumentRoot /var/www/src
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

Enable an Apache rewrite module

The Puppet module we need to create to enable the rewrite module is fairly basic; we simply need to run a command, which installs the module. The code requires that we already have Apache installed:

class modrewrite{

  exec { 'enabledmodrewrite':
    command => '/usr/sbin/a2enmod rewrite',
    require => Package['apache2']
  }

}

Tip

Notice that here we close the apache class. Also, the mod rewrite command is specific to our operating system; it may vary if you are using a different distribution.

Installing MySQL

Installing MySQL is also fairly straightforward, we just want to install a few related packages. The following code should be placed in the file provision/modules/mysql/init.pp:

class mysql {

  package { "mysql-server":
    ensure => present
  }

  package { "mysql-client":
    ensure => present
  }

  package { "libmysqlclient15-dev":
    ensure => present
  }
}

Installing PHP

To install PHP we need to install a range of related packages including the Apache PHP module. This would be in the file provision/modules/php/init.pp:

class php {

  package { "php5":
    ensure => present
  }

  package { "php5-mysql":
    ensure => present
  }

  package { "php5-dev":
    ensure => present
  }

  package { "php5-curl":
    ensure => present
  }

  package { "php5-gd":
    ensure => present
  }

  package { "php5-imagick":
    ensure => present
  }

  package { "php5-mcrypt":
    ensure => present
  }

  package { "php5-memcache":
    ensure => present
  }

  package { "php5-mhash":
    ensure => present
  }
  package { "php5-pspell":
    ensure => present
  }

  package { "php5-snmp":
    ensure => present
  }

  package { "php5-xmlrpc":
    ensure => present
  }

  package { "php5-xsl":
    ensure => present
  }

  package { "php5-cli":
    ensure => present
  }

  package { "php-pear":
    ensure => present
  }

  package { "libapache2-mod-php5":
    ensure => present,
    require => [Package[php5], Package[apache2]]
  }
}