Joeri Verdeyen bio photo

Joeri Verdeyen

Web-engineer, cyclist, Nespresso lover, Strava pusher.

Twitter LinkedIn Instagram Github Stackoverflow Last.fm Strava

Install PHPNG (next generation) on Ubuntu 14.04 Trusty Tahr

You might have heard of PHPNG (next generation). PHPNG is the new approach to dramatic speedup website performance, it aims to better performance and memory usage efficiency to PHP. It is a new PHP branch based on a refactored Zend Engine.

Spin up a Digital Ocean Droplet

The easiest (and probalby fastest) way to test PHPNG, is to spin up a new Digital Ocean droplet with a Ubuntu 14.04 x64 Linux Distribution.

Install required packages

Install some packages to build PHPNG.

Note: All of the following shell commands should be executed as root, or prefixed with sudo.

apt-get install autoconf automake libtool re2c flex make libxml2-dev libssl-dev libbz2-dev libcurl3-dev libdb5.1-dev libjpeg-dev libpng-dev libXpm-dev libfreetype6-dev libt1-dev libgmp3-dev libc-client-dev libldap2-dev libmcrypt-dev libmhash-dev freetds-dev libz-dev libmysqlclient15-dev ncurses-dev libpcre3-dev unixODBC-dev libsqlite-dev libaspell-dev libreadline6-dev librecode-dev libsnmp-dev libtidy-dev libxslt-dev libt1-dev openssl openssl-dev libssl libssl-dev libxml2 libxml2-dev libpspell-dev libicu-dev

Install correct version of bison

Since the distro package of bison1 isn’t the correct version, you need to build it from source.

mkdir ~/tmp
cd ~/tmp
wget http://ftp.gnu.org/gnu/bison/bison-2.4.tar.gz
tar -xzf bison-2.4.tar.gz
cd bison-2.4
./configure
make && make install
cd ../

Clone PHPNG source

Clone the PHPNG source repository and checkout the phpng branch.

cd ~/tmp
git clone git@git.php.net:php-src.git
cd php-src
git branch phpng origin/phpng
git checkout phpng

Build and install PHPNG

./buildconf
./configure \
   --with-config-file-path=/etc \
   --enable-mbstring \
   --enable-zip \
   --enable-bcmath \
   --enable-pcntl \
   --enable-ftp \
   --enable-intl \
   --enable-exif \
   --enable-calendar \
   --enable-sysvmsg \
   --enable-sysvsem \
   --enable-sysvshm \
   --enable-wddx \
   --with-curl \
   --with-mcrypt \
   --with-iconv \
   --with-pspell \
   --with-gd \
   --with-jpeg-dir=/usr \
   --with-png-dir=/usr \
   --with-zlib-dir=/usr \
   --with-xpm-dir=/usr \
   --with-freetype-dir=/usr \
   --with-t1lib=/usr \
   --enable-gd-native-ttf \
   --enable-gd-jis-conv \
   --with-openssl \
   --with-mysql=/usr \
   --with-pdo-mysql=/usr \
   --with-gettext=/usr \
   --with-zlib=/usr \
   --with-bz2=/usr \
   --with-recode=/usr \
   --with-mysqli=/usr/bin/mysql_config

make && make install

Create php.ini

Create a new file /etc/php.ini and add the following lines:

 1 max_execution_time=600
 2 memory_limit=128M
 3 error_reporting=0
 4 display_errors=0
 5 log_errors=0
 6 user_ini.filename=
 7 realpath_cache_size=2M
 8 cgi.check_shebang_line=0
 9 
10 zend_extension=opcache.so
11 opcache.enable_cli=1
12 opcache.save_comments=0
13 opcache.fast_shutdown=1
14 opcache.validate_timestamps=1
15 opcache.revalidate_freq=60
16 opcache.use_cwd=1
17 opcache.max_accelerated_files=100000
18 opcache.max_wasted_percentage=5
19 opcache.memory_consumption=128
20 opcache.consistency_checks=0
21 date.timezone = Europe/Brussels

Test installation

Check if your installation succeeded:

$ php -v
PHP 5.7.0-dev (cli) (built: Jul 17 2014 16:56:38)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.7.0-dev, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies

You can try running composer update on a project and see the performance gain!

Run FastCGI server using CGI SAPI or FPM

Create a shell script (fpm.sh) with the following lines:

vim /etc/fpm.sh
1 #!/bin/bash
2 PHP_FCGI_CHILDREN=4 PHP_FCGI_MAX_REQUESTS=0 php-cgi -b /tmp/fcgi-php

Executing that will create a socket on /tmp/fcgi-php.

  • PHP_FCGI_CHILDREN will launch 4 child processes to actually handle the requests.
  • PHP_FCGI_MAX_REQUESTS tells how many requests to serve before respawning fcgi, 0 means unlimited.
chmod +x /etc/fpm.sh
nohup ./etc/fpm.sh &

PHPNG and Nginx

You can create a simple virtual host using your new socket to process php.

 1 upstream phpfcgi {
 2   server unix://tmp/fcgi-php;
 3 }
 4 
 5 server {
 6   listen   80;
 7 
 8   root /var/www/web;
 9   server_tokens off;
10 
11   index index.php;
12 
13   location ~* \.php$ {
14     try_files $uri =404;
15     fastcgi_pass    phpfcgi;
16 
17     fastcgi_split_path_info ^(.+\.php)(/.*)$;
18     include fastcgi_params;
19     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
20     fastcgi_param HTTPS off;
21   }
22 }

Enjoy your blazing fast PHPNG setup!

  1. Bison is a general-purpose parser generator that converts an annotated context-free grammar into a deterministic LR or generalized LR (GLR) parser employing LALR(1) parser tables.

Thanks for reading

Feel free to leave a comment if you have remarks or like this post