Home Backend Development PHP Tutorial How do PHP scripts get permission to control Nginx startup and stop?

How do PHP scripts get permission to control Nginx startup and stop?

Apr 01, 2025 am 09:24 AM
docker nginx operating system php script

Solve the problem of PHP script controlling Nginx startup and stop permissions

This article discusses how to use PHP scripts to control the start and stop of an Nginx server. Many users try to execute system commands using shell_exec() function, but the command execution fails due to insufficient permissions. For example, shell_exec('service nginx stop') seems to be successful, but the Nginx service does not actually stop.

How do PHP scripts get permission to control Nginx startup and stop?

The root cause of the problem is that PHP scripts are usually run as non-root users (such as www-data), and the service command requires root permissions to operate the system. Even if the command returns "done", it does not mean that the service has been successfully stopped.

Solution: Grant sudo permissions

To solve this problem, it is necessary to give PHP runners (such as www-data) permission to execute sudo commands without a password. This requires careful editing of the /etc/sudoers file. It is strongly recommended to use the visudo command to edit the file to avoid file corruption:

 sudo visudo
Copy after login

Add the following line at the end of the /etc/sudoers file:

 <code># 允许www-data用户无需密码执行所有sudo命令www-data ALL=(ALL) NOPASSWD: ALL</code>
Copy after login

Note: ALL=(ALL) NOPASSWD: ALL gives www-data users extremely high permissions. Please adjust the permission scope according to actual needs. Only necessary commands are allowed to be executed, such as www-data ALL=(ALL) NOPASSWD: /usr/sbin/service nginx * Only operation of nginx services are allowed.

After the modification is completed, the commands in the PHP code should be changed to:

 $command = 'sudo service nginx stop';
$output = shell_exec($command);
echo $output; // Output command execution result
Copy after login

Additional steps in the Docker environment:

In the Docker container, you need to install sudo first:

 apt-get update && apt-get install -y sudo
Copy after login

Then, follow the steps above to modify the /etc/sudoers file. Make sure that sudo is correctly configured in the container and that the /etc/sudoers file modification takes effect.

Through the above steps, the PHP script can obtain sufficient permissions to control the start and stop of the Nginx service. Remember, modifying the /etc/sudoers file requires careful operation, as incorrect configuration may lead to system instability. It is recommended to back up the file before modification and carefully check the correctness of the configuration after modification.

The above is the detailed content of How do PHP scripts get permission to control Nginx startup and stop?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to run the h5 project How to run the h5 project Apr 06, 2025 pm 12:21 PM

Running the H5 project requires the following steps: installing necessary tools such as web server, Node.js, development tools, etc. Build a development environment, create project folders, initialize projects, and write code. Start the development server and run the command using the command line. Preview the project in your browser and enter the development server URL. Publish projects, optimize code, deploy projects, and set up web server configuration.

Can you use Tauri to develop desktop applications if you don't know Rust? Can you use Tauri to develop desktop applications if you don't know Rust? Apr 04, 2025 pm 11:42 PM

The impact of Rust language proficiency on desktop program development under the Tauri framework Tauri is a desktop application development framework built using Rust, thanks to its lightweight and...

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

C language conditional compilation: a detailed guide for beginners to practical applications C language conditional compilation: a detailed guide for beginners to practical applications Apr 04, 2025 am 10:48 AM

C language conditional compilation is a mechanism for selectively compiling code blocks based on compile-time conditions. The introductory methods include: using #if and #else directives to select code blocks based on conditions. Commonly used conditional expressions include STDC, _WIN32 and linux. Practical case: Print different messages according to the operating system. Use different data types according to the number of digits of the system. Different header files are supported according to the compiler. Conditional compilation enhances the portability and flexibility of the code, making it adaptable to compiler, operating system, and CPU architecture changes.

How to control the top and end of pages in browser printing settings through JavaScript or CSS? How to control the top and end of pages in browser printing settings through JavaScript or CSS? Apr 05, 2025 pm 10:39 PM

How to use JavaScript or CSS to control the top and end of the page in the browser's printing settings. In the browser's printing settings, there is an option to control whether the display is...

How to use locally installed font files on web pages? How to use locally installed font files on web pages? Apr 05, 2025 pm 10:57 PM

How to use locally installed font files on web pages Have you encountered this situation in web page development: you have installed a font on your computer...

What are the reasons and solutions for the server file that cannot be downloaded after sftp.json configuration? What are the reasons and solutions for the server file that cannot be downloaded after sftp.json configuration? Apr 04, 2025 pm 06:54 PM

Solution to the problem that the server file cannot be downloaded after SFTP.json configuration After configuring the sftp.json file, users may encounter the inability to download the target server file...

See all articles