有没有办法用 php + bash 运行 Google Lighthouse?
P粉799885311
P粉799885311 2024-03-28 10:21:11
[PHP讨论组]

我尝试在我的网站上使用 Google-Lighthouse。 我编写了一个 php 文件,该文件调用 bash 脚本来运行 lighthouse-cli。

run_bash.php

<?php

if(isset($_GET['subject2'])) {

$text = $_GET['subject2'];

$text = escapeshellarg($text);

$command = './rex.sh 2>&1 >> path/mylog ' . $text ;
$output = shell_exec($command);
echo "<pre>$output</pre>";

}
?>

我的 bash 脚本:

rex.sh

#!/bin/bash
SITE=""
SITE=$1
VALUE=$(echo $SITE | awk -F'//' '{print $2}')
lighthouse $SITE --output html  --output-path ./path/$VALUE.html

还有一个简单的 php 表单,名为 run_bash.php

现在的问题是: 如果我使用 Putty 在 shell 中运行 bash 脚本,效果很好,但是当调用我的 URL 并运行 run_bash.php 时,这些错误会显示在浏览器上:

Sun, 05 Jun 2022 19:20:59 GMT LH:ChromeLauncher Waiting for browser...............................................................................................
Sun, 05 Jun 2022 19:21:00 GMT LH:ChromeLauncher Waiting for browser.................................................................................................
Sun, 05 Jun 2022 19:21:00 GMT LH:ChromeLauncher Waiting for browser...................................................................................................
Sun, 05 Jun 2022 19:21:01 GMT LH:ChromeLauncher Waiting for browser.....................................................................................................
Sun, 05 Jun 2022 19:21:01 GMT LH:ChromeLauncher Waiting for browser.......................................................................................................

Sun, 05 Jun 2022 19:21:01 GMT LH:ChromeLauncher:error connect ECONNREFUSED 127.0.0.1:33989
Sun, 05 Jun 2022 19:21:01 GMT LH:ChromeLauncher:error Logging contents of /tmp/lighthouse.dVx18OP/chrome-err.log

Unable to connect to Chrome

我很想知道是否有人曾经这样做过? 或者说这根本有可能吗?

P粉799885311
P粉799885311

全部回复(1)
P粉541565322

终于找到了答案! 我必须在 php 文件中使用函数 exec() 来运行 bash 脚本,如下所示。

$output = exec('./rex.sh  ' . $text  );

还有一个重要的点。 我将所有 /var/www/ 路径文件的访问级别更改为 www-data

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号