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 :

$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 :


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 :

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

Now, let change your scripts into :

test.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) :

#!/bin/bash
export DISPLAY=:0
phantomjs --version

test.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.

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 :

Xvfb :23 -screen 0 1024x768x24

And execute from shell_exec() php by :


$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” :


#!/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 :

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 thought on “Run latest PhantomJS with shell_exec PHP on Ubuntu 11.10 Oneiric

  1. Just to add one thing. if you ever need to run phantomjs server from php, remember to add ‘&’ at the end of your command. You’ll get an infinite request without it.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>