PHP exec() from URL? - php

I have a php file with an exec() function that executes some unix commands when I do this: php file.php on terminal, but now I need to do the same from URL on the browser, that is: localhost/file.php
So, how can I achieve this?

It sounds like you need a web server. It's a huge topic so it can't be covered here.
However. If you just want to run a simple PHP server, try
$ php -S localhost:8000
From the root directory of your PHP application. It should be accessible in your browser by going to localhost:8000. Or to make it accessible on any interface:
$ php -S 0.0.0.0:8000
Note that this only work for newer versions of PHP (>=5.4).

Thanks for your answers. The problem was that the files which I used in the commands inside exec() were outside of the /var/www/html directory and apache was complaining about permissions and after I had to give them permissions with the chown command.

Related

Unable to run Bash Script from PHP Code out of HTML Page

having a small issue here.
Already tried many approaches I found on the web to resolve this, didn't get it to work so far.
I am trying to execute a server-side Linux Bash Script when a Button on a static HTML side is clicked.
It just doesn't work.
My setup is a nginx webserver running on ubuntu, got a static HTML page with PHP code within it:
Ubuntu Version: 18.04.2 LTS (Bionic Beaver) /
Linux Version: 5.4.0-42-generic /
Nginx Version: nginx/1.14.0 (Ubuntu) /
PHP Version: 7.4.11
Bash Script is stored in the same folder as the index.html for the website (/var/www/website).
For testing purposes, gave ownership if the script file to the user www-data and even gave full access to all users to the file (chmod 777 script.sh),
script file is also definitely executalble.
Also this works perfectly fine:
sudo -u www-data php -r "shell_exec('bash script.sh');"
PHP Code within the HTML file:
<?php
shell_exec('bash script.sh)';
?>
Tested with some echo's that this part gets executed. Also looks good.
But the script is never running ..
Would very much appreciate any hints, as you may be able to tell, my web skills are as bad as my scripting skills ;)
Thanks a lot and best regards
Juls
Is the shellscript executable without sudo? Maybe try
chmod +x script.sh

Could not open input file : localhost:8080

On Windows, to run the PHP web server from the command prompt I type:
php -s localhost:80800 -t public
And I get this error:
Could not open input file : localhost:8080
And yet cmd php test.php will echo the text. Why?
It was just the capital S.
php -S localhost:8888 -t public
Now I can see the page in a browser.
I had to cd to the right directory ...and public has an index.php file.
It will work without using XAMPP/WAMP or any other external server, as PHP has its own server inbuilt. You have done a mistake in typing s in php -s localhost:80800.
This command will work - php -S localhost:80800.
"s" in the command will be capital, not small...
You can not run localhost using native PHP. You need a web server like CAMP or WAMP and configure the port while installing this software.
It sounds like you are running PHP as a CLI (command-line interface).
As stated, there are WAMP stacks to install and configure. I would suggest looking online for one of the many tutorials. To install one, they will take you through the process as well as some of the advanced configuration to secure the installation from unwanted outside access.
PHP itself is not a web server, hence why you cannot just write php -s localhost:8080 -t public.
You will need to have a corresponding web server, e.g. Apache, and link this with your PHP, allowing PHP to process the files that Apache will serve on its server.
You may want to use XAMPP to do this, since it is an easy-to-use bundle for Apache, MySQL (a database system), and PHP.
Download it on its website.
try php -S localhost:8080 It's Will work but capital S not The Small one i mean

php shell_exec() command is not working

I am trying to run a .sh file from php.
I tried doing it with shell_exec(). but its not working
I refered many questions related to this in stack overflow but could not solve
my php code is(web.php)
<?php
echo shell_exec('/var/www/project/xxe.sh');
echo "done";
?>
only done is printed. but it is working from terminal(php /var/www/project/web.php)
In xxe.sh I am calling a python file
python vin.py
I have also changed the file permission to 777 for both .sh n .py files
please help
If you say it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().
See http://www.php.net/manual/en/ini.core.php#ini.disable-functions
Your apache's php.ini file may look something like
disable_functions=exec,passthru,shell_exec,system,proc_open,popen
Remove shell_exec from this list and restart the web server, although this is a security risk and I don't recommend it.
shell_exec might not know what directory to look in for your executable's location directory. What solved it for me was this before the shell_exec:
putenv('PATH=/usr/local/bin');
Then the terminal can find the executable. Also check permissions on every part of the command to make sure apache user has read and execute permissions.
If it works well in shell, I think apache is chrooted. So php can't find /var/...
Or user of httpd user does not have permission to enter /var/...
If you are good at PHP. Open dir /var/... And readdir() and check dir exists and check file exists.
This question might help you. scanning /home/ with opendir()
The problem is usually that when you exec code from within php it is run as the webservers user www-data in alot of linux distros. Normaly this user does not have an enviroment set up, and because of that no PATH. By using full paths in your files you can usually overcome this.
xxe.sh
/usr/bin/python /path/to/script/vin.py
While trying to run a script triggered by github post-receive webhook.
Here is where my project directory is located(cloned git repo):
/var/www/html/my-repo
I create a script inside the above directory called webhook.php:
<?php
#webhook.php
$cmd = shell_exec("git pull 2>&1");
#for debugging
echo $cmd;
?>
Execute the following command inside /var/www/html
sudo chown www-data:www-data -R my-repo/
Test it by going to http://www.myserver.com/my-repo/webhook.php
Add the path to your script to github webhooks.
I have been stuck in this problem for several hours.
I have thought about a solution.
1. move your script to a python file "script.py" and place this file to your server root.
2. shell_exec("python script.py");
Any way, it works for me.
On my host I had to give a different path for my php file to be executed from shell_exec().
This didn't work shell_exec('/usr/bin/php backgroundtask.php');.
While this did shell_exec('/opt/php/php-5.5.0/bin/php backgroundtask.php');.
You can visit this Reference.
I had the same issue because PHP backslashes.
PHP escapes the backslashes, so the command that reaches the shell
'COPY E:path1\path2\file.prn /B \127.0.0.1\"PRINTER NAME"'
so I gave command like this
'COPY E:\\path1\\path2\\file.prn /B \\\\127.0.0.1\"PRINTER NAME"'.
You have to double-escape the backslashes: once for PHP and once for the shell.

Running PHP script from command line

So I'm working with a designer on a website in PHP/MySQL and there are a few scripts that he would like to have to make life easier for him. He is pretty comfortable using the command line for stuff like git, SASS, node, etc. I would like him to be able to run my scripts like he would run a program, instead of running it through PHP.
So instead of this:
php /path/to/file/create_module.php module_name
I would like to do this:
myscript create_module module_name
Is it possible to do this with PHP on an Apache server? I know I will most likely have to modify the server to interpret it properly, which is fine. I just don't even know where to begin, and couldn't find what I needed on Google.
Your best bet would to be to create an alias.
So an alias of myscript would actually point to the command: php /path/to/file/create_module.php and then any extra arguments will be passed as typed.
In command line, do the following:
cd /etc/
nano bash.bashrc
At the very bottom of the file, add this line of text:
alias "myscript=php /path/to/file/create_module.php"
BASHRC is a script that is run on user login, so the alias will be recreated every time the user logs into the system.
I am not sure what you are looking for myscript to do, but to run a php script via the command line without specifying the php binary, just add a a shebang, like
#!/bin/env php
<?php
// The above expects env is in /bin
$foo = "bar";
Or the full path if you like
#!/usr/bin/php
Another option: Shell script wrapper
create a new text file in /bin or another directory in your PATH, name it how you would like to invoke your script and give it this content
#!/bin/bash
cd /path/to/your/php/scripts/folder
php script.php $*
don't forget to
chmod a+x /path/to/bash/script
The advantage of this is that your PHP script is run in the right directory where it may expect other resources to be that it depends on.
On A Linux Solution:
/usr/bin/php /path/to/file.php
On a Windows Solution:
C:\Path\To\PHPExe C:\Path\To\phpfile.php

Running PHP script with Crontab (Plesk / CentOS6)

As a customer in Plesk, I am attempting to run:
php -q /httpdocs/_external/export/test.php
From this tutorial: http://daipratt.co.uk/crontab-plesk-php/
I'm receiving the error
"php: command not found"
Is there something I need to enable from the main user or a different command I would need to use to run the script?
(also tried /bin/php with no luck, there is no php file in that dir)
"which php"
-/usr/bin/php
(when I use this dir I also get "no such file or dir" I guess since when I use / it's pulling from the customers root not the server root)
This answer will help you. My understanding is that Cron runs everything relative to itself, so you should always use absolute paths when running something from Cron.
Good luck, and happy holidays!

Categories