403 Forbidden
403 Forbidden

Reputation: 91

exec() and phantomjs issue with absolute paths

I'm using phantomJS for the purposes of programmatically taking screenshots of a webpage. My webserver runs on Linux 64 bit.

The Scenario

My test.php file

exec('./phantomjs --version', $o, $e);
print_r($o);
echo $e;

I open test.php in a browser. The out put I get is:

1.9.1 // version number
0 // exit code

This proves that I can run commands through exec() and phantomJS is working perfectly.

The Issue

Now when I replace the above code with:

exec('./phantomjs http://mywebsite.com/test.js', $o, $e);
print_r($o);
echo $e;

The output is:

Array ( ) // empty output
139 // exit code which on investigating turned out to be segmentation fault

I also tried:

exec('./phantomjs ./test.js', $o, $e); // since phantomjs and test.js are in same folder

but the result was the same (segfault)

test.js code:

var page = require('webpage').create();
var url = 'http://www.rediff.com/';
page.open(url, function (status) {
    phantom.exit();
});

This makes me believe that using the full path as the second argument of phantomJS is causing it to crash. Thus, the things that I'm wondering are:

Upvotes: 7

Views: 7875

Answers (4)

ashgallery
ashgallery

Reputation: 71

I found the issue to be with selinux (which is now disabled by default on all production server builds as standard).

in file /etc/selinux/config find line containing:

SELINUX=

and change it to this:

SELINUX=disabled

run command (to take immediate effect without reboot)

/usr/sbin/setenforce 0

Upvotes: 0

paul
paul

Reputation: 488

After a lot of searching and testing I got it to work with following additions:

//throws a lot of errors because searching some libraries
$cmd = 'unset DYLD_LIBRARY_PATH ;';
$cmd.= ' /abs/path/to/phantomjs';
$cmd.= ' /abs/path/to/script.js';

//set environment variable to node source
putenv('PATH=/abs/path/to/node/bin/');

//now exec the cmd and pipe the errors to stdout
exec($cmd.' 2>&1', $output);

//and output the results
print_r($output);

I'm not the best server admin, so I can not explain everything in detail, but the lines above generate an pdf. Yeah.

Upvotes: 3

Preston S
Preston S

Reputation: 2771

I had a similar issue. PHP + PhantomJS Rasterize I found that phantomjs does not like running from an apache process. Try running your exec command from the command line:

php -r "exec('./phantomjs http://mywebsite.com/test.js', $o, $e); print_r($o); echo $e;"

If this works you have a few options:

1.) Some have suggested modifying sudoers to give no password sudo permissions for apache user to phantomjs binary

2.) Do like I did and run your script as a cron.

Upvotes: 1

Mikhail Gerasimov
Mikhail Gerasimov

Reputation: 39606

Try to place test.js in folder where test.php located (when calling exec('./phantomjs ./test.js', $o, $e);) or use full path.

Upvotes: 0

Related Questions