Phantomjs is a headless webkit browser with javascript API. There are many practical uses for this library but it can be only applied when it is hosted on a server.We have to build the phantomjs application on the server side in order to use it.
Talking about the server we need to use a server for which you have the administrative access to its shell.Not many web hosting providers have this option because mostly those servers will have the jailed access to the server shell.I tried many free alternatives but Amazon EC2 is the best choice if you need a free server.
We have seen the steps for launching an Amazon Instance but now we go through the steps involved in setting up the phantomjs on Amazon EC2 server.
Also read: Phantomjs- Webkit browser built on Javascript
The creators of this library have provided the binary packages for windows,linux and mac operating systems which you can directly copy it to your server and execute it .But the binary packages might not be supported if your server architecture is different.
If the binary package is not working as expected then I would suggest to download the source code and build it on your server so that it works fine on your environment.I chose Amazon EC2 linux micro instance which is free to use for one year.
Steps to compile PhantomJS on Amazon EC2
1. Login to the server and copy the source code to the directory of your choice.
2. Change the execution to super user by executing “sudo su” command on the shell.
3.
1 2 3 4 5 | sudo yum install gcc gcc-c++ make git openssl-devel freetype-devel fontconfig-devel git clone git://github.com/ariya/phantomjs.git cd phantomjs git checkout 1.7 ./build.sh --jobs 1 |
The first command will install the necessary components needed to compile.
4. The build.sh command will start the build process and it normally takes several hours to complete it.Build command can run four jobs in parallel on the dual core processor.In amazon EC2 micro instance the process speed is based on the availability of the CPU cycles.For this reason we are limiting the execution to one job at a time so the build time is quite longer.
For me it took more than 10 hours to build the phantomjs package and finally after completion a binary file will be generated on bin folder.You can use this package to execute the shell commands.
Now we have deployed the package on our own server.In our next tutorial we will show you how to take screenshots of websites using php and phantomjs.
If you have any difficulties in setting up phantomjs on Amazon EC2 please share it on the comments section.
Reference
Phantomjs Build Instruction