[Django]-Executing python script from a PHP site β€” do I need Django? Can it be done on a raspberry pi?

3πŸ‘

βœ…

No, you do not need django at all.

If all you want to do is execute a Python script from PHP – assuming you have already written the script and stored it somewhere:

First, assign execute permissions on the Python script to the user that is running the PHP code. Normally, this is the same user that is running Apache. Usually this is called www-data or apache or something similar. The user will be listed in the Apache configuration file.

Then, on your PHP side, all you really need is exec:

<?php

    exec('/path/to/python /path/to/your/script.py')

?>
πŸ‘€Burhan Khalid

1πŸ‘

If the shell_exec function is allowed on your server, you can use that to run your Python script through Bash. shell_exec returns the output of the bash call. The only thing you have to make sure of is that shell_exec isn’t disabled in your server’s php.ini file (look for the line disable_functions shell_exec).

If your python script is called mypythonscript.py and is in the same directory as the PHP file, you can run it like this:

<? shell_exec('python mypythonscript.py'); ?>

Leave a comment