Create a Web Application#

This week we’re going to start turning your devbox into a working server by installing the Apache webserver and PHP. Apache and PHP are required to run Mediawiki but, there’s a lot more setup required before we can make a complex program like MediaWiki work. This week you’ll get an example program running. To do so you should review how to use the chmod, chown and chgrp commands on Linux.

Labs#

The setup for this milestone is more specific than the last ones. The lab this week doesn’t use external resources. The instructions are on this page. In class we’ll do the following tasks:

Tip

Updating packages takes a long time on an e2-micro, update to an e2-medium while you do the updates.

  1. Update the software on your devbox

  2. Find the package that installs Apache and PHP

  3. Install Apache and PHP

  4. Use curl to check your webserver

  5. Use port forwarding in vscode to view your webserver

You First Web Application#

PHP allows you to mix HTML and code to create server-side dynamic web content. PHP was extremely popular in the early 2000s and there are still, many, many applications that use it, including Wikipedia. One awesome thing about PHP is how easy it is to get started without having to do a lot of coding. In this lab you will use the PHP code shown below.

<!DOCTYPE html>
<html>
    <head>
        <title>My cis-91 Project</title>
    </head>
    <body>
        <h1>Welcome to my cis-91 project!</h1>
        <?php 

$file = 'counter.txt';
$display = '';

if (!\file_exists($file) OR !\is_writable($file)) {
    throw new Exception(\sprintf("'%s' does not exist or is not writable.", $file));
}

if (!($fp = \fopen($file, 'r'))) {
    throw new Exception(\sprintf("'%s' could not be opened.", $file));
}
$filesize = \filesize($file);
$count = \fread($fp, $filesize);
\fclose($fp);

if (!($fp = \fopen($file, 'w'))) {
    throw new Exception(\sprintf("'%s' could not be opened for writing.", $file));
}

\fwrite($fp, $count + 1);
\fclose($fp);


echo "You are visitor #$count";
        ?>

    </body>
</html>

The steps for this lab are:

  1. Copy and paste the PHP code above into a file on your devbox called /var/www/html/index.php

  2. Delete the /var/www/html/index.html file on your devbox. That will make index.php the default page when you access your site.

  3. Create a file called /var/www/html/counter.txt and put the number 0 in it.

  4. Fix the permissions on counter.txt so that the www-data user can read and write it.

Having trouble?

In order to get the lab working you have to put files in just the right place and set permissions properly. If you’re having trouble look in the following files on your dev box for clues about what could be wrong:

  1. /var/log/apache2/error.log - PHP errors are reported here

  2. /var/log/apache2/access.log - All browser accesses are logged here.

Project Documentation#

To meet the project requirements for this week you have to accomplish the following tasks:

  1. Update to the latest software on your devbox

  2. Install Apache and PHP

  3. Install the PHP script in this milestone and create the counter file

Your documentation should clearly list what packages you installed on your devbox. In your final writeup you should keep a section where you track installed packages. In future milestones we’ll add more packages. Your documentation should also clearly list the chown and chmod commands you used to make the PHP example work. Post this milestone to the class discussion form. After you’ve posted you will be able to see other’s documentation and make changes to your own. Your documentation must include the following items:

  1. The packages you installed.

  2. The chmod and chown commands you used to make the PHP program work.

  3. A screenshot of your application seen through a local browser.

Warning

Don’t forget to reduce your machine size to e2-micro after you’re done!