2016-06-30 49 views
0

在我的服务器(Ubuntu 14.04.4 LTS)上,我安装了Firefox,以及用于无头Firefox操作的xvfb以及带有SlimerJS的CasperJS。我也有一个CasperJS脚本,工作正常。我想从PHP使用这个脚本;这是我这个PHP脚本的精髓,让我们把它mytest.php在网络服务器上通过PHP运行SlimerJS +无头Firefox脚本?

echo "php_sapi_name() " . php_sapi_name() . "\n"; // "cli" for php cli, "apache2handler" for php via webserver 

chdir(dirname(__FILE__)); 

$nodeModPath = "/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules"; 

putenv("SLIMERJSLAUNCHER=/usr/bin/firefox46"); 
$cmdline = "xvfb-run $nodeModPath/casperjs/bin/casperjs --engine=slimerjs --debug=true mySlimerScript.js"; 

$returnString = shell_exec($cmdline); 
echo "$returnString\n"; 

编辑:请注意,命令可能也只是:

$cmdline = "xvfb-run $nodeModPath/casperjs/bin/casperjs --engine=slimerjs --debug=true 2>&1"; 

...这是,没有列出任何JS脚本 - 在这种情况下应该转储帮助(并且在CLI访问的情况下 - 但通过网络服务器访问时报告的错误与以下相同)


当我运行从终端命令行(通过SSH)这个PHP脚本,即通过PHP的CLI模式:

$ php mytest.php 

...一切都正常运行,也没有任何问题。

然而,当我通过Web服务器在线调用此PHP脚本,即通过http://example.com/mytest.php,它第一次失败,出现错误:

Gecko error: it seems /usr/bin/firefox46 is not compatible with SlimerJS. 
See Gecko version compatibility. If version is correct, launch slimerjs 
with --debug=true to see Firefox error message 

...并添加--debug=true后(如已包含在例如以上),我还收到此错误:

JavaScript error: resource://gre/modules/FileUtils.jsm, line 63: NS_ERROR_FAILURE: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIProperties.get] 

所以,显然我的无头Firefox不希望运行,当PHP通过Web服务器调用(在这种情况下,PHP报告说,它使用apache2handler SAPI)。

有人会知道为什么会发生这种情况 - 以及如何在从Web服务器调用脚本时正确执行脚本,就像在PHP CLI模式下运行一样?编辑2:现在可以通过CLI模式重建此错误,并且可以确认它是由于用户;所以没有在$command提供任何JS脚本,我得到这个:

$ sudo -H -u root php mytest.php 
... 
Usage: casperjs [options] script.[js|coffee] [script argument [script argument ...]] 
     casperjs [options] test [test path [test path ...]] 
     casperjs [options] selftest 
... 

$ sudo -H -u www-data php mytest.php 
JavaScript error: resource://gre/modules/FileUtils.jsm, line 63: NS_ERROR_FAILURE: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIProperties.get] 
Gecko error: it seems /usr/bin/firefox46 is not compatible with SlimerJS. 
See Gecko version compatibility. If version is correct, launch slimerjs 
with --debug=true to see Firefox error message 
+1

第一个想法:在'cli'模式下运行脚本的文件权限将让用户运行它,但是,通过网络服务器,用户将是'www-data'。可能,不是吗? –

+0

的确,@ php-dev,但我无法真正弄清楚谁的权限会成为问题;这里我称之为'xvfb-run',它调用'casperjs',它调用调用无头Firefox的'slimerjs'。有没有办法在这种情况下检查权限错误?我尝试了'/ var/log/apache2/error-mysite.log',但它只报告了PHP的东西,例如''shell_exec'调用失败后试图获取非对象的属性。 – sdbbs

+1

尝试将'stderr'输出重定向到'stdout'或文件。 –

回答

4

好了,这是一个讨厌的问题。最后我做一个strace,并比较日志,为root用户和www-data用户运行一个完整slimerjs时(完整的命令行可以通过添加echo ES被发现/path/to/slimerjs-0.10.1-pre/slimerjs):

sudo -H -u www-data strace \ 
    /usr/bin/firefox46 -app /path/to/slimerjs-0.10.1-pre/application.ini \ 
    --profile /path/to/firefox-46.0.1/profile-46 -no-remote --debug=true /home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs/bin/bootstrap.js --casper-path=/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs \ 
    --cli 2>&1 \ 
    | tee /tmp/strace.log 

sudo -H -u root strace \ 
    /usr/bin/firefox46 -app /path/to/slimerjs-0.10.1-pre/application.ini \ 
    --profile /path/to/firefox-46.0.1/profile-46 -no-remote --debug=true /home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs/bin/bootstrap.js --casper-path=/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs \ 
    --cli 2>&1 \ 
    | tee /tmp/straceR.log 

如果这些现在日志中说meld相比,则最终开始在这样一个点发散:

mkdir("/root/.innophi", 0700)   = 0 
mkdir("/root/.innophi/slimerjs", 0700) = 0 

... [vs.] ... 

mkdir("/var/www/.innophi", 0700)  = -1 EACCES (Permission denied) 
access("/var/www/.innophi", F_OK)  = -1 ENOENT (No such file or directory) 

所以,casperJS基本上试图创建在用户的主目录的目录;问题是,www-data$HOME/var/www,它似乎没有写入权限!

所以,对我来说最简单的事情是“黑客”,在mytest.php脚本$HOME环境变量,并将其设置为/tmp,其中www-data肯定有写权限:

... 
putenv("SLIMERJSLAUNCHER=/usr/bin/firefox46"); 
putenv("HOME=/tmp"); 
... 

...和whaddayaknow ,最后根据脚本CLI中www-data用户太多的工作原理:

$ sudo -H -u www-data php test_commands.php 
... 
Options: 

--verbose Prints log messages to the console 
--log-level Sets logging level 
--help  Prints this help 
... 

顺便说一句,这.innophi目录似乎也被中提到https://docs.slimerjs.org/current/configuration.html#profiles,

相关问题