Translated using DeepL

Machine-translated page for increased accessibility for English questioners.

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 #!/packages/run/php/bin/php line 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.

Do not use a specific PHP version (e.g. /packages/run.64/php-7.3.2/bin/php-cgi), it can be deprecated at any time without notice.

Currently, php-next is version 8.0, while the default php is version 7.3. Document backwards incompatible changes:

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 are 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 command chmod 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 command dos2unix 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)
    Make sure you meet the conditions required by the suexec function, in particular the access rights of the script. The specific problem will probably be logged in the /var/log/httpd-user/suexec file.

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 command to the beginning of the program:

<?php ini_set('default_charset', 'ISO-8859-2'); ?>

HTML files can be set to the encoding in .htaccess:

AddDefaultCharset ISO-8859-2