Assignment: Intro to PHP

In this assignment, your job is to:

  1. Create a website directory on your Google Cloud server
  2. Be able to upload PHP files there
  3. Set up the Propel library in that directory with the posting database we all made together
  4. Create a PHP page that lists all the user records from that database
  5. Bonus: Add a form at the bottom so you can POST new users (username, first_name, last_name, join_date)

Note that the examples given to you in the Propel Setup reference show you most of what you need to do. However, there's a lot of details to wrestle with.

Project Setup

The standard setup for your LAMP server serves up websites from the /var/www/html directory. Follow these steps to setup a website project directory.

  1. Create the directory in your home directory (where you start)
    etomai@lamp-6312-vm:~$ ls -l
    total 0
    etomai@lamp-6312-vm:~$ mkdir myproject
    etomai@lamp-6312-vm:~$ ls -l
    total 4
    drwxr-xr-x 3 etomai etomai 4096 Mar 15 14:54 myproject
  2. Put a dummy html file in the project so you have something to test
    etomai@lamp-6312-vm:~$ echo "hi" > myproject/test.html
    etomai@lamp-6312-vm:~$ ls -l myproject
    total 1
    -rw-r--r-- 1 etomai etomai       3 Mar 21 22:53 test.html
  3. Link that project directory from /var/www/html
    etomai@lamp-6312-vm:~$ ls -l /var/www/html
    total 16
    -rw-r--r-- 1 www-data www-data 10708 Feb 22 01:05 index.html
    -rw-r--r-- 1 www-data www-data   132 Dec 12 19:14 robots.txt
    etomai@lamp-6312-vm:~$ sudo ln -s ~/myproject/ /var/www/html/myproject
    etomai@lamp-6312-vm:~$ ls -l /var/www/html
    total 16
    -rw-r--r-- 1 www-data www-data 10708 Feb 22 01:05 index.html
    lrwxrwxrwx 1 root     root        23 Mar 21 22:53 myproject -> /home/etomai/myproject/
    -rw-r--r-- 1 www-data www-data   132 Dec 12 19:14 robots.txt
  4. Now everything in that directory will be served up by the VM web server as the myproject directory. Test it by getting the external IP from your GCP Console and browse to http://[IP]/myproject/test.html

File Management

You'll submit your project to the empty repository below. Do not commit the vendor subdirectory (don't check the boxes for it!) that holds all the Propel files. It's quite large and there's no point. You should commit and push your models directory and the PHP file you made that displays all the users.

GitHub invite link to the repo:

https://classroom.github.com/a/8cmz3cCI

You can use command line git to directly clone, commit and push your files there on the cloud server without bringing anything down to your local machine. If you're happy using a command-line editor on the cloud server (nano, emacs, vim, vi, etc), then this is a fine solution.

Otherwise, you'll want to edit files locally on your machine and upload them for testing. (Note: Yes, you could test them locally using a LAMP install on your local machine. But since we want to learn about infrastructure, we're going to treat the cloud servers as our official platform). Editing locally is just like what we did with JavaScript. But instead of dragging the file into a local browser, we will upload them and access them over the web. There are two reasonable ways to do this.

One: Google Cloud Storage Buckets

This way is easy to set up, but not as convenient in the long run. Start here.

  1. Open an SSH terminal from the VM Instance Details page
  2. Selecting Storage->Browser from the menu in your GCP console
  3. Follow the instructions to create a new bucket (default settings are fine)
  4. (More details here: https://cloud.google.com/storage/docs/creating-buckets)
  5. Through that web interface, you can now drag and drop files (and directories) to upload them to the bucket
  6. To copy all the files in the bucket over to the current directory on your VM, use:
    gsutil cp -r gs://[BUCKET_NAME]/* .
  7. If you get an insufficient authorization error trying to copy the files, you may need to do gsutil config -b and follow the instructions to authorize your VM to access your storage.

Two: SFTP w/ Filezilla using SSH keys

This way is a pain to set up, but ultimately a more usable workflow. Unfortunately, our lab computers don't currently have FileZilla or the GCP SDK installed. On your own machine, however, you can do so.

  • Install the gcloud SDK: https://cloud.google.com/sdk/
  • Install FileZilla: https://filezilla-project.org/download.php?type=client
  • At a new terminal, run gcloud auth login and follow the directions. It will take you to a browser where you can login and authorize access.
  • On your GCP Console VM Instances page, where you launch SSH, click the drop-down menu and select view gcloud command. Copy and paste that into your terminal and run it. Agree to connect if an alert comes up.
  • gcloud will create SSH keys on first connect, and tell you where they are. You should see something like:
  • In the SSH terminal that opens, run whoami and note the username it prints out
  • Launch FileZilla, go to Edit->Settings and select SFTP. Click Add key file... and add the .ppk file from the location it gave you earlier (default is c:\Users\{username}\.ssh)).
  • Go to File->Site Manager and create a new connection with your VM's external IP address, the SFTP protocol and the username that you noted above. No password is needed since it's using SSH keys.
  • Connect and you can transfer files conveniently through the FileZilla interface. FileZilla also allows you to set a default editor and automatically update files as you edit them. That's something you can look up on your own.

Database Catch-up

If you missed or fell behind the database creation that we did through phpMyAdmin, here are the details. Please ensure that your database uses the same names (case-sensitive!) so that when we grade it it works with our database as well. There are details about setting up a foreign key in the Propel Relations reference (just pay attention to the database part).

Database name: posting

Table: user

  • id: int, primary key, auto_increment
  • username: varchar(32)
  • first_name: varchar(64)
  • last_name: varchar(64)
  • join_date: date

Table: post

  • id: int, primary key, auto_increment
  • user_id: int, indexed, foreign key to user.id (id in the user table)
  • content: text
  • post_date: datetime

Table: comment

  • id: int, primary key, auto_increment
  • user_id: int, indexed, foreign key to user.id
  • post_id: int, indexed, foreign key to post.id
  • content: varchar(512)
  • post_date: datetime