4. Create a Web Application#

This week we’re going to start turning your VM 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#

Most of what we have to do this week is on Linux. In class you’ll see how package management works on different Linux distros. Also, we’ll run the commands that install your first web application on your VM. But, it’s important to remember that you shouldn’t have any bespoke setup on a VM, so we’ll remember the commands and make them a part of a startup script.

Step 1: Allow HTTP Access#

The firewall prevents your VM from accessing the internet. In order to see the results of this lab you need to edit the VM configuration to allows HTTP connections. This can be done by navigating to your VM and clicking “Edit”. There is a checkbox in the settings.

Step 2: Your First Web Application#

PHP allows you to mix HTML and code to create server-side dynamic web content. PHP was extremely popular in the 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.

Use this command to write the PHP code into a file called /var/www/html/index.php:

$ sudo tee /var/www/html/index.php << 'EOF' > /dev/null
<!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>
EOF

You need to create a file that contains the visit count. Do that with this command:

$ sudo tee /var/www/html/counter.txt << 'EOF' > /dev/null
0
EOF

Now use commands to do the following:

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

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

Make a note of the commands you used.

Step 3: Automate It!#

The commands for this lab should run anytime a new VM is created in your project. That ensures that new VMs come online and are ready to be a part of your application. In GCP you can put a script in the Metadata of your project that is automatically run on new VMs.

Follow the instructions to put the commands from the previous setup into a startup script.

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 VM

  2. Install Apache and PHP

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

  4. Automate the process with a startup script

Your documentation should contain your startup script and clearly list the instructions for installing the script into your project metadata. Post this milestone to the class discussion forum. 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. Your startup script

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