Instructions for using PHP on FI
We recommend that you read the
User HTML Pages tutorial before reading the following. We assume that there is a directory
~/public_html/
with the correct permissions and that access to static pages works for you.
CGI
CGI is a simple interface that can be used in any programming language. In this tutorial, we'll show you how to use it to write simple web applications in PHP. If you're serious about using PHP, it might make more sense to use FastCGI instead of CGI, which we describe below.
First, we'll create a directory for our new application:
mkdir ~/public_html/php-app
Switch to the new directory and save the test application to the file
index.php
to list the current time:
cd ~/public_html/php-app
cat > index.php <<'EOF'
#!/packages/run/php/bin/php
<html>
<head><title>Time App</title></head>
<body>
<?php
echo strftime("%H:%M:%S\n");
?>
</body>
</html>
EOF
This is a regular script, which on Unix operating systems means that the first line must have the path to the interpreter, in our case
/packages/run/php/bin/php
, after the
#!
character pair. In order to execute it, we still need to set the owner to execute it:
chmod u+x index.php
Note: the line
#!/packages/run/php/bin/php
is generally not appropriate to insert into files that the application uses with the
include
or
require
directives.
Now you can test the script by running it from the command line, it will output some HTTP headers and the resulting HTML code:
./index.php
X-Powered-By: PHP/1.2.3
Content-type: text/html; charset=UTF-8
<html>
<head><title>Time App</title></head>
<body>
16:20:03
</body>
</html>
Finally, the
.htaccess
configuration file tells the Apache web server that files with the
.php
extension should be run as CGI scripts:
cat > .htaccess <<'EOF'
DirectoryIndex index.php
AddHandler cgi-script .php
AddType application/x-httpd-php .php
EOF
You can now go to
https://www.fi.muni.cz/~xlogin/php-app/
to verify that the application works ( replace
xlogin
with your own username).
FastCGI
This alternative provides more speed because, unlike CGI scripts, it does not re-execute on every request, but serves multiple requests. This eliminates the latency caused by constantly running the entire PHP runtime.
If you want to use FastCGI, edit the
AddHandler
directive in
.htaccess
:
DirectoryIndex index.php
AddHandler fastcgi-script .php
AddType application/x-httpd-php .php
PHP Upgrades
The PHP language is constantly evolving and old versions are no longer supported, so every once in a while we have to upgrade to a newer, usually backwards incompatible, version. To make it easier for you to prepare for the upgrade, a new version of PHP is available at
/packages/run/php/bin/php-next
.
To test the new version, change the first line in your scripts to:
#!/packages/run/php/bin/php-next
The default
/packages/run/php/bin/php
will then be switched to the new version so you can change
php-next
back to
php
.
Currently
php-next
and the default
php
are both version
8.2.
Documentation of backwards incompatible changes:
Pay particular attention to the Backward Incompatible Changes section in these instructions!
What to do in case of problems
Apache logs are the primary source of detailed information about errors.
When debugging errors, it is preferable to use CGI instead of FastCGI. Error logs are then more clear and changes to the script code will be reflected on the site more quickly.
The following are examples of possible errors if
index.php
fails. We recommend testing from the command line first:
-
-bash: ./index.php: Permission denied
The PHP script does not seem to have the right to run by the owner, execute the commandchmod u+x index.php
. -
-bash: ./index.php: /packages/run/php/bin/php^M: bad interpreter: No such file or directory
PHP script has DOS line breaks. It can be converted to Unix line breaks with the commanddos2unix index.php
.
When you subsequently test execution from the web:
-
End of script output before headers
(in the file/var/log/httpd-user/xlogin.log
)
Check that you meet the conditions required by the suexec function, in particular the access rights of the script. The specific issue will probably be logged in the file/var/log/httpd-user/suexec
.
Encoding
The default encoding is set to
UTF-8
, which applications should use for both program code and any HTML files.
In special cases, you can set the encoding in PHP (e.g., to
ISO-8859-2
) by adding this statement to the beginning of the program:
<?php ini_set('default_charset', 'ISO-8859-2'); ?>
HTML files can have their encoding set to
.htaccess
:
AddDefaultCharset ISO-8859-2