Finally, we need to pull these modules together, and install them when our machine is provisioned. To do this, we simply add the following modules to our vagrant.pp
manifest file in the provision/manifests
folder.
We need to include our nginx
class and optionally provide a filename for the configuration file; if we don't provide one, the default will be used:
class { 'nginx': file => 'default' }
Similarly for PHP, we need to include the class and in this case, pass an nginx
parameter to ensure that it installs PHP5-FPM too:
class { 'php': nginx => true }
We should tell our Vagrant virtual machine what its hostname is by adding a host resource to our manifest:
host { 'lemp-stack.local': ip => '127.0.0.1', host_aliases => 'localhost', }
Because some of our projects might involve sending e-mails, we should install e-mail sending services on our virtual machine. As these are simply two packages, it makes more sense to include them in our Vagrant manifest, as opposed to their own modules:
package { "postfix": ensure => present } package { "mailutils": ensure => present }
Because the MySQL module is very flexible and manages all aspects of MySQL, there is quite a bit for us to configure. We need to perform the following steps:
Create a database.
Create a user.
Give the user permission to use the database (
grants
).Configure the MySQL root password.
Install the MySQL client.
Install the MySQL client bindings for PHP.
The MySQL server class has a range of parameters that can be passed to configure it, including databases, users, and grants. So, first, we need to define what the databases, users, and grants are that we want to be configured:
$databases = { 'lemp' => { ensure => 'present', charset => 'utf8' }, } $users = { 'lemp@localhost' => { ensure => 'present', max_connections_per_hour => '0', max_queries_per_hour => '0', max_updates_per_hour => '0', max_user_connections => '0', password_hash => 'MySQL-Password-Hash', }, }
Note
The password_hash
parameter here is for a hash generated by MySQL. You can generate a password hash by connecting to an existing MySQL instance and running a query such as SELECT PASSWORD('password')
.
The grant maps our user and database and specifies what permissions the user can perform on that database when connecting from a particular host (in this case, localhost—so from the virtual machine itself):
$grants = { 'lemp@localhost/lemp.*' => { ensure => 'present', options => ['GRANT'], privileges => ['ALL'], table => 'lemp.*', user => 'lemp@localhost', }, }
We then pass these values to the MySQL server class. We also provide a root password for MySQL (unlike earlier, this is provided in plain text), and we can override the options from the MySQL configuration file. This is unlike our own Nginx module that provides a full file—in this instance, the MySQL module provides a template configuration file and the changes are replaced in that template to create a configuration file:
class { '::mysql::server': root_password => 'lemp-root-password', override_options => { 'mysqld' => { 'max_connections' => '1024' } }, databases => $databases, users => $users, grants => $grants, restart => true }
As we will have a web server running on this machine, which needs to connect to this database server, we also need the client library and the client bindings for PHP, so that we can include them too:
include '::mysql::client' class { '::mysql::bindings': php_enable => true }