title: how to solve error QXcbConnection: Could not connect to display when using exec function for phantomJs using PHP
body: <p>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</p>
  
  <p>dir: /var/www/html/phantom/index.js</p>
  
  <pre><code>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();
  
              });
  
  });
  </code></pre>
  
  <p>dir: /var/www/html/phantom/index.php</p>
  
  <pre><code>exec('/usr/bin/phantomjs  index.js 2&gt;&amp;1',$output);
  echo '&lt;pre&gt;';
  print_r($output);
  die;
  </code></pre>
  
  <p>Also tried with</p>
  
  <pre><code> exec('/usr/bin/phantomjs  /var/www/html/phantom/index.js 2&gt;&amp;1',$output);
      echo '&lt;pre&gt;';
      print_r($output);
      die;
  </code></pre>
  
  <p>After runing this i am getting below error</p>
  
  <pre><code>Array
  (
      [0] =&gt; QXcbConnection: Could not connect to display
      [1] =&gt; PhantomJS has crashed. Please read the bug reporting guide at
      [2] =&gt;  and file a bug report.
      [3] =&gt; Aborted (core dumped)
  )
  </code></pre>
  
  <p>But if I run index.php file from the terminal like this:</p>
  
  <pre><code>user2@user2-H81M-S:/var/www/html/phantom$ php index.php
  </code></pre>
  
  <p>then it works fine.I don't know how to solve it. Please help.</p>
  
  <p>i am using following version<br/><br/>
  system version: Ubuntu 16.04.2 LTS<br>
  PHP version: 5.6<br/>
  phantomJs version: 2.1.1</p>