I;m trying to run phantomjs from php. To check correct path and permissions, as first I execute this commands:
$output = shell_exec('ls /opt/phantomjs-1.9.2-linux-x86_64/bin/ -l');
echo "<pre>$output</pre>";
Result:
-rwxrwxrwx. 1 root root 38526976 Sep 7 11:17 phantomjs
drwxrwxrwx. 2 root root 4096 Feb 10 15:44 savedpages
-rwxrwxrwx. 1 root root 803 Jan 19 18:36 saveonepagelin.js
-rwxrwxrwx. 1 root root 4074 Jan 19 19:00 test.js
But when I run:
$output = shell_exec('/opt/phantomjs-1.9.2-linux-x86_64/bin/phantomjs --version');
echo "<pre>$output</pre>";
Output is empty :( why? Thanks!
My first guess would be a permission problem. Is the www-data user (or whichever user your server is called) allowed to run the program?
Do you get anything returned if you place $output in var_dump() instead?
I.e.
$output = shell_exec('/opt/phantomjs-1.9.2-linux-x86_64/bin/phantomjs --version');
var_dump($output);
I got the same error, and I try to modify the dir where I execute the phantomjs and set the mode with 777, then it worked. I guess the root cause if the owner of the php-fpm doesn't have right to write image into the dir.
my code for refer:
$output = passthru('phantomjs echarts-convert.js -options ' . $options . ' -outfile line.png');
echo $output;
Related
EDIT 01 UPDATE:
ERROR Showing UP Warning: mkdir(): Permission denied in
/var/www/html/projeto01/index.php on line 7 Sucess. Dir Created
EDIT 02
Change to FEDORA and is working.
Thank you guys
before submitting this topic I read and tried all the topics here. No success.
The environment is working great until here. Php 7.4, Apache, Mysql Workbench, Atom... i can code and select from database, everything was great.
The code is working with no error execpt it did not create the directory. :/
If i create the dir manually, return the message that it already exist.
After read a lot of topics and get help from facebook.
I did change on group polices. Not worked.
chmod on the folder, not worked.
On CENTOS the apache user is APACHE instead www:data
ls -l /var/www return:
drwxr-x---. 2 apache apache 6 Nov 16 13:19 cgi-bin
drwxrwxrwx. 3 apache apache 39 Mar 19 13:54 html
ls -l /var/www/html return:
-rw-r--r--. 1 root root 86 Mar 19 00:09 info.php
drwxrwxrwx. 2 apache apache 23 Mar 19 13:58 projeto01
ls -l /var/www/html/projeto1 return:
-rwxrwxrwx. 1 root root 148 Mar 19 15:14 index.php
Thanks for the help!
My code is:
> <?php
$name = "images";
if (!is_dir($name)) {
mkdir($name);
echo "Sucess. Dir Created.";
} else {
echo "Dir already exist. $name";
}
?>
So this is my code for the raspberry pi to get a still shot from the raspicam and save it on a directory,
<?php
exec('raspistill -n -hf -o /var/www/img/image.jpg --timeout 1');
?>
I have given the ownership and the permission to read/write in that forlder using -R. so my ls -al in /var/www is this
drwxr-xr-x 3 www-data www-data 4096 Jun 19 08:05 .
drwxr-xr-x 12 root root 4096 Jun 19 05:54 ..
-rwxrwxrwx 1 www-data www-data 74 Jun 19 08:30 getImg
drwxrwxrwx 2 www-data www-data 4096 Jun 19 09:21 img
-rw-r--r-- 1 root root 70 Jun 19 10:07 index.php
getImg is the script i tried to run the script as a file like shell_exec('/bin/bash ./getImg'); that also doesn't work.
i have added /bash/bin and tried to run the script without using the script file too but that doesn't get the results.
How ever when i try to run the php file in the terminal, it creates the image as it normally should. So i figure this must be a permission issue, but what else should i do with the permissions? I have given all the rights to the directory.
EDIT
So I have found a workaround to this. since I don't know what the cause for the problem, i'd not mark this as an answer, but please vote it to appear at the top.
I now execute the script using the cgi scripts. I have created a shell script in the /usr/lib/cgi-bin/
#!/bin/bash
echo "Content-type:text/html\n"
sudo raspistill -vf -n -o /var/www/img/image.jpg --timeout 1200 --metering matrix
echo "Status: 204"
I saved this as capture and made this executable, did nothing with the permissions though.
sudo chmod +x capture
now when i open the link http://192.168.1.85/cgi-bin/capture the browser will still get me a 500 internal server error message. how ever, the image would still be created.
I would now need to get the 500 internal server error to be fixed.
[I'd add this as a comment but don't have enough points for it]
if you use the optional parameters $output and $return_var to capture the output and return value what do you get?
string exec ( string $command [, array &$output [, int &$return_var ]] )
does your command rely on environment variables that may be available when you run it as your user but not as www-data? (you can use the env command to check that)
does it still work if you run it via terminal after switching user to www-data?
This is how my python code looks like:
[root#localhost html]# vi brow1.php
<?php
echo exec("whoami");
echo exec("ls -l brow1.php") . "<br>";
$command = escapeshellcmd('/usr/temp_rohit.py');
shell_exec($command);
?>
I want to call python script from this php.
If i run the php script then it executes the python code but when run from the browser it does not execute the python script.
python script has all the required execute permission.
The output on the web browser is:
apache
-rwxrwxrwx. 1 root root 219 Nov 26 23:03 brow1.php
-rwxrwxrwx. 1 root root 115 Nov 26 22:38 /usr/temp_rohit.py
I am stuck at this point, Please help!
I have to execute a php script (a.php) in the background. I tried this but it's not working:
<?
$cmd = "php /home/megad404/www/prove/a.php &> /dev/null &";
exec('/bin/bash -c "'.$cmd.'"',$output,$return);
if($return===0)
{
echo 'Successful';
}
else
{
echo 'Unsuccessful';
}
?>
It returns "Successful" but it doesn't execute a.php
a.php:
<?
file_put_contents(date("s"),"");
sleep(5);
file_put_contents(date("s"),"");
sleep(5);
file_put_contents(date("s"),"");
?>
a.php writes a file every 5 second and it works fine, except if I try to execute it in the background with the first script.
You can try adapt mi script.
Look a command shell_exec() not exec(). First return all , second only last line.
function run_in_background($Command, $Priority = 0) {
if($Priority)
$PID = shell_exec("nohup nice -n $Priority $Command > /dev/null & echo $!");
else
$PID = shell_exec("nohup $Command > /dev/null & echo $!");
return($PID);
}
//Verifies if a process is running in linux
function is_process_running($PID) {
exec("ps $PID", $ProcessState);
return(count($ProcessState) >= 2);
}
and example
$PIDPHP=run_in_background("php -S 127.0.0.1:18086 ".__DIR__."/index.php"); // or any other process.
if (is_process_running($PIDPHP)){
exec("kill $PIDPHP");
}
You could also look into using real Posix/PCNTL functionality to actually detach the script to background, eg. with pcntl_exec and pcntl_fork(). This is after my opinion the right way to handle background scripts that runs for a longer period of time as you can communicate with the child/process to get updates, status and so on and even have them understand real signal handling.
PCNTL - http://www.php.net/manual/en/book.pcntl.php
POSIX - http://www.php.net/manual/en/book.posix.php
Cheers
This just worked for me:
<?php
$cmd = "/usr/bin/php /home/auser/a.php &> /dev/null &";
exec($cmd,$output,$return);
sleep(30);
if($return===0)
{
echo 'Successful';
}
else
{
echo 'Unsuccessful';
}
?>
I saved it as runa.php and ran it from the command window as php runa.php.
It produced 3 files.
running a.php also worked from the cron job:
]$ crontab -l
18 * * * * /usr/bin/php /home/auser/a.php
I put the script in a web directory and find that I have some writing problems. What can you see in the server log?
sudo tail -f /var/log/httpd/error_log
And what if you hit a.php from the web browser? Because you mention the script is 755, but how about the directory. Maybe it needs to be 775 or 777 for testing so that the script can write a file?
For testing I created a sub directory "output" and changed a.php
<?php
ini_set('date.timezone','America/New_York'); //without this it makes extra messages
error_log("a.php putting contents", 0);
file_put_contents("output/".date("s"),"");
sleep(5);
file_put_contents("output/".date("s"),"");
sleep(5);
file_put_contents("output/".date("s"),"");
error_log("a.php done", 0);
?>
It was unable to write files until I gave write permission to the ouput folder
sudo chmod 777 /var/www/html/output
Then I found out the apache user is writing the files:
~]$ sudo ls -l /var/www/html/output/
total 0
-rw-r--r--. 1 apache apache 0 Apr 18 11:38 00
-rw-r--r--. 1 apache apache 0 Apr 18 11:38 05
-rw-r--r--. 1 apache apache 0 Apr 18 11:37 55
So I changed the owner of output, in order to tone down the permissiosn again.
~]$ sudo ls -lu /var/www/html/ | grep output
drwxr-xr-x. 2 apache root 4096 Apr 18 12:21 output
This also works now:
~]$ sudo ls -l /var/www/html/output
total 0
-rw-r--r--. 1 apache apache 0 Apr 18 12:21 44
-rw-r--r--. 1 apache apache 0 Apr 18 12:21 49
-rw-r--r--. 1 apache apache 0 Apr 18 11:37 55
Inside my automation.php controller, I have the following function:
public function deploy_test() {
echo json_encode(system("python --version"));
}
When the user wants to deploy a test, by clicking a test button in the webpage, he would be able to accomplish such a task.
However, when I click the test button, my output is:
""
Meanwhile, when I execute the same function with the command:
public function deploy_test() {
echo json_encode(system("ls -l"));
}
I'm getting:
total 32
drwxr-xr-x. 15 philippe philippe 4096 Mar 4 16:48 application
drwxrwxr-x. 2 philippe philippe 4096 Mar 4 17:28 css
-rw-r--r--. 1 philippe philippe 6357 Jan 30 11:53 index.php
drwxrwxr-x. 2 philippe philippe 4096 Feb 27 15:38 js
-rw-r--r--. 1 philippe philippe 2496 Jan 30 11:53 license.txt
drwxr-xr-x. 8 philippe philippe 4096 Jan 30 11:53 system
drwxr-xr-x. 12 philippe philippe 4096 Jan 30 11:53 user_guide
Could someone please help me to get that straighten out?
The problem is not with your code or PHP.
The problem is with your permissions.
php uses permissions which are set in the env-vars of apache.
Which is ideally set as :
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
under your apache2 / httpd conf file.
For example:
Try running:
<?= `whoami` ?>
via your shell and via your browser.
Your browser will probably say www-data and shell will say your username or if you are on AWS - default, you would get root
You do not have to use system() , use exec() instead.
We should be close as :
echo json_encode(exec("python --version"));
Performing operations will require you to have correct User and Groups set.
Look up for : In the shell, what does " 2>&1 " mean?
So your code should be :
echo json_encode(exec("python --version 2>&1"));
Hope it helps!
This works fine for my production server
public function deploy_test() {
echo json_encode(system("python --version 2>&1"));
}
with the output
Python 2.7.3
"Python 2.7.3"
Output of the unix command printed twice as system() itself outputs the result to browser. So exec() can be used in the place of system to avoid this.
public function deploy_test() {
echo json_encode(exec("python --version 2>&1"));
}
which outputs
"Python 2.7.3"
as expected.
I suspect it is not in the path. Try:
Type the full path to the python command (such as /usr/bin/python --version)
Find out with the command which, 'which python'
try executing your script from the command line, 'php script.php' => sometimes the web
server sets up things differently
make sure the error displaying is enabled with
ini_set('display_errors',1);
system returns only last line.
system
public function deploy_test() {
system("python --version", $out);
echo json_encode(implode($out));
}
It's a bit of hack, but you can find out the version number with 1 decimal even if you aren't allowed to execute python (which is the case with the 'user' CI).
//find python path
exec("which python", $path);
//show all subdirs in python
exec("ls -l ".$path[0]."*", $output);
$output = implode("\n", $output);
//preg match on version numbers
preg_match_all("#python(\d+(?:\.\d{1,2})?)#", $output , $matches);
$installed_versions = $matches[1];
//sort in reversing order
$versions_sorted_desc = array_reverse($installed_versions);
//latest version is element 0
$latest_version = $versions_sorted_desc[0];
echo $latest_version;