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
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 ouruploads
folder (we need to create analias
, 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 theprovision/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>
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']
}
}
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 } }
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]] } }