PHP is a popular server-side scripting language for web development. We provide an example PHP application for our beloved beers database that you used in your homework. We assume that you already have this database set up; if not, run /opt/dbcourse/examples/db-beers/setup.sh in your VM.

Setting Up

First, refresh your VM by running the following:
/opt/dbcourse/sync.sh
To install PHP (and the Apache web server, which PHP uses) on your VM, run the command:
/opt/dbcourse/install/install-apache-php.sh
Next, make a copy of the example PHP application so you can play with it (feel free to use a different destination as you see fit; for example, if you have a remote VM, putting it under shared wouldn’t be helpful):
cp -pr /opt/dbcourse/examples/php-beers/ ~/shared/php-beers
Then, link the PHP application into the website run by your Apache server (make sure the destination you picked above is used here):
sudo ln -s ~/shared/php-beers/ /var/www/html

Now, we need to make sure you can access the website served by your VM.

  • If you can get to your VM’s GUI directly (e.g., if you run VirtualBox and have installed GUI for it), you can simply start a Web browser inside your VM, and access the PHP website at http://localhost/php-beers/.
  • If you have (or prefer having) only shell access to your VM, you can access the PHP website from a browser running on your host as follows:
    • If you are running a local VM using Vagrant, follow the instructions at Creating and Running VM Using VirtualBox/Vagrant to set up forwarding between your guest port 80 and your host port 8080. You should then be able to access the PHP app at: http://localhost:8080/php-beers/.
    • If you are running a Google Cloud VM, follow the instructions at Creating and Running VM on Google Cloud to find your VM’s external IP address (say it’s 123.45.67.89, for example). You should be able to access the PHP application at http://123.45.67.89/php-beers/ (replace 123.45.67.89 with your VM’s public IP).

Working on the Website

Just go into the directory holding your copy of the website (NOT the original inside /opt/dbcourse/examples) and modify the files there. The website immediately reflects your changes (you might need to reload the page in your web browser).

For debugging, you will find the detailed Apache log in /var/log/apache2/error.log on your VM useful.

A Mini-Tutorial of PHP/PDO

The concept behind PHP (or any other server-side dynamic page generation language) is very simple: Instead of serving a static HTML page, the web server executes a piece of code to generate the HTML output to serve to the client. For PHP, such pieces of code are enclosed within <?php ... ?>.

The syntax of PHP resembles Java and C++, so it should be fairly easy to pick up. You can start with a simple tutorial, or you just can dive into the .php files in our example, which are fairly well documented.

PDO is a standard PHP extension that allows PHP code to work with a database server. Note that statement and connection objects will be automatically closed when a page finishes, so the code is simple. Note also the use of exceptions to catch database errors.

The file /etc/php5/pdo-beers.php stores information necessary for your PHP application to connect to the local PostgreSQL database via PDO. This file should be included by any .php file that needs to connect to the database. Examples of talking to the database via PDO can be found in our code.

  • You can create one such file for connecting to your own database as well. Just make a copy of pdo-beers.php and modify it to suit your purpose. Keep in mind that you need root permission on your VM to write under the /etc/php5/ directory, so prefix your copy and edit commands with sudo as follows:
    sudo cp /etc/php5/pdo-beers.php /etc/php5/pdo-mine.php
    sudo nano /etc/php5/pdo-mine.php

Note that our example is very much quick-and-dirty in that our PHP code spills out low-level HTML code directly. Web applications in the real world tend to be more complex—to simplify development and maintenance of these applications, people have developed “frameworks” where the code is more neatly partitioned along the lines of logic vs. presentation. We have some other example applications implemented in such frameworks—explore other links under “Help” for details. You are also welcome to explore PHP-based frameworks yourself.

Additional Information

See PHP5 documentation.