Installing PhantomJS on Ubuntu for use by Apache/PHP

UPDATE:  PhantomJS 1.5 is now purely headless and pre-compiled!  This makes it way easier than the method described in my original post.  Now, you just:

1.  Install the necessary dependencies:
sudo apt-get install libfreetype6 fontconfig

2.  Download the pre-compiled version that matches your OS.

3.  Extract it somewhere (in this case I extracted it to my $HOME directory) and then create a symbolic link to the binary: 
sudo ln -s ~/phantomjs/bin/phantomjs /usr/local/bin/phantomjs
4.  Set group: 
sudo chown root:www-data ~/phantomjs/bin/phantomjs
5.  Set file permissions: 
sudo chmod 0755 ~/phantomjs/bin/phantomjs
6.  To test everything, open a command prompt and type (this should output something like 1.5.0): 
phantomjs --version

...I'll keep this old post for historical purposes:

PhantomJS is a really cool project for specific use cases.  To install on Ubuntu and make the functionality available to Apache/PHP, do the following:

1.  Install dependencies and Xvfb (due to issue #163):
sudo apt-get install xvfb git build-essential gtk2-engines-pixbuf xfonts-100dpi x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic libqt4-dev libqtwebkit-dev qt4-qmake python-qt4

2.  Create the Xvfb service:

sudo nano /etc/init.d/Xvfb

Paste the following into the file:

#! /bin/sh

### BEGIN INIT INFO
# Provides: Xvfb
# Required-Start: $local_fs $remote_fs
# Required-Stop:
# X-Start-Before:
# Default-Start: 2 3 4 5
# Default-Stop:
### END INIT INFO

N=/etc/init.d/Xvfb

set -e

case "$1" in
  start)
Xvfb :0 -screen 0 1024x768x24 &
;;
  stop|reload|restart|force-reload)
;;
  *) 
echo "Usage: $N {start|stop|restart|force-reload}" >&2exit 1
;;
esac

exit 0
3.  Set service to be executable and to autostart:

sudo chmod +x /etc/init.d/Xvfb

sudo update-rc.d Xvfb defaults

4.  Restart your computer

5.  Create a directory where you will build PhantomJS: 
mkdir -p ~/github && cd ~/github
6.  Download PhantomJS:
git clone git://github.com/ariya/phantomjs.git && cd phantomjs
7.  (optional)  If you want to build a specific version of PhantomJS (1.4 in this example), perform this next step, otherwise skip to Step 5 and the latest PhantomJS code (master) will be used:
git checkout 1.4
8.  Build PhantomJS:
qmake-qt4 && make
9.  Once PhantomJS is built, copy the binary into your local path: 
sudo cp ~/github/phantomjs/bin/phantomjs /usr/local/bin
10.  Set group: 
sudo chown root:www-data /usr/local/bin/phantomjs
11.  Set file permissions: 
sudo chmod 0755 /usr/local/bin/phantomjs
12.  To test everything, open a command prompt and type (this should output something like 1.4.1): 
DISPLAY=:0 phantomjs --version

Congratulations!  You can now use PhantomJS in your PHP code, for example:
<?php
echo "PhantomJS " . shell_exec("DISPLAY=:0 phantomjs --version");
?>
P.S. If you're looking for an inexpensive, reliable web host that supports PhantomJS, I recommend WebFaction

Comments

  1. Everything in one place ... great, thank you :)

    ReplyDelete
  2. Just plain awesome. As succinct as you can be. You've made this part of the php / phantomjs process as painless as it gets. Thanks for writing it out.

    ReplyDelete

Post a Comment

Keep it clean and professional...

Popular Posts