PHP cheatsheet

build PHP from source

ref: How to Run Multiple Versions of PHP on One Server

PHP Git Access


    $ ./bin/php --ini
    Configuration File (php.ini) Path: /opt/php-5.6/lib
    Loaded Configuration File:         /opt/php-5.6/lib/php.ini
    Scan for additional .ini files in: (none)
    Additional .ini files parsed:      (none)

edit php-fpm config (refer to following section of this doc):

    sudo vi etc/php-fpm.conf

make sure:

    user = www-data
    group = www-data

    listen =

install PHP 5.6 or to upgrade from PHP 5.5 on Ubuntu 14

apt-get -y update
add-apt-repository ppa:ondrej/php
apt-get -y update
apt-get -y install php5.6 php5.6-mcrypt php5.6-mbstring php5.6-curl php5.6-cli php5.6-mysql php5.6-gd php5.6-intl php5.6-xsl php5.6-zip

it also installs a php5.6 module for apache libapache2-mod-php5.6, you need to enable it

sudo a2dismod php5
sudo a2dismod php5.6
sudo service apache2 restart

looks like pecl doesn't work with multiple php versions, just download the source file and compile it, install memcached in php5.6, make sure use phpize5.6 when compiling

make php-fpm work with Apache

ref: Httpd Wiki - PHP-FPM

PHP FPM installation


from 5.3.3, PHP includes the fastCGI process manager (php-fpm) in the stock source code, your distribution or OS will either include it in the stock PHP package, or make it available as an add-on package

you can build it from source by adding --enable-fpm to your ./configure options, other options include --with-fpm-user, --with-fpm-group

in Ubuntu, there is a separate package php5-fpm, its config files are at /etc/php5/fpm/

global config is in php-fpm.conf, each process pool should got a separate config in pool.d folder, do the following config in www.conf

user = www-data
group = www-data

listen =

start php-fpm

sudo service php5-fpm start


apache httpd 2.4 introduced a new proxy module for fastCGI (modproxyfcgi), and moved to event MPM as process manager

enable the modules:

sudo a2enmod proxy proxy_fcgi
sudo service apache2 restart

create a vhost config file /etc/apache2/sites-available/fpm.local.conf, add config:

ServerName fpm.local

DocumentRoot /var/www/fpm
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://$1
DirectoryIndex /index.php index.php

enable config and restart apache:

sudo a2ensite fpm.local
sudo service apache2 restart

Done! try visit


Server API should be 'FPM/FastCGI'


When using DOMDocument, add a meta tag to make sure your HTML file is treated as 'UTF-8' encoded, this will cause a 'misplaced tag' warning, suppress with '@'

$doc = new DOMDocument();
@$doc->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">' . $html);

mod_php vs. FastCGI vs. PHP-FPM


install php-fpm


$ sudo apt-get install php5-fpm

# config files are at /etc/php5/fpm/

# restart php fpm
$ sudo service php5-fpm restart

php arrays

array elements order:

$a = array(1=>'1', 0=>0, 2=>2);

foreach ($a as $value) {
    echo $value . ' ';


1 0 2

php resources


php coding styles


it runs every 30 minutes, and clears every session files older than session.gc_maxlifetime (24mins by default), so by default all sessions get cleared around 30 mins