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 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:
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
#!/packages/run/php/bin/php line is generally not appropriate to insert into files that the application uses with the
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>
.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).
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
DirectoryIndex index.php AddHandler fastcgi-script .php AddType application/x-httpd-php .php
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
To test the new version, change the first line in your scripts to:
/packages/run/php/bin/php will then be switched to the new version so you can change
php-next back to
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
When you subsequently test execution from the web:
End of script output before headers(in the file
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
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