简体   繁体   English

通过SSH远程运行PHP

[英]Running PHP remotely via SSH

I have a video encoding server set up on Laravel Forge with nginx. 我在Laravel Forge上使用nginx设置了视频编码服务器。 I'm trying to run a testing script to encode a video remotely via SSH, using the LaravelCollective SSH package . 我正在尝试使用LaravelCollective SSH软件包运行测试脚本以通过SSH远程编码视频。

This is my testing script (index.php) 这是我的测试脚本(index.php)

<?php
exec("ffmpeg -I input.mpg -c:v libx264 -preset faster -crf 22 -c:a aac -strict experimental -movflags +faststart -vf scale=360:-1 output.mp4 1> output.txt 2>&1");

When I SSH into the server and run the script from the command line it works: the video is encoded; 当我通过SSH进入服务器并从命令行运行脚本时,它可以工作:视频已编码; the script is working. 该脚本正在运行。

$ php /home/forge/mydomainname.com/public/test/index.php

However, when I run the same command locally in my Laravel app - using the SSH package - it doesn't encode and I receive no output; 但是,当我在我的Laravel应用中本地运行同一命令时(使用SSH软件包),该命令不会进行编码,也不会收到任何输出。 the browser just returns a white page. 浏览器仅返回白页。

SSH::run('php /home/forge/mydomainname.com/public/test/index.php', function($line) {
    echo $line.PHP_EOL;
});

However however, if I open index.php , comment out the FFmpeg command and add some code to check if exec is enabled, it will execute and send output, so I know that the SSH package is actually working and executing the script remotely. 然而不过,如果我打开index.php ,注释掉FFmpeg的命令,并添加一些代码来检查是否exec启用, 它将执行并发送输出,所以我知道SSH软件包实际工作和远程执行脚本。

<?php
// exec("ffmpeg -I input.mpg -c:v libx264 -preset faster -crf 22 -c:a aac -strict experimental -movflags +faststart -vf scale=360:-1 output.mp4 1> output.txt 2>&1");

if(function_exists('exec')) {
    echo "exec is enabled";
} else {
    echo "exec is disabled";
}

It will return "exec is enabled" to my browser. 它会向我的浏览器返回“已启用exec”。

To sum up: 总结一下:

  1. The script will encode video when running it via the command line on the server. 通过服务器上的命令行运行脚本时,该脚本将对视频进行编码。
  2. The script will not encode video when running it remotely. 该脚本在远程运行时不会对视频进行编码。
  3. The script will execute when running it remotely. 该脚本在远程运行时执行。
  4. WTH? 什么

I figured it out. 我想到了。

From within my Laravel app I decided to try echo exec('whoami'); 从我的Laravel应用程序中,我决定尝试echo exec('whoami'); and see if that returned anything to the browser. 看看是否有任何东西返回浏览器。 It did, so I knew exec() was working and I could trigger it via the SSH package. 它确实做到了,所以我知道exec()正在工作,并且可以通过SSH软件包触发它。

Then I realized that my ffmpeg encoding command was suppressing output with 2>&1 . 然后我意识到我的ffmpeg编码命令正在抑制2>&1输出。 I removed that and finally saw what was going on: I was receiving a "file not found" error, which was weird because input.mpg is in the same directory as index.php . 我删除了它,最后看到了怎么回事:我收到一个“找不到文件”错误,这很奇怪,因为input.mpgindex.php在同一目录中。

This has worked on three other servers, but not on this one created with Forge. 这已经在其他三台服务器上工作,但是在用Forge创建的那台服务器上却没有。

So I added the full path to the input file and voilà! 因此,我将完整路径添加到输入文件中,瞧! It works! 有用!

exec("ffmpeg -i /home/forge/mydomainname.com/public/test/input.mpg -c:v libx264 -preset faster -crf 22 -c:a aac -strict experimental -movflags +faststart -vf scale=360:-1 /home/forge/mydomainname.com/public/test/output.mp4 1> /home/forge/mydomainname.com/public/test/output.txt ");

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

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