: This article mainly introduces the program for executing the root permission through sudo on php on Raspberry Pi. if you are interested in the PHP Tutorial, refer to it. 1. problems and causes
The reason why the PHP homepage on Raspberry Pi cannot run commands or scripts with root permissions is not that PHP cannot run,ApacheThe permission is insufficient !!!!
2. PHP method for running shell scripts or Python
Php provides the system (), exec (), passthru () functions to call external commands.
Although these three commands can execute shell commands in linux, they are actually different:
System () outputs and returns the last shell result.
Exec () does not output results. The last shell result is returned. all results can be saved to a returned array.
Passthru () only calls the command and directly outputs the command running result to the standard output device.
Similarities: both of them can obtain the command execution status code.
Example: system ("/usr/local/bin/webalizer ");
3. PHP uses sudo to execute the root command or script
1) open the sudo commandApacheUser permissions.
Modify the sudo permission control file/etc/sudoers file. Raspberry Pi uses the following command:
Sudo nano/etc/s udoers or you can run the following command to edit the/etc/sudoers File: sudo login do
Add the following line: Open all permissions for this user
Www-data ALL = (ALL) NOPASSWD: ALL # MyApacheThe user is www-data.
Or nobody ALL = (ALL) NOPASSWD: ALL # if yourApacheIs the default user, then the user is nobody
You can also add the following content to open the permission for a command:
Nobody ALL = (ALL) NOPASSWD:/usr/local/Apache/Bin/ApacheCt1 restart
2) comment out the Defaults in the file.RequireTty line. otherwise
Sudo: sorry, you must have a tty to run sudo.
3) save the disk and exit
The above describes the programs on Raspberry Pi that execute root permissions through sudo in php, including Apache and require, and hope to help those who are interested in PHP tutorials.