1. Problems and reasons
The reason why the PHP homepage on the Raspberry Pi cannot run commands or scripts with root permissions is not that PHP cannot be executed, but that Apache does not have enough permissions! ! ! !
2. How to run shell script or Python in PHP
PHP provides us with three functions: system(), exec(), and passthru() to call external commands.
Although these three Both commands can execute the shell commands of the Linux system, but in fact they are different:
system() outputs and returns the last line of shell results.不 Exec () does not output the results, and return the last line of shell results. All results can be saved into an array returned.
passthru() only calls the command and outputs the running result of the command directly to the standard output device as it is.
Same point: you can get the status code of command execution
Example: system("/usr/local/bin/webalizer/webalizer");
3, PHP executes root commands or scripts through sudo
1) Open the sudo command permissions for the Apache user.的 Modify the permissions control file of the SUDO/ETC/SUDOERS file. The Raspberry Pi uses the following command:
Sudo Nano/ETC/S Udoers or can also be edited by the following commands/ETC/SUDOERS File: sudo visudo
Add the following line: Open all permissions for this user
www-data ALL=(ALL) NOPASSWD: ALL#My Apache user is www-data
or nobody ALL=(ALL) NOPASSWD: ALL# If your Apache user is the default, then the user is nobody
You can also add the following content to open the permissions of a certain command:
ALL= (ALL) NOPASSWD: /usr/local/Apache/bin/Apache
ct1 restart2) Comment out the Defaults requiretty line in the file, otherwise
will appear sudo : sorry, you must have a tty to run sudo.
3) Save and exit The above introduces the program for PHP to execute root permissions through sudo on Raspberry Pi, including Apache and require content. I hope it will be helpful to friends who are interested in PHP tutorials.