PHP executes shell scripts with root privileges

Source: Internet
Author: User
Tags apache log

Turn a blog, before doing this thing for a long time, the results of this evening saw a life-saving blog, Instant happy ... Reproduced reproduced

Use sudo to give Apache user root execution permissions, which are documented below:

Using PHP to execute a shell script with root privileges requires the following steps: (All steps are my own experiment, if there is something wrong to point out, thank you!) )

1. Determine who your Apache execution user is. Note: Not necessarily is nobody, I install the httpd, my Apache user is daemon

2. Use Visudo to give root execution privileges to your Apache execution user and, of course, set no password. Note: For security reasons, it is best to create a new user, let him as Apache execution user (modify the httpd.conf file, I will point out later)

3. This step is simple, write your script, take advantage of PHP Exec,system ... function to execute.

The following is the detailed implementation process:

1. Check who your Apache execution user is: lsof-i:80 after running the result:

We can clearly see, httpd (that is, Apache) execution user is: Exec_shell (Note: This is my machine on the user after the changed, just to illustrate, your affirmation is not this!). )

Lsof is the list of file abbreviation, is to list the current system open file tools, about his specific use of the method can be consulted: Http://club.topsage.com/thread-234763-1-1.html said the more good

Determine who the Apache performer is on your Linux, and below for security reasons, create a new user to modify the Apache execution user to our new user.

2. Create a new Apache execution user

Useradd Your_exec_user We know that when creating a user, a user group with the same user name will be created by default, which means we now have a Your_exec_user user group.

Let's change the Apache configuration file so that its execution user changes to the user Your_exec_user we just created:

Vi/home/houqingdong/httpd-exe/config/httpd.conf (This is the directory where your Apache resides)

Find the place below and modify the new user for you: Your_exec_user

Restart Apache:/home/houqingdong/httpd-exe/bin/apachect1 Restart-------------> After restarting you can use: lsof-i:80 to see 。

3. Execute Visudo (or vi/etc/sudoers), give your_exec_user root privileges, and do not require a password, there is an important change (I am troubled by this place)

Visudo find this place, add Your_exec_user, and set no password

I was done here before the execution of the PHP script to go, the results have been created unsuccessful, and it is very depressing that I switched to your_exec_user users under the direct execution can be executed successfully. Later, I looked at Apache log files and found:

It is evident here that when Sudo is executed, it is necessary to have a TTY to run sudo, to know where the problem is, and to solve the problem: Vi/etc/sudoers will comment out the following sentence:

This is because by default the execution of Sudo requires a terminal, which is commented out here. Next, write your shell script and PHP commands.

Ok! This is the above blog, the perfect solution to the inability to run the shell script problem!

PHP executes shell scripts with root privileges (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.