Run latest PhantomJS with shell_exec PHP on Ubuntu 11.10 Oneiric


Running PhantomJS with exec() or shell_exec PHP and grab it results may lead you into several headache problems. Several web that talking how to run phantomjs in PHP just send me into fail results. I will show you several pitfalls for instance, here a small scripts to run PhantomJS :

test.php :

1
2
3
4
<?php
$result = shell_exec(‘phantomjs –version’);
echo $result;
?>


Running this php scripts through commandline using PHP CLI will give success page! But, not if you execute this script through browser (eg: Firefox, Chrome, IE). Running this scripts with Apache2 permission give you never ending loading / loop pages.

When you check apache2 error log with “tail -f /var/log/apache2/error.log”, you will see :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2011-12-22T21:18:47 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
No protocol specified
2011-12-22T21:18:47 [WARNING] phantomjs: cannot connect to X server :0
2011-12-22T21:18:47 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:47 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:47 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:47 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:47 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:47 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:48 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:48 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:48 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:48 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:48 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:48 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:48 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:48 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:48 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified
2011-12-22T21:18:48 [WARNING] phantomjs: cannot connect to X server :0
No protocol specified

Why this “cannot connect to X server :0” show up?

PhantomJS need Virtualframebuffer to run. You should run PhantomJS in virtualframebufer. You need XVFB installed in your Ubuntu :

1
sudo apt-get install xvfb xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic

Now, let change your scripts into :

test.php :

1
2
3
4
<?php
$result = shell_exec(‘DISPLAY=:0 phantomjs –version’);
echo $result;
?>

It’s should run, but not yet. You still got error here. Because apache2 run everything with “www-data” and it’s not have permission to execute with another user environment. So, another tricks is run it by bash script and execute from PHP. For instance :

test.sh (chmod +x to make it executable) :

1
2
3
#!/bin/bash
export DISPLAY=:0
phantomjs –version

test.php :

1
2
3
4
<?php
$result = shell_exec(‘sh test.sh’);
echo $result;
?>

You will got same error here. Why? Because bash script still running www-data to run phantomjs. Damn! So, what the solution?

There are two solution here.

First solution
You should add “xhost + ” before running everything.

1
xhost +

Now, when you run “test.php” which execute “test.sh”, apache2 host will allowed and run PhantomJS without errors. But, I don’t recommend this solution since there may security leaks when adding xhost +. All risk is in your fingers. ๐Ÿ™‚

The second solution ?
This is what recommended by me. You can run xvfb first by :

1
Xvfb :23 -screen 0 1024x768x24

And execute from shell_exec() php by :

1
2
3
4
<?php
$value = shell_exec(‘DISPLAY=:23 phantomjs –version’);
echo $value;
?>

I have spend a few hours to figure out how to run phantomjs successfully with PHP. Now, I share to you how to make it works! ๐Ÿ™‚

Updated!
We want to run Xvfb when system start. So, create “/etc/init.d/xvfb” :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash

if [ -z "$1" ]; then
echo "`basename $0` {start|stop}"
exit
fi

case "$1" in
start)
/usr/bin/Xvfb :99 -ac -screen 0 1024x768x8 &
;;

stop)
killall Xvfb
;;
esac

Don’t forget to register it by :

1
2
sudo chmod 755 /etc/init.d/xvfb && cd /etc/init.d
sudo update-rc.d xvfb defaults

Reboot your Ubuntu or you can start by “sudo service xvfb start” to start XVFB daemon. Next, check if Xvfb running by “ps aux | grep Xvfb”. Then you can pointing Xvfb into DISPLAY=:99 from your PHP.


One response to “Run latest PhantomJS with shell_exec PHP on Ubuntu 11.10 Oneiric”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.