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
- Setup CGI access: You must first have requested and been approved for personal CGI script access.
- 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 yourcgi-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
). - 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 thephp
script. Edit (see why) thedoc_root
line. - 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 yourphp.ini
:
extension=pdo.so extension=pdo_mysql.so extension=pdo_pgsql.soYou 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
- 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 yourcgi-bin
files, in which case you could
mkdir ~/public_html/php
and setdoc_root
to be
/home/username/public_htm/php
- 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 setdoc_root
to be
/home/username/php
Renaming the example script
In order to prevent the current Wiki from possibly trying to execute the example scripttest.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 simplymv test.php.txt test.php
once you have downloaded the file.