Community Forums
Connect with us on LinkedIn
+ Reply to Thread
Results 1 to 11 of 11
  1. #1
    Member
    Join Date
    Mar 2003
    Posts
    222
    cPanel/Enkompass Access Level

    Root Administrator

    Default Simple PHP Script crashing server

    Hi,

    I have simple PHP script that crash the server. The script is


    PHP Code:
    <?php

    $cmd 
    "php -v";
    exec("$cmd 2>&1"$output);

    print_r($output);
    This works fine on my server with PHP 4.4.4

    But on server with php 5, it just get into some loop (too many process in short time) and crash the server. This is part of a script so many clients use the script and crash my server so i checked the script and found these lines create the problem. If i use full path to php, it will be fine.


    My php version is PHP 5.2.5 running in suPHP mode.

    Anyone know why this happening ?

    Thanks,

    Yujin

  2. #2
    Member brianoz's Avatar
    Join Date
    Mar 2004
    Location
    Melbourne, Australia
    Posts
    1,117
    cPanel/Enkompass Access Level

    Root Administrator

    Default

    Try this instead, at least you're more likely to see the error:

    PHP Code:
    <?php

    $cmd 
    "php -v";
    passthru("$cmd 2>&1");

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    4

    Default

    I also have a similar problem. One user uploaded a simple script that only has:

    echo exec('pear install PhpDocumentor');

    If you open the page in the browser, the system starts spawning a lot of php processes quickly and the server runs out of memory soon enough.

    I was able to reproduce the same with just:

    echo exec('pear');

    I'm a bit worried about how easy is for a user to crash the server so I am hoping someone can share some hints about how to avoid this.

    The system has the latest httpd 2.2.8 and php 5.2.5 with suPHP build with the latest easyapache from Release. Is suhosin the solution ? blacklisting the exec function ?

  4. #4
    Member
    Join Date
    Mar 2006
    Posts
    1,215

    Default

    exec is one of many functions that should be disabled in php.ini disable_functions

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    4

    Default

    Quote Originally Posted by jayh38 View Post
    exec is one of many functions that should be disabled in php.ini disable_functions
    But if the user can upload his own php.ini that's not good enough, right ?
    Thanks.

  6. #6
    Member
    Join Date
    Mar 2003
    Posts
    222
    cPanel/Enkompass Access Level

    Root Administrator

    Default

    Disabling exec is not a solution. Many scripts need exec to work. So i don't think every one can disable exec function on their servers. It seems a bug, same code work with out any problem on php 4 (running phpsuexec).

  7. #7
    cPanel Development cpanelkenneth's Avatar
    Join Date
    Apr 2006
    Posts
    3,788
    cPanel/Enkompass Access Level

    Root Administrator

    Default

    This is the expected behavior of PHP CGI, as shown by this report:

    http://bugs.php.net/bug.php?id=30463

  8. #8
    Member
    Join Date
    Mar 2003
    Posts
    222
    cPanel/Enkompass Access Level

    Root Administrator

    Default

    Thanks for the reply. That bug is dated 17 Oct 2004, so... any client who knows the one line code can crash the server ? No way to prevent this ?

    The bug report says Apache limit execution and show Internal Server Error. Is there anyway we can also do the same, that is stop the runaway process to kill the server ?

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    4

    Default

    OK, so I edited the pear script to make sure PHP=/usr/bin/php-cli then no more crash.

    But it might be rewritten by software updates, so.. the more elegant fix would be to ensure that the envvar PHP_PEAR_PHP_BIN gets set properly as that determines which php binary the pear command will call.

    Anyone know where to set it ? Also, in case someone calls php like in brianoz's example, isn't it safer to make sure php will always point to the cli ? No more php -> php-cgi symlinks please.
    Last edited by robalo; 02-15-2008 at 12:19 PM.

  10. #10
    Member brianoz's Avatar
    Join Date
    Mar 2004
    Location
    Melbourne, Australia
    Posts
    1,117
    cPanel/Enkompass Access Level

    Root Administrator

    Default

    I guess the fix would be to make sure that the PATH when running PHP as CGI includes a directory with the CLI version ahead of the directory containing the CGI version.

    In other words, if the CGI version comes from /usr/bin/php and the CLI version from /usr/local/bin, make sure PATH is set something like:
    Code:
    PATH=/usr/local/bin:/usr/bin:...
    Another option that might work well is to make sure that the CLI version is the only one available in the PATH, that is, have the CGI binary come from /usr/local/php5/bin/php and theb make sure that /usr/bin/php is CLI. As far as I remember that's how it used to be done but easyapache3 seems to have discarded that wisdom!

  11. #11
    Member
    Join Date
    Nov 2005
    Posts
    64

    Default

    In order to protect the server from crashing, setting the following option might help (it will only make sense on phpsuexec/suphp configurations, i.e., when php scripts are running under a specific user):

    Just add
    RLimitNPROC 20
    into your httpd.conf

    Do not forget about
    /usr/local/cpanel/bin/apache_conf_distiller --update

    I use that on all our servers.

    More reading:
    http://httpd.apache.org/docs/2.2/mod...ml#rlimitnproc
    Dmitry Postrigan

Similar Threads & Tags
Similar threads

  1. Newbie, Need help with simple script =)
    By Orsenfelt in forum Database Discussions
    Replies: 2
    Last Post: 08-11-2008, 02:04 PM
  2. php running as root crashing server
    By deieno in forum cPanel and WHM Discussions
    Replies: 5
    Last Post: 08-09-2007, 12:47 PM
  3. mail list script sending out spam and crashing server big style
    By draggle in forum cPanel and WHM Discussions
    Replies: 1
    Last Post: 10-20-2006, 02:21 PM
  4. cron jobs - how to run a simple php script
    By adentone in forum cPanel and WHM Discussions
    Replies: 7
    Last Post: 09-19-2005, 06:26 AM
  5. need a simple linux script
    By webbhost in forum cPanel and WHM Discussions
    Replies: 1
    Last Post: 03-17-2004, 04:50 AM
Linkedin       Facebook       Twitter       RSS       Flickr       YouTube