How to execute a shell script in PHP?

前端 未结 4 1831
暖寄归人
暖寄归人 2020-12-03 01:16

I have a script in /var/www/myscript.sh which creates folders and runs the command svn update for my projects. I need to execute this script by calling

4条回答
  •  半阙折子戏
    2020-12-03 02:05

    Residuum did provide a correct answer to how you should get shell exec to find your script, but in regards to security, there are a couple of points.

    I would imagine you don't want your shell script to be in your web root, as it would be visible to anyone with web access to your server.

    I would recommend moving the shell script to outside of the webroot

        
    

    In regards to the:

    i added www-data ALL=(ALL) NOPASSWD:ALL to /etc/sudoers works

    You can modify this to only cover the specific commands in your script which require sudo. Otherwise, if none of the commands in your sh script require sudo to execute, you don't need to do this at all anyway.

    Try running the script as the apache user (use the su command to switch to the apache user) and if you are not prompted for sudo or given permission denied, etc, it'll be fine.

    ie:

    sudo su apache (or www-data)
    cd /var/www
    sh ./myscript
    

    Also... what brought me here was that I wanted to run a multi line shell script using commands that are dynamically generated. I wanted all of my commands to run in the same shell, which won't happen using multiple calls to shell_exec(). The answer to that one is to do it like Jenkins - create your dynamically generated multi line of commands, put it in a variable, save it to a file in a temp folder, execute that file (using shell_exec in() php as Jenkins is Java), then do whatever you want with the output, and delete the temp file

    ... voila

提交回复
热议问题