Set up PHP 7 development environment

Setup requirements

Install VirtualBox

Install Vagrant

Verify all requirements

Open the terminal and try this command to verify Virtualbox is ready.

1
$ Virtualbox -h

Open the terminal and try this command to verify Vagrant is ready.

1
$ vagrant --version

Pull PHP 7 box source code

Clone this repository from Github :

1
$ git clone https://github.com/khanhicetea/phpbox

Provision the box

1
2
$ cd phpbox
$ vagrant up

Note : First provision will take a long time to build the box (about 30-45 minutes). But next time you run the box, it only takes 2 mins.

Development guideline

SSH to the box

Run this command in phpbox folder

1
$ vagrant ssh

Point your virtualhosts to Box IP

Your PHPBox IP address is 192.168.67.101, you can change in Vagrantfile

Open /etc/hosts file in your PC to map virtualhosts IP to PHPBox

1
$ sudo vi /etc/hosts

Each line in this file will have format (IP HOSTNAME). Eg:

1
2
192.168.67.101 test.dev
192.168.67.101 php7iscool.dev

Folder structure

PHPBox Structure

  • conf : Configuration
    • modules.ini : PHP module configuration
    • nginx_default : Nginx default host
    • php-fpm.conf : PHP7-FPM configuration
    • vhosts_apache.conf : Apache virtualhosts (if you use Apache)
    • vhosts_nginx : Nginx virtualhosts
    • www.conf : PHP7-FPM worker configuration
  • scripts : provision scripts
  • www : Web root
    • default : document root of default host 192.168.67.101
    • test : document root of test host test.dev
  • .gitignore : gitignore file
  • Vagrantfile : Vagrantfile of PHPBox

Create a new virtualhost

  • Duplicate a server section in vhosts_nginx or VirtualHost in vhosts_apache.conf
  • Edit the server name and document root
  • Pointing servername to PHPBox IP via hosts file
  • Restart web server sudo service nginx restart or sudo service apache2 restart

Utils

Composer, Git, Docker is ready

This box included helpful tools like composer, git, docker, redis-server, htop, vim, screen and zsh.

PHPMyAdmin or Adminer

  • Adminer (default) : http://192.168.67.101/adminer
  • PHPMyAdmin : http://192.168.67.101/phpmyadmin

Services

  • MySQL user : root / passwd
  • Redis : localhost:6379

Installation config

You can open the scripts/provision/setup.sh to modify some variables to modify some packages.

Ref

PHP 5.6 vs PHP 7RC8 - Benchmarking using Docker

New version of PHP, PHP7 has been released yesterday after 11 years of PHP5. I am very excited with it, so I made a benchmark to compare the performance of 2 versions.

The fastest way to test out PHP multi-versions is using Docker image. You can get it from : https://hub.docker.com/_/php/

Let’s start pull PHP images

1
2
docker pull php:5.6
docker pull php:7

Change directory to the web root folder of PHP project (I used my micro-framework, Sifoni to test). And add this file server.php to web folder with content

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php

error_reporting(0);

$filename = __DIR__.preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']);
if (php_sapi_name() === 'cli-server' && is_file($filename)) {
return false;
}

define('ROOT_PATH', dirname(dirname(__FILE__)));

$autoloader = require_once (ROOT_PATH . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');

\Sifoni\Engine::getInstance()->init()->bootstrap(array(
'path.root' => ROOT_PATH,
'autoloader' => $autoloader,
))->start()->run();

Next, run this command to run with PHP 5.6

1
docker run -it -p 8080:8080 -v `pwd`:/code php:5.6 php -S 0.0.0.0:8080 -t /code/web /code/web/server.php

Open http://localhost:8080/ to see the memory and processing time of PHP5.6.

Continue with PHP7, exit the PHP 5.6 session and run this command

1
docker run -it -p 8080:8080 -v `pwd`:/code php:7 php -S 0.0.0.0:8080 -t /code/web /code/web/server.php

Open http://localhost:8080/ to see the memory and processing time PHP7.

This is my result :

PHP 5.6 benchmarking result

PHP PHP 7 RC8 benchmarking result

Amazing… PHP 7 is winner in Saving Memory and Processesing Time (about 1.4x times than PHP 5.6).

At the moment I tested, official docker image of PHP is PHP7RC8. Hope image maintainer updates it soon :)