简体   繁体   English

从PHP运行Bash脚本以创建VM

[英]Running Bash Script from PHP which creates VMs

Currently I am working on a project which require to create a vm from a click on the webpage. 目前,我正在一个项目中,该项目需要通过单击网页来创建虚拟机。

This is my bash script which i am using to create a vm and it is working perfect but when called through php i am unable to run this script. 这是我用来创建虚拟机的bash脚本,它运行完美,但是当通过php调用时,我无法运行此脚本。

 #!/bin/bash
 set -e
 if [ ! -f "/tmp/value.dat" ] ; then
      value=0
 else
      value=`cat /tmp/value.dat`
 fi
 value=`expr ${value} + 1`
 echo "${value}" > /tmp/value.dat
 qemu-img create -f raw /var/lib/libvirt/images/disk$value.img 5.1G
 virt-install --name=instance$value --ram=1024 --arch=i686 --vcpus=1 --os-type=linux  --disk path=/var/lib/libvirt/images/disk$value.img,bus=virtio,format=raw --location ftp://10.21.40.227/ --extra-args "ks=ftp://10.21.40.227/ks.cfg"  --autostart

This is the php script that i am trying to run 这是我正在尝试运行的PHP脚本

 <?php
    $bashfile = shell_exec("sudo /root/Desktop/install.sh");
    echo "<pre>$bashfile<pre>";
 ?>

This is the entry in the sudoers file by me 这是我在sudoers文件中的条目

apache ALL=(ALL) NOPASSWD: ALL apache ALL =(全部)NOPASSWD:全部

Still i am unable to run the bash script via php suggest some solutions. 我仍然无法通过php运行bash脚本,建议一些解决方案。

The problem may be that $PATH is not set correctly when it is not called from a shell. 问题可能是当未从shell调用$PATH时,未正确设置$PATH Try using absolute paths for qemu-img and virt-install . 尝试对qemu-imgvirt-install使用绝对路径。

Another problem I see here: you really don't want Apache to be able to run any command through sudo , do you? 我在这里看到的另一个问题:您真的不希望Apache能够通过sudo运行任何命令,是吗? Change your sudoers line to this to limit it to your script only: 将您的sudoers行更改为仅将其限制为您的脚本:

apache  ALL=NOPASSWD:/root/Desktop/install.sh

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

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