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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
max_execution_time=600
memory_limit=128M
error_reporting=0
display_errors=0
log_errors=0
user_ini.filename=
realpath_cache_size=2M
cgi.check_shebang_line=0

zend_extension=opcache.so
opcache.enable_cli=1
opcache.save_comments=0
opcache.fast_shutdown=1
opcache.validate_timestamps=1
opcache.revalidate_freq=60
opcache.use_cwd=1
opcache.max_accelerated_files=100000
opcache.max_wasted_percentage=5
opcache.memory_consumption=128
opcache.consistency_checks=0
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
2
#!/bin/bash
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
upstream phpfcgi {
  server unix://tmp/fcgi-php;
}

server {
  listen   80;

  root /var/www/web;
  server_tokens off;

  index index.php;

  location ~* \.php$ {
    try_files $uri =404;
    fastcgi_pass    phpfcgi;

    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS off;
  }
}

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