当前位置:网站首页>php执行shell脚本
php执行shell脚本
2022-07-26 09:23:00 【为天空着色】
原文:https://blog.csdn.net/zhangsheng_1992/article/details/52805760
php作为一门服务器端脚本语言,也是可以调用系统命令的,这其中就包括了执行shell脚本
编写test.sh脚本如下
#! /bin/sh
mkdir /www/a
很简单的一个shell脚本 创建一个文件夹
php脚本如下
system('/usr/bin/test.sh')
在运行前 首先需要验证的
1.test.sh脚本是否具有可执行权限
2.apache nginx phttp等web服务器是否有test.sh的权限
3.由于是同步文件命令 所以需要确保用户对要下载的目录有写入权限
4.由于我使用php版本高于5.4,已经删除了安全模式一说,说一也不存在运行在安全模式下
5.执行的脚本请使用绝对路径 这点很重要
但是 实际运行后 我发现输出了faild 说明脚本并没有执行成功 查了下原因 是因为apache用户不具备某些目录的执行权限(没个用户有没个用户的可执行目录,你的脚本需要在该目录下) 切换目录属主容易造成安全问题 在linux下 root用户是不受此限制的 所以 临时让用户可以切换到root权限 既可以简单的规避此类问题 如何操作呢?
首先 查看你的apache用户或者nginx php-fpm用户
可以使用 ps -ef | grep httpd 命令来查看 其他同理
经查我的apache用户为_www用户
然后 visudo 或者 vim /etc/sudoers 找到
## Allow root to run any commands anywhere
root ALL=(ALL) ALL这一行 在下边追加
_www ALL=(root) NOPASSWD:ALL
第一列是用户 第二个是可以切换到的用户 我这里写的是root 如果你想让随意切换可以在这里写All 第三个是可以执行的脚本路径 ALL 表示所有
最后 重启apache让用户重新获得权限
修改php脚本
system('sudo /usr/bin/test.sh')
运行 执行成功~~~
边栏推荐
- Mysql事务
- Thread Join 和Object wait 的区别
- 2022 chemical automation control instrument operation certificate test question simulation test platform operation
- Cat installation and use
- Use of off heap memory
- CF1481C Fence Painting
- 756. Serpentine matrix
- antUI中a-modal 拖拽功能制作
- OFDM 十六讲- OFDM
- Pat grade a A1034 head of a gang
猜你喜欢
随机推荐
Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
服务器内存故障预测居然可以这样做!
Pat grade a a1013 battle over cities
Redis principle and usage - installation and distributed configuration
Pat grade a A1034 head of a gang
QT | about how to use EventFilter
“could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32” 问题处理
围棋智能机器人阿法狗,阿尔法狗机器人围棋
2022 mobile crane driver test question simulation test question bank simulation test platform operation
JS closure: binding of functions to their lexical environment
暑假末尾学习笔记
What is the difference between NFT and digital collections?
省政府召开全省高温天气安全防范工作电视电话会议
使用openLayer画箭头
STM32+MFRC522完成IC卡号读取、密码修改、数据读写
volatile 靠的是MESI协议解决可见性问题?(上)
Byte buffer stream & character stream explanation
Two tips for pycharm to open multiple projects
多项式开根
Windows通过命令备份数据库到本地







![[shutter -- layout] detailed explanation of the use of align, center and padding](/img/01/c588f75313580063cf32cc01677600.jpg)

