有沒有辦法用 php + bash 運行 Google Lighthouse?
P粉799885311
P粉799885311 2024-03-28 10:21:11
0
1
390

我嘗試在我的網站上使用 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=
VALUE=$(echo $SITE | awk -F'//' '{print }')
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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!