Skip to content

Commit 681b8b9

Browse files
authored
Hello world w/ Puppet & GCP
A step-by-step with Apache & PHP via Puppet on GCP.
1 parent d300a40 commit 681b8b9

4 files changed

+673
-0
lines changed

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Puppet Tutorials & Tips & Tricks
2+
3+
This repository contains step-by-step instructions and tips for Puppet users.
4+
5+
### Why Puppet?
6+
7+
Puppet helps automate deployment, and has a vast selection of powerful
8+
deployment [modules][] ready for use. We'll leverage those modules to create a
9+
simple, easy to maintain, and reliable deployment setup.
10+
11+
## Google Cloud Platform
12+
13+
### Getting Started
14+
15+
- [Setup Puppet and Apache][hello-puppet]: This is the simplest step-by-step to
16+
Puppet and cloud ever written! (allegedly)
17+
- [Serving my first Web site][hello-apache]: This is the minimalistic "serve me
18+
a page" step-by-step
19+
- [Serving my first PHP site][hello-php]: "serve me a page" now with dynamic PHP
20+
content
21+
22+
### Advanced Cloud Deployment
23+
24+
- [Deploying an e-Commerce portal][]: This tutorial was presented at
25+
[Puppet Conf 2016][] and shows how to use Google Cloud Platform modules for
26+
Puppet to deploy an e-commerce portal named [Magento][].
27+
28+
29+
[hello-puppet]: google/setup_puppet_and_apache_google-cloud-platform.md
30+
[hello-apache]: google/first_web-app_google-cloud-platform.md
31+
[hello-php]: google/setting_up_php.md
32+
[modules]: https://forge.puppet.com
33+
[Deploying an e-Commerce portal]: https://github.com/nelsonjr/puppetconf-2016
34+
[Puppet Conf 2016]: https://puppetconf2016.sched.com/event/6fj8/puppetize-all-the-things-google-cloud-nelson-araujo-google-david-schmitt-puppet
35+
[Magento]: https://magento.com
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Simplest Web page on Google Cloud Platform with Puppet
2+
3+
* [Creating your first page]()
4+
* [Running gcloud... 'Insufficient Permission'. Why!?](#running-gcloud-insufficient-permission-why)
5+
6+
You need to have Puppet and Apache installed. Follow the steps on
7+
setup [Setup Puppet and Apache][] before continuing.
8+
9+
## Creating your first page
10+
11+
Create a Puppet manifest, say coming-soon.pp with:
12+
13+
```puppet
14+
include apache
15+
16+
file { '/var/www/html/index.html':
17+
ensure => present,
18+
content => 'My first page served. Full app coming soon!',
19+
}
20+
```
21+
22+
and apply it:
23+
24+
```
25+
[root@my-first-app ~]# puppet apply coming-soon.pp
26+
Notice: Compiled catalog for my-first-app.c.graphite-playground.google.com.internal in environment production in 1.43 seconds
27+
Notice: /Stage[main]/Main/File[/var/www/html/index.html]/ensure: defined content as '{md5}4fc3570416d1d43fbccb01c7bfd67a27'
28+
Notice: Applied catalog in 0.65 seconds
29+
```
30+
31+
## Running gcloud... 'Insufficient Permission'. Why!?
32+
33+
In a security conscious environment the machine should not be able
34+
to change its own security settings, or an attacker could open services if they
35+
manage to get into the machine. That's the default settings of Google Cloud
36+
Platform.
37+
38+
The means you have to run the [gcloud][] commands from outside the computer you
39+
wish to configure, e.g. from the computer you used to _create_ the machine, or
40+
from your computer (the computer you are actually typing on).
41+
42+
If you want to use the [gcloud][] commands please install the [Google Cloud
43+
SDK][] on your computer. Alternatively you can use the [Developer Console][] to
44+
perform the configuration actions.
45+
46+
47+
## Opening Firewall
48+
49+
For security Google Cloud Platform built-in firewall blocks all access to your
50+
machines. But that means nobody can see your site, so we need to "poke a hole"
51+
on the firewall:
52+
53+
```
54+
gcloud compute instances add-tags my-first-app --tags http-server \
55+
--zone=us-central1-a
56+
```
57+
58+
The special `http-server` tag will tell Google that your machine is a web server
59+
and the default port should be allowed in.
60+
61+
### Using Developer Console
62+
63+
If you don't have the [gcloud][] tool you can use the Developer Console for this
64+
step:
65+
66+
1) Click on the machine
67+
2) Click Edit
68+
3) Check the `[x] Allow HTTP traffic` option
69+
4) Save
70+
71+
## Get your machine IP address
72+
73+
The `External IP` shows in the Developer Console. After the command above
74+
completes you can refresh the page and click on it. You can put that on your
75+
browser and see a blank page.
76+
77+
If you're using the [gcloud][] tool you can execute:
78+
79+
```
80+
gcloud compute instances list my-first-app --zone=us-central1-a
81+
```
82+
Take the `EXTERNAL_IP` and put on your browser.
83+
84+
## See your page
85+
86+
You should see your page with whatever you put in the `content` section of the
87+
Puppet file.
88+
89+
## Edit your content (just for fun)
90+
91+
1) Go back and change your Puppet file `content` to something else
92+
2) Apply the file again
93+
3) Refresh your browser
94+
95+
You should see your new content.
96+
97+
98+
## What's Next? I don't want boring, static pages!
99+
100+
The content here is static. The Internet is full of sites that create content
101+
when you visit them. Let's do the same! Follow [Setting up PHP & Hello PHP
102+
site][next] to get there.
103+
104+
105+
[next]: setting_up_php.md
106+
[Setup Puppet and Apache]: setup_puppet_and_apache_google-cloud-platform.md
107+
[gcloud]: https://cloud.google.com/sdk
108+
[Google Cloud SDK]: https://cloud.google.com/sdk
109+
[Developer Console]: https://cloud.google.com/console

google/setting_up_php.md

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# Setting up PHP & Hello PHP site
2+
3+
[PHP][] is one most versatile web languages around. It is also very simple to
4+
setup. There are various other web toolkits out there, but for the sake of
5+
simplicity of this tutorial we'll stick with PHP for now
6+
7+
## Enable PHP on Apache
8+
9+
Let's update our apache manifest to include the Apache PHP plugin:
10+
11+
```puppet
12+
include apache
13+
include apache::mod::php
14+
```
15+
16+
and as usual, apply it via Puppet:
17+
18+
```
19+
[root@my-first-app ~]# puppet apply apache-via-mod.pp
20+
Notice: Compiled catalog for my-first-app.c.graphite-playground.google.com.internal in environment production in 1.36 seconds
21+
Notice: /Stage[main]/Apache::Mod::Php/Apache::Mod[php5]/Package[php]/ensure: created
22+
Notice: /Stage[main]/Apache/File[/etc/httpd/conf.d/php.conf]/ensure: removed
23+
Notice: /Stage[main]/Apache::Mod::Php/File[php5.conf]/ensure: defined content as '{md5}24420bffd2939b1fe3ff0ca36dbad419'
24+
Notice: /Stage[main]/Apache::Mod::Php/Apache::Mod[php5]/File[php5.load]/ensure: defined content as '{md5}5aabed26d29e38135b5157acff80a2a6'
25+
Notice: /Stage[main]/Apache/File[/etc/httpd/conf.modules.d/10-php.conf]/ensure: removed
26+
Notice: /Stage[main]/Apache::Service/Service[httpd]: Triggered 'refresh' from 1 events
27+
Notice: Applied catalog in 4.51 seconds
28+
[root@my-first-app ~]#
29+
```
30+
31+
Puppet did all the necessary work to setup the necessary modules on your
32+
machine, including the necessary packages from the Operating System. Apache is
33+
now *ready* to take PHP content, so let's do it.
34+
35+
36+
## Update main website page
37+
38+
Let's replace our main "coming soon" page with a page that tells the time:
39+
40+
```puppet
41+
include apache
42+
include apache::mod::php
43+
44+
# Let's get rid of the coming soon file
45+
file { '/var/www/html/index.html':
46+
ensure => absent,
47+
}
48+
49+
# Define our main website as a PHP page
50+
file { '/var/www/html/index.php':
51+
ensure => file,
52+
content => '<?php echo "Now it is " . date("r"); ?>', # echoes the time
53+
}
54+
```
55+
56+
... and apply ....
57+
58+
```
59+
[root@my-first-app ~]# puppet apply php-sample.pp
60+
Notice: Compiled catalog for my-first-app.c.graphite-playground.google.com.internal in environment production in 1.31 seconds
61+
Notice: /Stage[main]/Main/File[/var/www/html/index.html]/ensure: removed
62+
Notice: /Stage[main]/Main/File[/var/www/html/index.php]/ensure: defined content as '{md5}7b787125b1026cc7582f46a127f58115'
63+
Notice: Applied catalog in 0.67 seconds
64+
[root@my-first-app ~]#
65+
```
66+
67+
Now refresh your browser (where the 'coming soon' page was showing) and you
68+
should see something like this:
69+
70+
```
71+
Now it is Sun, 25 Jun 2017 07:38:19 +0000
72+
```
73+
74+
## Updating time zone
75+
76+
You will notice that the time is apparently not correct. Actually it is (the
77+
machine has its clock synced to an atomic clock). The "problem" is that it is
78+
showing the time in "UTC" (Greenwich) time zone "+0000".
79+
80+
PHP `date('r')` command draws the time zone from the machine, so we need to
81+
adjust the machine time zone accordingly. (There are more sophisticated ways of
82+
dealing with global time, but they are beyond this quick start tutorial).
83+
84+
You'll never guess: we'll be using Puppet to do that :) We'll write this Puppet
85+
manifest once and use it anytime we need. As before put it in your file and
86+
apply it using Puppet:
87+
88+
```puppet
89+
include apache
90+
include apache::mod::php
91+
92+
file { '/etc/php.d/timezone.ini':
93+
ensure => file,
94+
content => join([
95+
'[Date]',
96+
'date.timezone = America/Los_Angeles',
97+
]),
98+
notify => Class['apache::service'],
99+
}
100+
```
101+
102+
The expected output:
103+
104+
```
105+
[root@my-first-app ~]# puppet apply time.php
106+
Notice: Compiled catalog for my-first-app.c.graphite-playground.google.com.internal in environment production in 1.50 seconds
107+
Notice: /Stage[main]/Main/File[/etc/php.d/timezone.ini]/ensure: defined content as '{md5}34cb0851c4094296a53415c09486c53d'
108+
Notice: /Stage[main]/Apache::Service/Service[httpd]: Triggered 'refresh' from 1 events
109+
Notice: Applied catalog in 1.80 seconds
110+
[root@my-first-app ~]#
111+
```
112+
113+
The `include` and `file` portions are not new. We're just creating another file
114+
that PHP will read to configure time zone (following PHP [date.timezone
115+
docs][php-date-timezone]). You can use any of the [Supported
116+
Timezones][php-timezones] described.
117+
118+
The new part here is the `notify =>` line. This one is critical for you to
119+
understand as it is a core Puppet strength. This tells Puppet to notify the
120+
service (and reconfigure itself) if there are changes to the file being applied.
121+
Notice the `Service[httpd]: Triggered 'refresh'` in your apply output.
122+
123+
**Why?** PHP reads this file when the Apache web server starts up. That means if
124+
you create the file and put it there nothing happens until next time the Apache
125+
server restarts (or reloads gracefully).
126+
127+
### What did Puppet do under the covers?
128+
129+
Puppet is now watching the file you defined, and if **and only if** there are
130+
changes to that file it will tell Apache to reload gracefully, picking up the
131+
changes.
132+
133+
### Try without Puppet yourself
134+
135+
1. Change `/etc/php.d/php.ini` to some other time zone
136+
2. Reload your web page
137+
3. You will notice that nothing changed
138+
4. Now execute `systemctl restart httpd`
139+
5. Reload the page on the browser
140+
6. Now it shows the correct time zones.
141+
142+
Puppet just did that for you without needing to resort to understanding which
143+
command on the operating system does that (systemctl is for Cent OS). **Other
144+
operating systems uses different commands that you have to know to manage
145+
them.** Note that you also did not need to know all the inner details of Apache
146+
or PHP get this this going.
147+
148+
> *[Pro Tip]* Before you deploy your application for real (what we call
149+
> production) I suggest you do understand better Apache and PHP. How to
150+
> configure it properly: to run fast, secure, reliable. It sucks when you put
151+
> your site and some hacker vandalizes it, or worse, steals all your data.
152+
153+
That's it for now. I hope you got the basis of what Puppet is great at.
154+
155+
Ah... added bonus: these manifests you wrote will run on any Linux operating
156+
system without changes. It is what we call _portable code_.
157+
158+
159+
[PHP]: https://www.php.net
160+
[php-date-timezone]: http://php.net/manual/en/datetime.configuration.php#ini.date.timezone
161+
[php-timezones]: http://php.net/manual/en/timezones.america.php

0 commit comments

Comments
 (0)