Technical Note - Personal PHP

Summary

PHP is a popular web scripting language. ECS does allow for PHP scripts to be run, but there are some steps that need to be taken for it to work. This document lists those steps.

Details

Getting PHP going

  1. Setup CGI access: You must first have requested and been approved for personal CGI script access.
  2. Copy the PHP CGI script: All URLs of the form http://homepages.ecs.vuw.ac.nz/~username/*.php are translated to call a PHP CGI script in your cgi-bin directory. Download this file and place it in your ~/public_html/cgi-bin/ directory. Set the permissions so that it is executable by you (chmod 700 ~/public_html/cgi-bin/php).
  3. Create a php.ini file: We recommend you download the recommended php.ini file and save that in your ~/public_html/cgi-bin directory, alongside the php script. Edit (see why) the doc_root line.
  4. Testing: Download this example script into your doc_root, then rename it (see why) and try viewing it in your browser.

Database access

If you already have access to ECS databases and wish to access them from PHP, you will need to add database modules to your PHP environment, a capability that is obtained by placing the following lines in your php.ini:
extension=pdo.so
extension=pdo_mysql.so
extension=pdo_pgsql.so

You should then be able to access MySQL and PostgreSQL databases, though other modules may be required depending on your application (and would be added to the php.ini in a similar manner).

If you have difficulties, please send a ticket to jobs@ecs.vuw.ac.nz.

Notes

What's this doc_root thing?

You will recall that the URLs of your own PHP scripts are translated through a script in your cgi-bin directory. What is less apparent is that the PHP environment will actually look to find any scripts below a doc_root directory specified in your own php.ini file.

This means that a URL of the form

http://homepages.ecs.vuw.ac.nz/~username/myprojects/project001/scripts/do_something.php

will actually invoke the script that lies at

path_to_doc_root/myprojects/project001/scripts/do_something.php

where path_to_doc_root is a directory path within your filestore and need not be related to the top level directory for your personal website files.

The default value of doc_root in the php.ini file you will have downloaded and placed in your ~/public_html/cgi-bin/ directory is currently the generic

/home/username/public_html

though it is probably best thought of as simply being your ~/public_html directory, which is the the default directory from which ALL your HTML files are normally served.

PHP scripts, like all personal CGI scripts, run as you. If you have files in your public_html that aren't for public consumption, you should probably change your doc_root to a different directory so that any unqualified indirection resulting from use or misuse of your PHP scripts will not necessarily expose files below your public_html directory.

Some PHP doc_root options

  1. You may wish to have all of your PHP script paths to start one level below your public_html directory so as to have all website-related stuff in one directory hierachy, as with your cgi-bin files, in which case you could
    mkdir ~/public_html/php
    and set doc_root to be
    /home/username/public_htm/php
  2. You may wish to remove all of your PHP script paths to a directory hierachy outside of your personal website-related stuff, in which case you could
    mkdir ~/php
    and set doc_root to be
    /home/username/php

Renaming the example script

In order to prevent the current Wiki from possibly trying to execute the example script test.php when the link is clicked on, it has been given the name test.php.txt. If you wish to be able to run the script it must be renamed to have .php at the end. You may wish to do this renaming at download time or simply
mv test.php.txt test.php
once you have downloaded the file.
Topic attachments
I Attachment Action Size Date Who Comment
phpEXT php manage 302 bytes 07 Mar 2008 - 16:12 Main.cflewis PHP CGI script
php.iniini php.ini manage 27 K 27 Oct 2016 - 11:02 Main.mark Recommended php.ini
test.php.txttxt test.php.txt manage 67 bytes 07 Mar 2008 - 16:11 Main.cflewis Test PHP script