简体   繁体   English

使用PHP运行Debian Bash脚本

[英]Run Debian Bash script with PHP

I'm having trouble to run a bash file using PHP. 我无法使用PHP运行bash文件。

PHP File : PHP文件:

chdir('/var/www/PATH/inc/bash/');
exec('./status.sh argument, $output);

Bash File : Bash文件:

#!/bin/bash
echo 'test' >> /var/www/PATH/inc/bashOutput/test.txt

PHP File (ls -al handler.func.php) : PHP文件(ls -al handler.func.php):

-rw-r--r-- 1 root root 461 Jul  5 11:35 handler.func.php

Bash File (ls -al status.sh) : Bash文件(ls -al status.sh):

-rwxr-xr-x 1 root root 255 Jul  5 11:39 status.sh

Script is working using through root with SSH. 脚本正在通过SSH与root一起使用。

I'm not a pro on Linux. 我不是Linux上的专业人士。 But I think it's a problem come with the file owner. 但是我认为文件所有者会遇到问题。

But I have already done some damages in the past with "chown" so If it is indead the problem I would prefer some guidance from more experienced people. 但是我过去使用“小丑”已经造成了一些损失,因此,如果这个问题不受欢迎,我希望有经验的人提供一些指导。

Thanks for you help, Konorr. 谢谢您的帮助,Konorr。

Script is working using through root with SSH. There is the problem. 有问题。 When a PHP script run via a web request it usually runs as the user www-data. 通过Web请求运行PHP脚本时,它通常以用户www-data的身份运行。 In anycase <?php exec('./status.sh argument, $output);?> in a security hole. 无论如何,在安全漏洞中<?php exec('./status.sh argument, $output);?> Most server admins would have this disabled. 大多数服务器管理员将禁用此功能。

Your other option is to put sudo in your exec function exec('sudo bash /var/www/PATH/inc/bash/status.sh') . 您的另一个选择是将sudo放入您的exec函数exec('sudo bash /var/www/PATH/inc/bash/status.sh') Along with running the script with an absolute path bash /var/www/PATH/inc/bash/status.sh 使用绝对路径运行脚本bash /var/www/PATH/inc/bash/status.sh

Why can't you run a cron on your script? 为什么不能在脚本上运行cron?

Thanks for you answer but unfortunatly it didn't worked for me. 感谢您的回答,但是很遗憾,它对我没有用。

I search a litle more about file owners. 我搜索了一小部分有关文件所有者的信息。

I did few changes 我做了些改动

I made in these change : 我做了这些改变:

chown -R www-data:www-data /var/www/PATH
usermod -a -G www-data user
chgrp -R www-data /var/www/PATH
chmod 2750 /var/www/PATH
chmod 2750 /var/www/PATH/inc/bash

It wasn't yet working till I removed the sudo from the EXEC function. 直到我从EXEC函数中删除了sudo,它才起作用。 So I don't know from which point my problem was already fixed. 所以我不知道我的问题已经解决了。

I followed this article : www-data permissions? 我遵循了这篇文章: www数据权限?

Thanks for you time and in the hope it can help someone else. 感谢您的时间,希望它能对其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM