PHP
Overview
PHP is a server-side programming language. Common usage includes writing dynamic webpages by loading content server-side before serving to the web client.
Traditionally PHP is processed by a PHP handler interfaced from the web server using CGI (protocol/interface for webservers to communicate with), or as standalone interpreters, e.g. mod_php
in Apache web servers.
PHP-FPM is a PHP handler daemon that runs as a service instead, which modern web servers like nginx can pass PHP scripts to for server-side processing. This architecture tends to be a bit more lightweight and fast due to it's FastCGI-based implementation.
See this for differences between PHP handler types, and this for differences between CGI and FastCGI.
Installation
Installation for php-fpm
(usually in combination with nginx) on Ubuntu is performed using:
sudo apt install php-fpm
This method installs only the latest PHP (8.1 as of 2022-12-26), which may cause compatibility issues with older software. For older versions, e.g. 7.4, load it from another repository:
sudo apt-get install software-properties-common sudo add-apt-repository -y ppa:ondrej/php sudo apt update sudo apt install php7.4-fpm
To check the status of the PHP service:
sudo systemctl status php8.1-fpm
php-fpm is not the php package
Pure PHP CLI is installed via the php
package instead, which also apparently happens to load the whole Apache web server as well. php-fpm
is a standalone package separate from php
.
Extensions for php
is typically installed via the same package manager, with the name of the extension appended to php-
, e.g. php-xml
.
Configuration
The behaviour of PHP is modifiable by editing the /etc/php/X.X/php.ini
config.
Some common values pertaining to script execution and file uploads one might consider adjusting (listed are the defaults):
max_execution_time = 30 # per script max_input_time = 60 # per script, for parsing data memory_limit = 128M # per script post_max_size = 8M # size of POST request allowable file_uploads = On # HTTP file uploads upload_max_filesize = 2M # size of uploaded files allowable max_file_uploads = 20 # per request
Note also the injection of deprecated "Pragma" caching headers by PHP, which can also be removed:
session.cache_limiter = nocache
Debugging
For simple strings, debugging messages can be generated by printing to the browser with either echo
or print
.
For arrays, use the var_dump
function.
Errors can be reported by issuing the following commands at the start of the PHP block, as suggested by this SO post:
error_reporting(-1); ini_set('display_errors', 'On');