How to Use Puppet Modules to Create a LAMP Stack on Ubuntu 20.04

Puppet is a powerful open-source configuration management tool that can be used to deploy, configure, manage, and maintain a server while accessing it remotely. Puppet is written in Ruby and helps you to deploy and manage a single server – all the way up to thousands of physical and virtual servers – all from a central location.

Puppet modules are collections of manifest files and data that allow Puppet to load and find classes, facts, functions, defined types, and tasks. With Puppet modules, you can install and update packages, create files and directories, and/or make any configuration changes quickly and easily. It aims to perform and automate almost any task, such as launching a LAMP environment, setting up virtual hosts, etc.

In this tutorial, we will show how to create a LAMP module to deploy a LAMP stack on the Puppet agent machine, both of which are Ubuntu 20.04 VPSes. Let’s begin.

Before we Begin

You will need:

  • Two Ubuntu 20.04 virtual servers with Puppet master and Puppet agent configured.
  • A root access enabled or a user with sudo privileges.

Getting Started

First, you will need to log into your server using SSH. You can do that by entering this command:

ssh [email protected]IP_Address -p Port_Number

Remember to replace “root” with your username if you are not using the root user. Change “IP_Address” and “Port_Number” according to your server’s IP address and SSH port number. The default port number is 22.

Once you are logged in, you can proceed to the next step.

Verify Puppet Server and Agent

First, verify the Puppet service by running the following command on the Puppet master node. We need to make sure that the service is active and running:

systemctl status puppetserver

Next, list all of the signed and unsigned agent requests with the following command:

/opt/puppetlabs/bin/puppetserver ca list --all

You should see the following output:

Signed Certificates:
    puppet-client       (SHA256)  58:73:AE:62:04:9E:B8:0F:16:07:83:08:34:4A:00:D2:E6:82:9B:47:2A:00:9F:F4:08:AE:56:A8:E7:1B:6A:31
    puppet-master       (SHA256)  7F:23:98:18:0E:3F:0F:FD:3E:12:FD:43:A6:50:C2:4C:58:0F:C6:EB:B0:5A:2A:74:6F:D8:A0:95:BC:31:EA:47	alt names: ["DNS:puppet-master", "DNS:puppet-master"]	authorization extensions: [pp_cli_auth: true]

On the Puppet agent node, run the following command to test the connectivity between both nodes.

/opt/puppetlabs/bin/puppet agent --test

If everything is fine, you should get the following output:

Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Retrieving locales
Info: Caching catalog for puppet-client
Info: Applying configuration version '1583136740'
Notice: Applied catalog in 0.05 seconds

Create a LAMP Module

First, you will need to create a LAMP module on the Puppet master node. To create a module, you must create a directory whose name matches your module name in Puppet’s modules directory, and it must contain a directory called manifests, and that directory must contain an init.pp file.

To do so, change the directory to the Puppet module directory and create a directory named lamp and then a manifests directory inside it:

cd /etc/puppetlabs/code/modules/
mkdir -p lamp/manifests

Next, create a init.pp file that will contain a Puppet class that matches the module name. We’re going to use nano for this, but you can use any text editor you like.

nano lamp/manifests/init.pp

Add the following lines:

class lamp {
  # execute 'apt-get update'
  exec { 'apt-update':                    # exec resource named 'apt-update'
    command => '/usr/bin/apt-get update'  # command this resource will run
  }

  # install apache2 package
  package { 'apache2':
    require => Exec['apt-update'],        # require 'apt-update' before installing
    ensure => installed,
  }

  # ensure apache2 service is running
  service { 'apache2':
    ensure => running,
  }

  # install mysql-server package
  package { 'mysql-server':
    require => Exec['apt-update'],        # require 'apt-update' before installing
    ensure => installed,
  }

  # ensure mysql service is running
  service { 'mysql':
    ensure => running,
  }

  # install php package
  package { 'php':
    require => Exec['apt-update'],        # require 'apt-update' before installing
    ensure => installed,
  }
  # ensure info.php file exists
  file { '/var/www/html/info.php':
    ensure => file,
    content => '',    # phpinfo code
    require => Package['apache2'],        # require 'apache2' package before creating
  }
}

Save and close the file when you are finished.

Create the Main Manifest File

A manifest is a file that contains client configurations that can be used to install the LAMP stack on the agent node. The main puppet manifest file is located at the /etc/puppetlabs/code/environments/production/manifests directory.

You can create a new manifest file named site.pp with the following command:

nano /etc/puppetlabs/code/environments/production/manifests/site.pp

Add the following lines:

node default { }

node 'puppet-agent' {
  include lamp
}

Save and close the file.

In the above file, a node block allows you to specify the Puppet code that will only apply to certain agent nodes. The default node applies to every agent node that does not have a node block specified.

Here, we have specified the puppet-agent node block that will apply only to our puppet-agent agent node. We have also added the include lamp snippet to get Puppet to use the lamp module on the agent node.

Test the LAMP Module

Now, you will need to pull the LAMP configuration on the agent node from the master node.

On the agent node, run the following command to pull the LAMP configuration from the master node.

/opt/puppetlabs/bin/puppet agent --test

This will install and set up the LAMP stack on the agent node, as can be seen below:

Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Retrieving locales
Info: Caching catalog for puppet-agent
Info: Applying configuration version '1594188800'
Notice: /Stage[main]/Lamp/Exec[apt-update]/returns: executed successfully
Notice: /Stage[main]/Lamp/Package[apache2]/ensure: created
Notice: /Stage[main]/Lamp/Package[mysql-server]/ensure: created
Notice: /Stage[main]/Lamp/Package[php]/ensure: created
Notice: /Stage[main]/Lamp/File[/var/www/html/info.php]/ensure: defined content as '{md5}d9c0c977ee96604e48b81d795236619a'
Notice: Applied catalog in 73.09 seconds

And with that, the LAMP stack has now been installed on your agent node. To check it, open your web browser and type the URL http://agent-ip-address/info.php. You should see your PHP page.

Congratulations! you have successfully created a LAMP module on your Puppet master and have deployed it successfully on your agent node (or nodes) with Puppet.


Deploying these changes on agent nodes is made to be as easy and as automated as possible, however you still need to configure both sides and perform these tasks yourself. If you use one of our Managed Ubuntu VPS plans, all you would need to do is to simply ask our expert admin team to install whatever you need for you. They can install and set up anything else that you might want, whenever you want it, as well as perform any server maintenance, all without you having to do a thing.

We hope you found this tutorial helpful. If you did, we would appreciate you sharing this tutorial with your friends through social media by using our share shortcuts, or you can leave a comment telling us how the installation went for you. Thank you.

Leave a Comment

To prove you are human please solve the following *