LFCS - Setting Up A Web Application

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
372
Reaction score
432
Credits
13,798
The LFCS exam requires knowledge of setting up a web application. In this article, we will set up a Hypertext PreProcessor (PHP). We will also create a small app using PHP in HTML.

We will not cover a lot of PHP coding in this article, but like any scripting language, there is a lot you can learn and do with the scripting language.

Installing PHP

This process is the same for CentOS and Ubuntu. Once installed, PHP is the same for both Operating Systems (OS).

To install PHP, use the command, use one of the following, depending on your OS:

sudo yum install php
- or -
sudo apt install php


Even though you install PHP on both Operating Systems with the same command, the Operating System stores the information in different locations. For CentOS, it is in '/etc/httpd/'. Ubuntu stores the PHP data in '/etc/php/<version>'. In my case, the version is '7.2'. For the version, you only need the major and minor version numbers. The real version on my Ubuntu system is '7.2.24', but the folder is only '7.2'.

No matter the OS, you can find the version number with the command 'php -v'.

Now that we have PHP on our server, there is no PHP service to start or enable. PHP is part of the Apache service and all you need to do is restart the 'httpd' service. Use the command 'sudo systemctl restart httpd' on CentOS, and 'sudo systemctl restart apache2' on Ubuntu.

PHP Settings

The configuration of PHP is done through the 'php.ini' file. The locations for CentOS are '/etc/' and Ubuntu is '/etc/php/7.2/apache2/cli' for command-line and '/etc/php/7.2/apache2/apache2/' when running from the web browser.

Within the 'php.ini' file are five entries that you can edit to help limit issues you may have in a business environment. The following are the entries with their default value:

max_execution_time = 30
upload_max_filesize = 2M
max_file_uploads = 20
memory_limit = 128M
post_max_size = 8M


On Ubuntu, the 'memory_limit' is set to '-1', which means no memory limit.

To see the configuration, use the command 'php -i' from a command line. For specific lines, you can pipe it through 'grep'.

The 'max_execution_time' is the number of seconds that a script is allowed execution time. If you have very long scripts, then 30 seconds may not be enough. The 'upload_max_filesize' is the maximum file size allowed to be uploaded using a PHP script. The default is 2 MB, if you have clients that will require to send larger files, this should be adjusted. The 'max_file_uploads' is 20 files per session. Again, if this is a low amount, increase it as needed. The 'memory_limit' is the amount of memory used by a script. Unless you are doing something extremely large, the value should be fine. The 'post_max_size' is the space available to post files. The value should be larger than the 'upload_max_filesize'. If a client sends files to your server, and the largest is 2 MB but attempts to send 10 files, if each file is 1.5 MB, then the 'post_max_size' is too small.

You can also see the information through a browser. To do this, we need to set up a web page for this.

In your folder for web pages, for either OS it is in '/var/www/html', we will make a file called 'php.php'. The following needs to be placed into it:

<h1> PHP Information for the Web Server </h1>
<br>
<p> The following is the PHP information saved in the php.ini file. </p>
<br>
<?php phpinfo(); ?>


Once saved, you can open your browser and go to the site 'server1.example.com/php.php', and change the server name and domain accordingly. You should see a site similar to Figure 1.

Figure 1.JPG

FIGURE 1

This should verify that PHP is running since we embedded PHP code into the HTML and we could see the configuration information.

So, let's try something a little more tricky. I've seen a few PHP examples online that convert Celsius to Fahrenheit, and vice versa. Let's throw in Kelvin as well. We will prompt the user for a temperature value. Then we will display three buttons. One to convert from Celsius, one from Fahrenheit and one from Kelvin. Each button has a different name: 'submitc', 'submitf' and 'submitk'. Depending on which button is pressed, it will perform the proper conversions. The code is as follows:

<h1> Temperature Conversion </h1>
<p> Enter a temperature value in the appropriate box and select submit.</p>
<?php if (isset($_GET['submitc'])) {
$c = $_GET['temperature'];
$f = (( $c *9 ) / 5 ) + 32;
$k = (( $c + 273.15 ));
echo "<p> $c Centigrade is $f Fahrenheit, and $k Kelvin.</p>";
}
?>
<?php if (isset($_GET['submitf'])) {
$f = $_GET['temperature'];
$c = (( $f -32 ) / 1.80 );
$k = (( $c + 273.15 ));
echo "<p> $f Fahrenheit is $c Centigrade, and $k Kelvin.</p>";
}
?>
<?php if (isset($_GET['submitk'])) {
$k = $_GET['temperature'];
$f = (( $k - 273 ) *1.8 ) + 32;
$c = (( $k - 273.15 ));
echo "<p> $k Kelvin is $f Fahrenheit, and $c Centigrade.</p>";
}
?>
<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="temperature"></input>
<br>
<input type="submit" value="Convert from C" name="submitc"></input>
<input type="submit" value="Convert from F" name="submitf"></input>
<input type="submit" value="Convert from K" name="submitk"></input>
</form>


The value taken from the text box is called 'temperature'. It is read into the appropriate temperature scale variable. Then the conversions are done to the other two scales. Once the conversions are completed, we then echo out the converted answers.

The initial screen output is shown in Figure 2.

Figure 2.JPG

FIGURE 2

If you get an error, on CentOS you need to check the file '/var/log/httpd/error_log' and Ubuntu is '/var/log/apache2/error.log', you can see the error in the log. Specific lines in the PHP code will be signified so you can correct any errors.

There are plenty of sites on the Internet that cover PHP coding if you are interested in learning more.

If you see the same as Figure 2, then PHP is working.

Conclusion

You should have a basic understanding of PHP installation and configuration.

If you want to learn some PHP coding, it may be beneficial, but not necessary.

Just be aware of PHP installation and changing some configuration values. If you do plan on coding, then be aware of the log file location when troubleshooting coding errors.
 

Members online


Top