I have a project PHP in my droplet LEMP 16.04 with script python, when I run in terminal is ok, but when I run in the browser don’t execute the script.
My PHP
$vehicleJSON = shell_exec('python vehiclepy.py ' . $plate);
I’ve already tried:
Please help me kkkkk, thanks!
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
First off, be careful running shell_exec like this, If the user can change ** $plate** they would be free to run any command they like.
You may find if you can’t execute it when running via HTTP it may be a blocked function.
https://stackoverflow.com/questions/24999673/how-to-enable-shell-exec-and-exec-on-php
Do you have anything in your error logs?
Heya,
Executing a Python script via PHP can be achieved using functions like exec(), shell_exec(), or system(). However, it’s important to note that there are potential security risks when running scripts this way, especially if the script or parameters are influenced by user input. Always validate and sanitize user input and limit the potential operations the Python script can perform.
Here’s a basic example of how to execute a Python script using PHP:
script.py:# script.py
print("Hello from Python!")
You can execute this Python script from a PHP file using the shell_exec() function:
<?php
$output = shell_exec('python3 /path/to/script.py');
echo $output;
?>
Here’s a brief overview of the PHP functions you can use:
exec(): Executes a command, and returns the last line of the output.shell_exec(): Executes a command via the shell and returns the complete output as a string.system(): Executes a command and directly outputs the result.When using these functions:
www-data on Linux) has permissions to execute the Python script./usr/bin/python3 /absolute/path/to/script.py.Remember to enable the necessary permissions and ensure that the path to the Python interpreter is correctly specified. If PHP is in safe mode or functions like exec() are disabled (which is common in many shared hosting environments), this method will not work.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.