I'm trying to use exec() to run a program in /var/www/litecoin/bin and echo the output.
Basically, this:
(from /var/www/html/index.php)
<?php
echo exec("../litecoin/bin/litecoin-cli getinfo");
?>
It just shows a blank page, though.
I know it's probably a permission error, as running ls works fine, but I have no idea how to fix it. (or if there's a better way of doing thing)
Running the command directly via the terminal works fine. I'm using PHP7.0 and Apache on Ubuntu 16.04, if it matters.
Edit
It works fine when I run it via the terminal doing php index.php when logged into www-data, but when I open it in the web browser it doesn't seem to execute /var/www/litecoin/bin/litecoin-cli at all.
Edit 2
When redirecting errors from stderr to stdout (2>&1), I get the following:
[0] =>
[1] =>
[2] => ************************
[3] => EXCEPTION: N5boost10filesystem16filesystem_errorE
[4] => boost::filesystem::create_directory: Permission denied: "/.litecoin"
[5] => litecoin in AppInitRPC()
[6] =>
www-data has full permission to /var/www/ though.
The major difference in such cases i find is the environment and the permissions. The error
[4] => boost::filesystem::create_directory: Permission denied: "/.litecoin"suggests that your php page is looking for the folderlitecoinin~or$HOMEand it is not set. That's why it becomes/.litecoinI created a simple
index.phpfile with below codeThe output of same on my default php is
As you can see there is
$HOMEand that could be one possible reason causing a issueSo you should make sure the correct environment is available to the executable that you are running. You can do that by doing
The output of the command shows it can see the updated variables
Figure out which environment variables you need and make sure they exists and try to set the paths so your executable used
/var/wwwor/var/www/htmlwhichever path it has access to and then executes the program