The software installation instructions configured a LAMP (Linux, Apache, MySQL, and PHP) stack that you can use for web development, including server-side scripting and database connectivity. As part of the configuration, Apache has been configured with per-user web directories.

Content stored in the public_html directory in your home directory (you’ll see this directory if you open the terminal and type ls to list directory contents) will be served by Apache. Open Firefox and enter http://localhost/~student to have Apache display the directory contents, which it does by default when there isn’t an index page (traditionally index.html or index.php). Note: If your username isn’t “student,” then you’ll need to replace “student” in the URL with your username.

Create a new file in ~/public_html (the tilde is shorthand for your home directory) named test.html. The GNOME text editor, gedit, can be used to edit files and supports syntax highlighting as well as a host of other features. Enter the following text in the test.html file and save it:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Test HTML Page</title>
  </head>

  <body>
    Hello, World!
  </body>
</html>

Enter http://localhost/~student/test.html in the web browser’s address bar (be sure to do this in the virtual machine (VM), not the host machine!) to verify that Apache is serving files correctly, and don’t forget to replace “student” with your username. You should see “Hello, World!” – contact the instructor if you don’t see this text.

If Apache is not serving files correctly, then you will not be able to complete the examples in upcoming lessons or the web development programming exercise (PEX).

Displaying PHP Errors

Regrettably, PHP’s default configuration disables error reporting, which is recommended for production deployment but not for development. In fact, error reporting is essential when learning PHP because otherwise syntax errors simply result in a blank web page!

To enable PHP error reporting for Apache, you must do the following:

  1. Modify the PHP configuration (php.ini)

    Open the following file using a text editor (e.g., vi or gedit): /etc/php/7.2/apache2/php.ini. Modifying this file requires administrative privileges so prefix your comment using sudo – e.g.,

    sudo vi /etc/php/7.2/apache2/php.ini
    

    You will be prompted for your password before the file is opened.

    Make the following change to that file:

    --- /etc/php/7.2/apache2/php.ini~	2020-03-31 19:03:27.521707388 -0600
    +++ /etc/php/7.2/apache2/php.ini	2020-03-31 19:03:49.814555381 -0600
    @@ -471,7 +471,7 @@
     ; Development Value: On
     ; Production Value: Off
     ; http://php.net/display-errors
    -display_errors = Off
    +display_errors = On
    
     ; The display of errors which occur during PHP's startup sequence are handled
     ; separately from display_errors. PHP's default behavior is to suppress those
    

    That is, you will change “Off” to “On” and then save the file. (You may want to open the file again to make sure that your change was saved.)

  2. Reload the Apache web server using the following command:

    sudo service apache2 reload
    
  3. Verify that errors are being reported. Create a PHP file named error.php in your public_html directory with the following contents:

    <?php
    
    echo "Error!';
    

    The syntax error (i.e., terminating the string with a single quote instead of a double quote) is intentional. Use your web browser to access that file by entering http://localhost/~student/error.php (and remember to replace “student” with your username!).

    You should see the following error message:

    Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in /home/student/public_html/error.php on line 5

    While it may not seem like the most informative error message, at least it’s clear that the script contains a syntax error.

    If you only see a white screen (i.e., a blank web page), then error reporting is not configured correctly. Contact the instructor for assistance.

Ideally this configuration should have occurred when installing PHP so if anyone wants to contribute a pull request with the change (see corresponding issue), it would certainly qualify for extra credit.