在PHP中以root身份運行外部命令[轉(zhuǎn)自linuxaid]
Jun 21, 2016 am 09:12 AMHunte 2001年4月15日
在PHP中運行只有root用戶才可以運行的外部程序,一直是個老問題,用常規(guī)的辦法很難實現(xiàn)。這是因為一般情況下,PHP是作為APACHE的一個模塊的,也就是說,PHP是APACHE的一部分,而APACHE除了suEXEC機制外,是不能以不同的用戶ID來執(zhí)行命令的,但suEXEC機制只能CGI有效。
網(wǎng)上曾經(jīng)有一篇文章,說用調(diào)用"su - -c COMMAND"可以實現(xiàn),但經(jīng)過多次試驗,發(fā)現(xiàn)不行,因為su命令必須在STDIN上輸入root的密碼。
怎么辦?用常規(guī)的方法難以奏效,只能再想其它的方法了。成功的關(guān)鍵在于能有一個可以切換用戶ID但又可以在命令上輸入密碼(或不用輸入密碼)的工具。有這樣的工具嗎?有,它就是super。
下面就具體說說如何來做?
要注意的是,安裝和配置super,都要以root身份來進行。
第一步,切換到root下
第二步,安裝super
先到ftp://ftp.mdtsoft.com/pub/super下載super-3.14.0-1.i386.rpm。這是一個RPM文件,其它包括了兩個工具:setuid和super,以及它們的文檔和man手冊。用下面的命令將它安裝到系統(tǒng)中:
% rpm -Uvh super-3.14.0-1.i386.rpm
你還可以用這個命令來查看這個RPM中的文件:
% rpm -qpl super-3.14.0-1.i386.rpm
從結(jié)果可以看到,兩個工具都將被安裝到/bin目錄下。
第三步,配置super
super的配置文件是/etc/super.tab。這是一個文本文件,格式也比較復(fù)雜。不過,我們這里只要很簡單的加上幾行就可以了。至于詳細(xì)的說明,可以通過man super.tab來查看。
假設(shè)運行Apache的用戶是nobody,我們欲通過super來增加系統(tǒng)用戶(調(diào)用useradd命令),那么我們只要在super.tab文件中加入以下這行:
auser /sbin/useradd nobody,hunte
第一段是super能夠識別的命令的別名;第二段是該別名所對應(yīng)的系統(tǒng)命令的全路徑;第三段是可以運行該命令的用戶列表,用逗號分隔。這里除了nobody外,還一個叫hunte的普通用戶,是用于下面的測試。當(dāng)然,你應(yīng)該用你系統(tǒng)中有的任意一個普通用戶。
至此,super的配置就算好了。
第四步,測試
以第三步中指定的非nobody用戶登錄,運行:
% /bin/super auser testuser
如果前面的配置沒什么錯誤的話,用戶testuser應(yīng)該是成功地創(chuàng)建了??梢杂茫?br>% cat /etc/passwd | grep testuser
命令來驗證一下。
第五步,在PHP中調(diào)用該命令
下面是PHP代碼:
if ($username)
{
//應(yīng)該檢查新用戶是否已經(jīng)存在
echo '正在創(chuàng)建用戶...';
system(escapeshellcmd("/bin/super auser $username"));
}
?>
使用super,使得在PHP中以root身份運行外部命令不再是難事。試試看吧。
測試環(huán)境:RedHat Linux 7.0 (Kernel 2.4.3) + Apache 1.3.9 + PHP 4.0.4pl1
chpasswd.cgi是一個修改linux用戶密碼的cgi程序,彌補更改密碼需要兩次輸入密碼的缺陷

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

The core role of Homebrew in the construction of Mac environment is to simplify software installation and management. 1. Homebrew automatically handles dependencies and encapsulates complex compilation and installation processes into simple commands; 2. Provides a unified software package ecosystem to ensure the standardization of software installation location and configuration; 3. Integrates service management functions, and can easily start and stop services through brewservices; 4. Convenient software upgrade and maintenance, and improves system security and functionality.

When choosing an AI writing API, you need to examine stability, price, function matching and whether there is a free trial; 2. PHP uses Guzzle to send POST requests and uses json_decode to process the returned JSON data, pay attention to capturing exceptions and error codes; 3. Integrating AI content into the project requires an audit mechanism and supporting personalized customization; 4. Cache, asynchronous queue and current limiting technology can be used to optimize performance to avoid bottlenecks due to high concurrency.
