我尝试在我的网站上使用 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
我很想知道是否有人曾经这样做过? 或者说这根本有可能吗?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我终于找到了答案! 我必须在 php 文件中使用函数
exec()来运行 bash 脚本,如下所示。$output = exec('./rex.sh ' . $text );还有一个重要的点。 我将所有
/var/www/路径文件的访问级别更改为www-data。