SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Problem: Shell script execution using PHP via browser

    When i call a shell script via a php script all the commands inside the shell script doesn't execute correctly (in my case the chmod, cp commands doesn't work properly) this happens when call to the php script is made via web browser, whereas when the same php script is called through command prompt all the shell script commands executes perfectly


    PHP script Code below.


    <?php

    $v1="mycompany";

    $v2="developer@mycompany.com";

    $output = exec ('bash auto_install.sh '.escapeshellarg($v1).' '.escapeshellarg($v2));

    echo $output;

    ?>



    Shell script code below.


    #!/bin/bash

    cp -R /Users/developer/Sites/master /Users/developer/Sites/$1

    chmod -R 777 /Users/developer/Sites/$1

    php /Users/developer/Sites/$1/install.php mysql localhost $1 root pulkit $1 admin $2 localhost/~developer/$1 create 1

    Thanks in advance

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Well you see, running the script from the command line will run the script as the user. But running the same script from Apache (I assume) will run the file as the Apache's user account. Two different permissions is, from what I gather, your problem.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    we use apache server, could you plz tell me how to find the apache's user account. How do we solve this issue?

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rams22 View Post
    we use apache server, could you plz tell me how to find the apache's user account. How do we solve this issue?
    posix_getlogin

    You can either: Change the file permissions for the files that your script must interact with, or use sudo to change user from within your script. Both are a security risk, unless you trust every process running under Apache (Eg. every single virtual host on the web server), but the latter is more severe.

  5. #5
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou for your reply, Sudo would require me to input the password, how can i do that in the shell script?

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rams22 View Post
    Thankyou for your reply, Sudo would require me to input the password, how can i do that in the shell script?
    Put the user in sudoers. Can't you change the file permissions instead? sudo basically gives the web server full access to the machine, making it very prone to hacking attempts.

    An alternative (depending on what you're trying to accomplish), could be to store the task in the database and then let an offline script (cronjob) process it. That way, the webserver won't have to change privileges.

  7. #7
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi I am having same proble. i.e. I am able to run shell script through PHP program when i run PHP program through command promt but when I run PHP program through web browser i am not able to run shell script.
    Thanks in advance



    Shripad Bharde


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •