translated by Google

Machine-translated page for increased accessibility for English questioners.

Instructions for using PHP on FI

We recommend that you read the instructions before reading the following text Custom HTML pages . We assume that a directory exists ~/public_html/ with the rights set correctly and you have access to static pages.

CGI

CGI is a simple interface that can be used in any programming language. In this tutorial we will show you how to use them for writing simple web applications in PHP. If you're serious about using PHP, it may make more sense to use FastCGI, which we describe below, instead of CGI.

First we will create a directory for our new application:

mkdir ~/public_html/php-app

Switch to the new directory and file index.php save the test application for listing 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 common script, which on Unix operating systems means that it must have a pair of characters on the first line #! the path to the interpreter, in our case /packages/run/php/bin/php . In order to do this, we still need to set it to run by the owner:

chmod u+x index.php

Note the line #!/packages/run/php/bin/php it is usually not advisable to embed in files that the application uses with directives include or require .

Now you can test the script by running it from the command line, 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 configuration file .htaccess we tell the Apache web server that the files end in .php to 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/ verify that the application works ( xlogin replace with your own username).

FastCGI

This alternative provides more speed because, unlike CGI scripts, they are not re-run on each request, but handle more. This eliminates the latency caused by constantly running the entire PHP runtime environment.

If you want to use FastCGI, edit in .htaccess directive AddHandler :

DirectoryIndex index.php
AddHandler fastcgi-script .php
AddType application/x-httpd-php .php

PHP upgrades

PHP is constantly evolving and old versions are no longer supported, so we have to upgrade to a newer, usually backward incompatible, version once in a while. To make it easier for you to prepare for the upgrade, the new version of PHP is available under the name /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

Implicit /packages/run/php/bin/php then we'll switch to the new version so you can change it php-next back to php .

Do not use a specific version of PHP (e.g. /packages/run.64/php-7.3.2/bin/php-cgi ) may be revoked at any time without notice.

It is currently php-next version 7.4 , while the default php is version 7.3 .

How to proceed in case of problems

The basic source of more detailed information about errors is Apache logs .

When debugging errors, it is more convenient to use CGI instead of FastCGI. The error logs are then clearer and changes to the script code will be reflected on the web faster.

The following are examples of possible errors in the event of a failed startup index.php . We recommend testing from the command line first:

  • -bash: ./index.php: Permission denied
    The PHP script probably does not 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
    The PHP script has DOS line terminations. It can be converted to Unix line breaks with the command dos2unix index.php .

For subsequent testing of launching from the web:

  • End of script output before headers (in the file /var/log/httpd-user/xlogin.log )
    Make sure you meet conditions required by the suexec function, especially script access rights. The specific problem will probably be logged in the file /var/log/httpd-user/suexec .

Coding

The default encoding is set to UTF-8 which applications should use both for the program code and for any HTML files.

In special cases, you can set the encoding in PHP (eg 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 encode in the file .htaccess :

AddDefaultCharset ISO-8859-2