running php via the web
(served by, say, itec-php01.radford.edu)
Reminder:
We previously saw
a review of essential linux/unix commands,
how to connect to rucs,
and
how to run php programs from the command line.
You should be familiar with those, before proceding.
Recall that when a web-server gets a request, it decides how to respond.
For itec-php01.radford.edu, some of the configuration rules:
For requests of the form ~userName/somePath/someFile.php,
it looks on
userName's H-drive,
for the file
dynamic_php/somePath/someFile.php,
and it runs that .php program.
The program's printed output becomes the response sent back to the browser.
The .php file must be readable by everybody (since it's being served up to the www),
and it must be non-writable by group,other
(as a security measure: other users can't implant malware into your php program).
Apache runs php with the privileges of the file's owner (su).
itec-php01.radford.edu only accepts secure (https) connections.
Place the .php file inside dynamic_php/ on your H: drive.
You may have to create that directory if it doesn't already exist, using mkdir.
Make sure the file is group-and-world-readable, but not group-or-world-writable:
chmod o+r blend.php ; chmod o-w blend.php.
(This makes it other-readable, non-group-writable, without changing any other permissions.
You could instead use go-w and ugo+r to additionally ensure that it
is non-group-writable, and readable by user,group,other;
however those are usually already part of the standard default.)
recall:
You did review unix-permissions and chmod a couple weeks back(running-php-from-rucs.html),
right?
And if necessary you looked up some tutorial
whose chapter 5 reviews all this, right?
Make sure any file you include is also readable, but not writable.
chmod 644 *.php is a reasonable default.
Make sure all the folders enclosing your file are executable (go+x) but not
group-writable (go-x).
This includes the folder ~/dynamic_php itself, as well as your home-directory ~.
So for a file like ~/dynamic_php/itec325/hw02/foo.php, you might ensure:
And you can check the permissions via ls -ld, even combining the shared-prefixes:
ls -ld ~/{,dynamic_php/{,itec325/{,hw02/{,foo.php}}}}
One crude way to get things to work:
Set every file to be rwx r-x r-x
(whether or not it's a folder, .html file, or .php file):
Recursively change permissions:
chmod -R u=rwx,go=rx ~/dynamic_php/
or (using the octal representation of the user/group/other permissions, 111 101 101) just
chmod -R 755 ~/dynamic_php/.
This is not ideal because it makes the directories readable
(susceptible to people rooting around and looking for files you don't mean them to have and
letting them clearly see any permissions-snafus you might have)1.
If the php interpreter hits an error, the program terminates with no output
(so nothing is sent back to the client —
status code 500: internal server error)!
Note that this often happens when php is simply first reading/parsing your file,
(for example, a missing semicolon).
So even if you have error_reporting(E_ALL); in the first line of your program,
you'll still get back an empty, 500:internal-server-error response.
To fix this: run you php from the command-line, where syntax errors will generate a message
and a line-number!
Here is a quick check-list of common problems, if you try running your program via the web but
get an error:
Error 404 (Not Found):
Recall from quizzes:
Given the URL
http://itec-php01.radford.edu/~someUser/someDir/someFile.php,
the web-servers itec-php01.radford.edu will look for someUser's H: drive,
and within that look for dynamic_php/someDir/someFile.php.
(Remember to (a) put someDir/ inside your ~/dynamic_php/,
and (b) don't include “dynamic_php/” in your URL.)
Error 403 (Forbidden):
Make sure the permissions are correct on (a) the file itself, and
(b) its enclosing folder, and (c) the next-higher enclosing folder, (d) …
(y) the next-higher-yet enclosing folder,
(z) the enclosing ~/dynamic_php.
For example, check:
ls -ld ~/dynamic_php/itec325/hw00/someFile.php
ls -ld ~/dynamic_php/itec325/hw00/
ls -ld ~/dynamic_php/itec325/
ls -ld ~/dynamic_php/
and make sure that each of the files is readable/executable by others,
and not writable by group/others (e.g.rwxr-xr-x).
Error 500 (Server Error):
This is almost certainly due to your php program not being a valid PHP program
(perhaps it's missing a semicolon somewhere).
Solve this by running your program from the command-line (not via the web at all),
and seeing what the error-message is.
1
Setting everything to 755
also marks all your data-files as being executable.
Even .php programs aren't actually executable files;
they are read by the interpreter, rather than being callable directly.
↩