Guide to using PHP at FI
Before reading the following text, we recommend that you familiarise yourself with the guide on
User HTML Pages.
We assume that the directory~/public_html/ exists with the correct permissions set
and that you are able to access static pages.
CGI
CGI is a simple interface that can be used in any programming language. In this guide, 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, which we describe below, instead of CGI.
First, let’s create a directory for our new application:
mkdir ~/public_html/php-app
We’ll change into the new directory and save a test application that displays the current time in the file `index.php `:
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 standard script, which on Unix-based operating systems means
that the first line must contain, after the ‘#! ’ prefix, the path to the interpreter,
in our case/packages/run/php/bin/php. To ensure it can be executed,
we must also set the execute permission for the owner:
chmod u+x index.php
Note: it is generally not advisable to include the line `#!/packages/run/php/bin/php ` in files
that the application uses via the `include ` or `require` directives.
You can now 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, using the configuration file.htaccess, we’ll tell the Apache web server
that files ending in.php 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 check athttps://www.fi.muni.cz/~xlogin/php-app/ that the application is working (replacexlogin with your own username).
FastCGI
This alternative offers greater speed because, unlike CGI, scripts are not restarted with every request but handle multiple requests. This eliminates the latency caused by constantly restarting the entire PHP runtime environment.
If you wish to use FastCGI, edit the `AddHandler` directive in `.htaccess ` as follows:
DirectoryIndex index.php
AddHandler fastcgi-script .php
AddType application/x-httpd-php .php
PHP Upgrades
The PHP language is constantly evolving and older versions are no longer supported,
so we need to upgrade to a newer, usually backwards-incompatible, version from time to time.
To make it easier for you to prepare for the upgrade, the new version of PHP is available
at/packages/run/php/bin/php-next.
If you wish to test the new version, change the first line of your scripts to:
#!/packages/run/php/bin/php-next
We will subsequently switch the default/packages/run/php/bin/php to the new version,
so you will be able to changephp-next back tophp.
Currently, bothphp-next and the defaultphp use version 8.5.
Documentation of backwards-incompatible changes:
Please pay particular attention to the ‘Backward Incompatible Changes’ section in these guides!
What to do if you encounter problems
The Apache logs are the primary source of detailed information about errors.
When debugging errors, it is preferable to use CGI rather than FastCGI. The error logs are then clearer and changes to the script code will be reflected on the website more quickly.
Below are examples of possible errors ifindex.php fails to run.
We recommend testing from the command line first:
-bash: ./index.php: Permission denied
The PHP script apparently does not have execution permissions set by the owner; run 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 uses DOS-style line endings. You can convert it to Unix-style line endings using the commanddos2unix index.php.
When subsequently testing 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, particularly the script’s access permissions. Any specific issues will likely be logged in the file `/var/log/httpd-user/suexec`.
Encoding
The default encoding is set toUTF-8, which applications
should use for both programme code and any HTML files.
In special cases, you can set the encoding in PHP (e.g. toISO-8859-2)
by adding this statement at the start of your programme:
<?php ini_set('default_charset', 'ISO-8859-2'); ?>
The encoding for HTML files can be set in the.htaccess file:
AddDefaultCharset ISO-8859-2