Running a ruby script from php - php

Ruby is installed here:
.rvm/rubies/ruby-2.2.1/bin
Ruby script:
puts "Hello world"
Php script:
<?php
$cmd = "ruby /home/balint/rubytest.rb";
echo system($cmd);
?>
I can run sudo php /home/name/public_html/phprubytest.php from CLI but not from the browser.
I reach the server via Putty and use Filezilla to put all my website-related files to the public_html folder.
I logged out the error and it turned out I have a permission error:
ruby: Permission denied -- /home/balint/rubytest.rb (LoadError)
This means as a user running the php script from the browser I have no access to that directory on the server.
Any ideas?

exec(rubyfile.rb);
Just add this code and it will execute your ruby file.

Related

Error Executing Ruby File via exec()

I need to execute a Ruby file via PHP on Debian 8 & Apache server.
The current code looks like
exec('/usr/local/rvm/rubies/ruby-2.1.1/bin/ruby /var/www/project/_backend/pull-data.rb clients', $null, $returnVal);
echo intval($returnVal);
In this example, $returnVal (see exec()) returns 1 which could be missing permissions for executing the specific file.
The current permissions for pull-data.rb:
-rwxr-xr-x 1 www-data www-data pull-data.rb
Watching the Apache error log a Load Error occures when the php scripts calls the ruby exec command:
`require': cannot load such file -- mysql2 (LoadError)
The MySQL2 gem is installed and executing the Ruby file via the shell ruby command just works fine.
Thanks,
Roman

PHP script running at Apache and Linux doesn't create file

I need some help as my script doesn't create a file. I want to run this script at cronjob. I am using Debian Linux with PHP and Apache.
My script is locate at /var/www/myapplication folder. I want to create / write a file at /var/www/myapplication/testfolder.
The commands I use in the php script is:
echo `whoami`;
exec ('whatever >> testfolder/testing.txt');
Folder rights:
drwxrwxrwx 2 www-data www-data 4096 Jul 27 09:55 testfolder
When I run the script directly from browser (http://127.0.0.1/myscript.php)
it doesn't create the file (testing.txt)
echo whoami says: www-data
When I run the script from the server with root rights (su):
php -q /var/www/myapplication/myscript.php >>log3.txt
it creates the file (testing.txt)
echo whoami says: root
log3.txt is created at /var/www/myapplication folder
When I run the script from crontab:
/usr/bin/php -q /var/www/myapplication/myscript.php >>log3.txt
it doesn't create the file (testing.txt)
echo whoami says: root
log3.txt is created at /root folder
Why isn't testing.txt created my I run my script from crontab as a cronjob ?
You shouldn't be executing operating system commands directly, there are already functions and objects avaliable in PHP for writing and read files. As well, you shouldn't be giving them permission to be writing files to /. Even though you're running it from localhost, if you ever expose the service you're allowing public users to run operating system commands.
You also mention in your comment that you want to redirect the output of 'whatever' to a file, but I still don't see why you couldn't do this with the PHP provided functions.
Regardless, to answer the original question when you run the script via localhost you're accessing it via Apache which means the user www-data is running the PHP script. When you access it via your cronjob you're calling PHP from the commandline and another user is accessing the service.
You can see the error that is being returned from attempting to write to the file by logging in with the user running the cronjob and using echo exec(...).

Executing .exe file using PHP on Linux server

I'm new in using Linux, I'm trying to write a PHP code which can run .exe linux compatible file, I've made a short shell script
hello bash script:
#!/bin/bash
./program.exe file.mp4 // file.mp4 is an an input for .exe
echo "Hello World!"
shell.php:
<?php
$output = exec ("./hello ");
echo "<pre>$output</pre>";
?>
Now when I run shell.php using web browser it shows Hello World! but the .exe doesn't run, however when I run php using terminal command php shell.php, It works fine.
I think I'm having problems with permissions but I'm new with Linux and I don't know how to solve this.
Update:
I ignored the shell script and I used
<?php
$output = shell_exec ("cd /var/www/ && ./program.exe file.mp4 2>& " );
?>
also I granted access to program.exe
chmod 777 program.exe
the error I receive in the browser :could not open debug.bin!
use the absolute path to hello executable exec("sh path/to/the/file")
I'm using something similar to call an app compiled with mono on a remote ubuntu webserver and return it's output to the calling script.
For any of this to work properly wine needs to be already installed.
On Ubuntu systems try:
sudo apt-get -y install wine
You then need to know the owner of the web server process. If you are running the apache web server try the following:
cat /etc/apache2/envvars | grep "RUN"
The output will look something like this:
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_RUN_DIR=/var/run/apache2$SUFFIX
Now that you have the name of the process owner, which in this case is www-data you should ensure the file is owned the user and its group:
sudo chown www-data /var/www/program.exe
sudo chgrp www-data /var/www/program.exe
Finally, we can invoke the application from inside our PHP script by passsing it as a parameter to 'wine' and using its full file path.
<?php
$output = shell_exec("wine /var/www/program.exe file.mp4" );
?>
Any output from the above shell command sent to the command line will be saved in the PHP script variable $output.
It looks like you are trying to do some output redirection with your use of program.exe file.mp4 2>& so I've left that off of the example for clairity.
Try using the absolute path, such as exec("sh /path/to/file")
Generally, php is run as www or apache, so make sure that the execute access permission is granted to all user.

Interact with external commands via shell_exec

Recently I installed Android SDK on my CentOS server because of working with aapt, the aapt works great in command line via Putty SSH application.
Now, I want to run aapt commands through php shell_exec method.
My Android-SDK has been installed on \ipl\android-sdk\platform-tools\aapt, but I can't interact with it through shell_exec method.
Code:
$out = shell_exec("/ipl/android-sdk/platform-tools/aapt d badging t.apk 2>&1");
var_dump($out);
Result:
string(60) "sh: /ipl/android-sdk/platform-tools/aapt: Permission denied "
Any ideas?
Check if the file has enough permissions and if the user is able to execute the file.
Hope it helps

Can't execute external program through system()?

I tried to compile my latex file in php script, but it can't call xelatex.
In php script:
system("/usr/bin/whoami");
system("/usr/bin/xelatex foo.tex 2>&1");
output:
myuser
sh: 1: /usr/bin/xelatex: not found
But in my terminal:
$ /usr/bin/whoami
=> myuser
$ /usr/bin/xelatex foo.tex
This is XeTeX, Version 3.1415926-2.2-0.9995.2 (TeX Live 2009/Debian)
...(successful output)...
I run php as myuser, and pass system() absolute path. And I turn safe_mode off. Why can't I still execute external programs?
Finally I contacted my system administrator and found the problem. The machine is in a NFS, so the apache and login shell is on different machines. There is no xelatex on the machine where apache is running.
Check the permission of the directory from where you running you PHP code. check for myuser permissions
just for verification try it with root.
Hope this help
Are you sure it's not the file 'foo.tex' which it is unable to find? Try having the shell output to a file, e.g. system("/usr/bin/xelatex ./foo.tex > ./test.out"); and see what luck you get then.

Categories