PHP on the apache Web Server

In this section we're going to install the popular PHP scripting language.

PHP Installation Top of Page

In this section we will install the PHP scripting engine and ensure that it's set to work with apache. PHP has become very popular, and - by design - is made to work with apache. There are a couple of steps to install PHP and set it to be parsed correctly through apache.

Figure 6.29. PHP Image #1

PHP Image #1

In the screenshot above, we have checked for the presence of PHP . As shown in the return, it's not installed.

Figure 6.30. PHP Image #2

PHP Image #2

PHP , when configured to work with apache, inserts as a module[26] The screenshot above parses the loaded modules in apache's configuration, and looks for the presence of PHP . Once again, no return. (However, note that the Syntax of the file is OK.)

Figure 6.31. PHP Image #3

PHP Image #3

By running the command shown above, `yum -y install php`, PHP will be installed with all dependencies.

Figure 6.32. PHP Image #4

PHP Image #4

This screenshot shows the action as PHP is installed.

Figure 6.33. PHP Image #5

Alt Tag.

Now, running the same checks as before, we can see that PHP has been installed as well as initially configured to work with apache.

Fine Tune PHP Configuration Top of Page

Now that PHP is installed, it's important to note that there are two distinct configuration files that are present on the system. Each of these two files alter and/or configure a major aspect of how PHP acts and reacts, each in their distinct area.

  1. '/etc/httpd/conf.d/php.conf' - This file controls a) the fact that PHP is interpreted through a web browser as a script, and b) how that web interaction behaves.
  2. '/etc/php.ini' - This file controls how PHP as a scripting language acts when invoked on the system.[27]

For the most part, PHP will work well without additional configuration changes. However, we need to enter the correct time zone. The steps below will address this task.

Figure 6.34. PHP Image #6

PHP Image #6

The first step is to go to the '/etc' directory. Once there, back up the php.ini file as shown. Next, edit the file.

Figure 6.35. PHP Image #7

PHP Image #7

We need to make an adjustment to the php.ini configuration file by setting the correct time zone. Alter the file as shown in the above screen shot.

[Warning] Backing Up Config Files

It is extremely important to backup config files before making changes to them. A general rule of thumb is to never get yourself into a situation that you can't back out of. If you don't believe me, don't do it. Just wait and see what your karma will eventually do to you.

The final step in this configuration is to restart or reload the server. See Figure 6.26, "httpd Reload" to perform that step.

Create PHP Test Content Top of Page

Figure 6.36. PHP Image #8

PHP Image #8

Now we'll create a very simple PHP script that will do two things: a) test that PHP is working on the server, & b) show us the built-in capability of PHP on the server. Perform the following steps:

  1. Changed location to the '/var/www/html' directory, as shown in the image above.
  2. Created a file called 'test.php' in that directory.
  3. Edit that file and enter the contents as shown below.

Figure 6.37. PHP Image #9

PHP Image #9

The 'test.php' file should have the simple contents as shown above, and below.

[Important] Getting PHP Files to Parse Properly

There are two things necessary to tell apache to parse PHP properly:

  1. The file must end with the .php extension.
  2. Any (and/or all) PHP code within the file must have container tags as shown.[28]

Figure 6.38. PHP Configuration Read-Out

Alt Tag.

Et voilĂ . If all has gone well, when you navigate to the page http://<>/test.php, the output should be similar to that shown above. If not, stop and figure out why. If you take a look through the page that results, it shows most everything your web server, now equipped with PHP, is capable of.[29]

[26] The apache core is compiled with built-in modules. Other modules can be inserted at run time, which is how PHP will be treated. This keeps the basic apache executable "clean and lean", and gives the option to only load modules that are needed for the specific purpose of the server.

[27] An important distinction is that PHP can also be called through the command line - without going through the web browser. In that case, the '/etc/php.ini' will affect PHP behavior, and - since it's not in a browser - '/etc/httpd/conf.d/php.conf' will have no effect.

[28] There are actually several variations of the PHP container tags. In some instances, the closing tag is not required or preferred. However, that subject is beyond the scope of this course. See the PHP documentation for more details.

[29] Leaving the phpinfo() script/page accessible is considered a security risk. Anyone accessing this page can potentially target vulnerabilities on your web server. Remove this script from public access on production servers.