Skip to content

Using PhantomJS on Shared Hosting like HostGator

by arlo on June 20th, 2013 

Installing PhantomJS

If you are a shared account Hostgator user you know that Hostgator locks down your account fairly well. You don’t have access to install anything on your machine and if CPanel doesn’t give you an option you are back to square one. Well luckily for us, the awesome people at PhantomJS provide us with an already compiled binary file for CentOS linux servers. This is awesome news because Hostgator uses CentOS linux servers for shared hosting.

A binary file is like a .exe file in Windows. That one file contains a whole program all you have to do is “execute” the program (equilavent to a double click with a mouse).

All we have to do is download the PhantomJS binary for CentOS 32 Bit and upload/ftp that binary file to your hosting account.

Incase you missed the link above, download the binary from here: http://phantomjs.org/download.html

Running PhantomJS in Command Line

I would suggest moving the PhantomJS binary file to your home directory. Once there, you can execute PhantomJS by simply pointing to the file: ~/phantomjs -v.

Note: Since you do not have permission to expose a program globally on shared hosting. You must use the PATH to PhantomJS in order to execute the program. e.g. /path/to/phantomjs -v

If you are not familiar with PhantomJS yet you may not know that PhantomJS needs a javascript program passed to it in it’s first parameter. If you visit PhantomJS’ github page you can obtain a folder full of example javascript programs.

Personally, I decided to make a folder in my home directory called phantomJSPrograms. So when I run PhantomJS from my home directory my command looks something like this.

./phantomjs phantomJSPrograms/rasterize.js http://arlocarreon.com arloBlog.png

The above command will render a screenshot of my blog.

Running PhantomJS in PHP

If you know how to run PhantomJS in command line then nothing changes in PHP, with the exception of 1 caveat.

  1. First practice in command line until you know exactly what command you want to run.
  2. [Caveat] – You must end all your commands with 2>&1. This is because PhantomJS returns some messages as error messages via stdout 2, so we need to pipe all stdout 2 messages to stdout 1. Our php methods return output from stdout 1.
  3. Use any php method you feel comfortable with for executing shell commands. exec, passthru, shell_exec, system, backticks, etc.

Demo

This is a demo to the app I was putting together. It periodically takes screenshots of our client’s sites and you can also manually reload the screenshot of any site. It uses a AJAX call to run a PHP script that runs PhantomJS.

From → Javascript

  • Pingback: Using PhantomJS on Shared Hosting like HostGator | Arlo Carreon | Webhost Reviewer.com

  • Manish

    I have a linux shell script which uses PhantomJS. This script requires linux server that have PhantomJS configured. The installation path of PhantomJS has to be specified in the included script file named hit.sh.

    Other parameters that can be configured in the same file are:

    1. COOKIE_SECONDS (to control the length of the visit in seconds), and
    2. SLEEP (to add delay between consecutive PVs)

    I want to replicate this method on few shared linux hosting server providers.

    Do you know if I would need special privileges on the shared hosting servers or will this easily run on a standard shared linux hosting server?

    Thanks for your help.

    Manish

  • http://www.arlocarreon.com Arlo Carreon

    Manish,

    Sorry for the late reply. You shouldn’t have a problem running this on a shared environment.

  • Yohan

    Hi,

    can you share your exec PHP command?

    I’m trying to use “shell_exec()”

    when only runs “./phantomjs -v” it runs well, saying the version is 1.9.2

    but when I try to run hello world as in the script below

    console.log(‘Hello, world!’);

    phantom.exit();

    it just crashed, and I don’t seem to able to produce crash dump. can you explain about this?

    thanks in advance

  • http://www.arlocarreon.com Arlo Carreon

    Yohan,

    I use the backtick operators in PHP. My command looks like this:
    $response = `/path/to/phantomjs script.js http://domain.com`;

    Maybe phantomjs is throwing errors. If so you need to pipe the error our put to stdout 1. You can do so by adding ” 2>&1″ at the end of you command.

  • Yohan

    Thanks for replying.

    still crashed, and the minidump thingy is not working as well.
    might need to check on other shared hosting.

    thanks

  • http://www.arlocarreon.com Arlo Carreon

    Have you ran that command on your shared hosting account via SSH?

  • Guest

    I’m trying to make this work, but when I try to run this command (on xampp localhost) nothing happens. I can run other exec commands, but not this. Here’s the php code:

    &1`;
    ?>

    and the phantom script

    var page = require(‘webpage’).create();
    page.open(‘http://localhost:8080/cards/’, function () {
    page.render(‘image.png’);
    phantom.exit();
    });

    Any ideas? Or does it simply just not work on a localhost?

  • http://davidholman.net/ David Holman

    I’m trying to make this work, but when I try to run this command (on xampp localhost) nothing happens. I can run other exec commands, but not this. Here’s the php code:

    `./bin/phantomjs -v ./phantom.js 2>&1`;

    and the phantom script

    var page = require(‘webpage’).create();
    page.open(‘http://localhost:8080/cards/’, function () {
    page.render(‘image.png’);
    phantom.exit();
    });

    Any ideas? Or does it simply just not work on a localhost?

    EDIT:

    If I execute the following:

    if(`./bin/phantomjs -v ./phantom.js 2>&1`) {
    echo “success”;
    } else {
    echo “FAIL”;
    }

    it gives me “sucess” back, but it doesn’t actually execute anything.

  • Yohan

    That’s the problem..
    just found out that I can’t access SSH within my shared hosting.
    still waiting for the admin’s reply :)

  • http://www.arlocarreon.com Arlo Carreon

    Make sure your command runs in the command line or cmd prompt first. This will test your phantom script.

    If it runs perfectly in command line.

    1) change ur path to absolute paths

    2) consider permission issues. PHP runs commands under the user apache is using.

    3) replay phantom output to php by using echo in front of whatever function is executing ur command or use an array like exec() requires.

    I don’t mid scheduling a screen sharing session if you email me.

  • http://davidholman.net/ David Holman

    I just got it to work, thanks.

    I’m wondering how to send information to the .js file and how to send information back from the .js file.

    EDIT:
    Also, I only got it working on lokalhost, but not on my host, all I get is “phantomjs: phantomjs: cannot execute binary file”, any ideas?

  • http://www.arlocarreon.com Arlo Carreon

    Sounds like permissions. How did you get phantomjs into your host? Also who are you hosting with?

  • http://www.arlocarreon.com Arlo Carreon

    To js file: Through params in your command. “/path/to/phantomjs /path/to/jsfile.js info1 info2 info3″

    From js file: via console.log(). This will spit out info that is captured as a string in PHP. I like to collect a JS object with all the info i want to return. Then I use JSON.stringify(myObj), which creates json. Then when I capture that in PHP I use json_decode() to turn it into a PHP array.

  • http://davidholman.net/ David Holman

    I transfered it through an ftp client.
    I’m embarrassed to say that I’m hosting with wpwebhost.com, but it’s true. I’ve got a few months left on my plan and will switch then, any recommendations?

  • http://davidholman.net/ David Holman

    Cool, thanks!
    But in the js file, how do I get the params then? They don’t have any hooks.

  • http://www.arlocarreon.com Arlo Carreon

    Using my previous example:

    var foo1 = system.args[1]; //info1
    var foo2 = system.args[2]; //info2
    etc

  • http://www.arlocarreon.com Arlo Carreon

    I would need to check permissions on the actual file uploaded. I doubt your host disallows the execution of any binary for your user.

    Do you have SSH access to your hosting account? If so is that where you are seeing these errors? I am super curious to check this out, I wonder what the problem might be.

  • http://davidholman.net/ David Holman

    I changed the permission, it was set to 644, and now is 755. Still doesn’t change anything.

    I do have ssh, but I can’t get putty to work. Got any tutorial to direct me towards?

    Yeah, I’ll definitely change to a big name web server when my renewal comes up. Thanks.

  • http://www.arlocarreon.com Arlo Carreon

    By permissions i meant more the owner and group of the binary file. Once SSH’d in you can do a “ls -l” or “ll” command to find out.

    I’ve had some tough times with putty. On mac and linux you have openssh so things work out a lot easier. When I was on windows I enjoyed the ssh client that came with Git Bash. Git Bash uses cygwin, which I have never gotten to work outside of Git Bash.

    If you happen to have Git Bash around, open that up and just use standard unix commands: ssh user@domain.com -p 22

    Hostgator uses port 2222 for SSH, so maybe your host also uses a non standard port.

  • http://davidholman.net/ David Holman

    I’ve tried pretty much everything at this moment. I tried GitBash, it just gives me:
    “ssh connect to host [DOMAIN] port 22: bad file number”

    Got any ideas of that?

  • http://www.arlocarreon.com Arlo Carreon

    I haven’t seen this before. I found an SO post that suggests using port 443 instead.

    Again in your case it might be your hosting provider using a non standard port, this is common amongst hosting providers.

    EDIT

    Contact your hosting provider and ask what port you have to use for SSH.

  • zain bash

    i am using hostgator shared account and its not excecuting. i have moved the pahtomjs to the home folder.

  • Michal Pina

    i read all the notes and just something isn’t right here…i execute this…/public_html]# phantomjs -v and I get….
    -bash: phantomjs: command not found

    the file is in the directory I have execute it under … whats up? Is it obvious?

  • http://www.arlocarreon.com Arlo Carreon

    /public_html]# phantomjs -v

    That command will only work if you added phantomjs folder into a bin folder that exists in your $PATH variable…So if you have the executable in your current directory, you need to prepend it with “./”

    try:

    /public_html]# ./phantomjs -v

  • Michal Pina

    i changed the directory then gave it 777 than I received this issuing the above command:

    ./phantomjs: /lib64/ld-linux-x86-64.so.2: bad ELF interpreter: No such file or directory

    I looked it up online and i saw many references to various things that needed installing but when I tried each they failed … kind of made me think there were all wrong anyways!

  • http://www.arlocarreon.com Arlo Carreon

    Wow. This is on Hostgator right? I never ran into these problems.

    Try downloading the 64bit version for Cent OS from http://phantomjs.org/download.html

    if that doesn’t work, maybe I can give you a link to the binary I am using.

  • Michal Pina

    unfortunate this is on godaddy!they are very frustrating! I had ran your suggestion already and these are the files I am working with…I do not want to take anymore of your time … but the tool is exactly what I am wanting to work with as it helps me do something else…I have no issue of connecting u to it…

  • Michal Pina

    that be great!

  • earthgld

    just put the phantomjs binary in website folder and work fine while running with PHP, the one i confuse is the what file permission? Sometimes i got error 505

  • Ram

    Hi, I tried uploading phantomjs binary file to my root folder and used shell_exec(“./phantomjs -v”) to display phantomjs version. Nothing displayed. I tried shell_exec(“ls”) which listed the directory listing properly. I tries with both 32bit version and 64 bit version.

    Any idea what i am missing with.

  • http://www.arlocarreon.com Arlo Carreon

    Sounds familiar. Did you try: shell_exec(“./phantomjs -v 2>$1″); ?

    Read #2 in the section “Running PhantomJS in PHP”.

  • Ram

    Thanks, it comes up with “sh: ./phantomjs: Permission denied”. Then i changed the phantomjs file permission to 755 and 777 after that no output. I checked with ls -ll to see whether file permission got affected or not. Permission got changed properly.

  • http://www.arlocarreon.com Arlo Carreon

    Hmm, not sure. Make sure your script is running the same binary you are changing permissions for. Try to use the full path /path/to/phantomjs. Besides that I’m not sure what it might be. Feel free to book a session with me (check my sidebar), I wouldn’t mind taking a look with some screen sharing.

  • Tiago

    Hi Arlo, Im having the same problem, can you help me on skype please? Skype: tiago.pmc

  • Tiago

    I have installed phantomjs , but I got the following error:

    sh: ./bin/phantomjs: Permission denied

  • http://www.arlocarreon.com Arlo Carreon

    Currently I am only accepting sessions through codementor.io.

    https://www.codementor.io/mexitek