简体   繁体   English

如何在Linux Web服务器上运行Phantomjs

[英]How to Run Phantomjs on Linux Webserver

I am trying to upload the executable file for Phantomjs onto my web server. 我正在尝试将Phantomjs的可执行文件上传到我的Web服务器上。 Currently, wordpress is installed on this server, so calling phantom will be done mainly through the use of php functions. 当前,wordpress已安装在此服务器上,因此调用phantom将主要通过使用php函数来完成。 The server is running Linux x86_64, which is the same repository of Phantom I have downloaded. 该服务器正在运行Linux x86_64,它与我下载的Phantom相同。 In one of my wordpress template files I try to call phantom and attempt to run a script, but nothing is echoed. 在我的一个wordpress模板文件中,我尝试调用phantom并尝试运行脚本,但是没有回声。 I know the javascript is right because it was one of the examples provided. 我知道javascript是正确的,因为它是提供的示例之一。 Also, the Phantomjs file has adequate permissions to run. 另外,Phantomjs文件具有足够的运行权限。 So no issues there. 因此,那里没有问题。 Here is all of the code I am trying to use to get this to work correctly. 这是我试图用来使其正常工作的所有代码。

<?php 
/* 
Template Name: Home Template
Description: Page template to show home page content.
*/

//phantom/phantomjs-directory/bin/phantomjs
//phantom/phantomjs-directory/examples/version.js

get_header(); 
?>
<?php 
echo exec('phantom/phantomjs-directory/bin/phantomjs phantom/phantomjs-directory/examples/version.js');
?>

The script being called is located one directory back in examples/ instead of bin/ and can be found below: 被调用的脚本位于examples/而不是bin/一个目录,可以在下面找到:

"use strict";
console.log('using PhantomJS version ' +
phantom.version.major + '.' +
phantom.version.minor + '.' +
phantom.version.patch);
phantom.exit();

Nothing at all is being logged or echoed. 根本没有记录或回声。 Why would this be? 为什么会这样呢?

尝试使用回声和反引号。

echo `/phantom/phantomjs myscript.js`; 

The correct way to exec is 以正确的方法exec的

exec('/phantom/phantomjs myscript.js', $response);
echo implode("<br>", $response);

Anyway you should first check that script works by logging in to server via SSH and trying to run the script manually. 无论如何,您首先应该通过SSH登录到服务器并尝试手动运行脚本来检查脚本是否正常运行。 PhantomJS binary could mismatch server processor architecture (x64 vs x86). PhantomJS二进制文件可能与服务器处理器体系结构不匹配(x64与x86)。

Also mind the path to the binary and the script, the easiest way is to use absolute paths. 还要注意二进制文件和脚本的路径,最简单的方法是使用绝对路径。

I made a couple of mistakes with this one. 我犯了几个错误。 The golden code that did this for me was only this: 为我做到这一点的黄金代码仅仅是这样:

echo exec('/home/mainshee/public_html/wp-content/themes/twentyseventeen/phantom/phantomjs-directory/bin/phantomjs /home/mainshee/public_html/wp-content/themes/twentyseventeen/phantom/phantomjs-directory/examples/version.js');

The major mistake I made was that the paths were not EXACT. 我犯的主要错误是路径不精确。 You would think that only including the path from he location you were already at would be adequate, but I found they want the ENTIRE path. 您可能会认为仅包括已经在他所处位置的路径就足够了,但是我发现他们想要整个路径。 Start to finish. 开始完成。 I also made sure that every file I was trying to execute had 0755 permissions. 我还确保我尝试执行的每个文件都具有0755权限。 The last precaution I took was to use scp to upload the phantomjs file folder rather than the traditional method. 我采取的最后一项预防措施是使用scp上传phantomjs文件夹而不是传统方法。 This was done to avoid any corruptions (although I am not sure if it made any difference in the end). 这样做是为了避免任何损坏(尽管我不确定最终是否会有所作为)。 Hope this helps somebody else! 希望这对其他人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM