Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to solve error QXcbConnection: Could not connect to display when using exec function for phantomJs using PHP

I am working on a project where I want to use PHP and Phantomjs together, I have completed my phantomJs script and trying to run it using php exec function. but the function is returning an array of error list. below I am writing my code of phantomjs and php

dir: /var/www/html/phantom/index.js

var page = require('webpage').create();
var fs = require('fs');

page.open('http://insttaorder.com/', function(status) {
    // Get all links to CSS and JS on the page
    var links = page.evaluate(function() {

        var urls = [];

        $("[rel=stylesheet]").each(function(i, css) {
            urls.push(css.href);
        });

        $("script").each(function(i, js) {
            if (js.src) {
                urls.push(js.src);
            }
        });

        return urls;
    });

    // Save all links to a file
    var url_file = "list.txt";
    fs.write(url_file, links.join("\n"), 'w');

    // Launch wget program to download all files from the list.txt to current
    // folder
    require("child_process").execFile("wget", [ "-i", url_file ], null,
            function(err, stdout, stderr) {

                console.log("execFileSTDOUT:", stdout);
                console.log("execFileSTDERR:", stderr);

                // After wget finished exit PhantomJS
                phantom.exit();

            });

});

dir: /var/www/html/phantom/index.php

exec('/usr/bin/phantomjs  index.js 2>&1',$output);
echo '<pre>';
print_r($output);
die;

Also tried with

 exec('/usr/bin/phantomjs  /var/www/html/phantom/index.js 2>&1',$output);
    echo '<pre>';
    print_r($output);
    die;

After runing this i am getting below error

Array
(
    [0] => QXcbConnection: Could not connect to display
    [1] => PhantomJS has crashed. Please read the bug reporting guide at
    [2] =>  and file a bug report.
    [3] => Aborted (core dumped)
)

But if I run index.php file from the terminal like this:

user2@user2-H81M-S:/var/www/html/phantom$ php index.php

then it works fine.I don't know how to solve it. Please help.

i am using following version

system version: Ubuntu 16.04.2 LTS
PHP version: 5.6
phantomJs version: 2.1.1

like image 557
Shakti Sisodiya Avatar asked Mar 07 '18 14:03

Shakti Sisodiya


2 Answers

Did you tried to set an environment variable on your server ? or added it before calling phantomjs ?

I was in the same situation and found some solutions:

a. define or set variable QT_QPA_PLATFORM to offscreen:

QT_QPA_PLATFORM=offscreen /usr/bin/phantomjs index.js

b. or add this line into your .bashrc file (put it at the end):

export QT_QPA_PLATFORM=offscreen

c. or install the package xvfb and call xvfb-run before phantomjs:

xvfb-run /usr/bin/phantomjs index.js

d. or use the parameter platform:

/usr/bin/phantomjs -platform offscreen index.js

Maybe you don't want / can't make modification on your server and in that case you may try to download the static binary from official website then:

/path/to/the/bin/folder/phantomjs index.js

and / or create an alias in your .bash_aliases file like this:

alias phantomjs=/path/to/the/bin/folder/phantomjs

make sure that phantomjs is not installed already on the system if you decide to use the alias.

if the file .bash_aliases not exist already, feel free to create it or add the alias line at the end of the .bashrc file

Some references:

  • https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=817277
  • https://github.com/ariya/phantomjs/issues/14376
  • https://bugs.launchpad.net/ubuntu/+source/phantomjs/+bug/1586134
like image 179
Jiab77 Avatar answered Nov 19 '22 15:11

Jiab77


I had the same problem running phantomjs on headless Ubuntu 18.04 (on the default Vagrant vm install of openstreetmap-website). Folloiwng Jiab77's links, it seems the Phantomjs team says the problem is the Debian package but the Debian team closed the bug as wontfix. I needed phantomjs to "just work" so it can be called by other programs that expect it to work normally. Specifically, openstreetmap-website has an extensive Ruby test suite with over 40 tests that were failing because of this, and I didn't want to modify all those tests.

Following Jiab77's answer, here's how I made it work:

  1. As root, cp /usr/bin/phantomjs /usr/local/bin/phantomjs
  2. Edit /usr/local/bin/phantomjs and add the line export QT_QPA_PLATFORM=offscreen so it runs before execution. Here is what mine says after doing so:

    #!/bin/sh LD_LIBRARY_PATH="/usr/lib/phantomjs:$LD_LIBRARY_PATH" export LD_LIBRARY_PATH # 2018-11-13: added the next line so phantomjs can run headless as explained on # https://stackoverflow.com/questions/49154209/how-to-solve-error-qxcbconnection-could-not-connect-to-display-when-using-exec export QT_QPA_PLATFORM=offscreen exec "/usr/lib/phantomjs/phantomjs" "$@"

After this change, phantomjs can be run from the command line without changing anything else, and all the tests that depend on phantomjs were successfully passed.

like image 2
krubo Avatar answered Nov 19 '22 17:11

krubo