|
| 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