Search code examples
phpfastcgiapache2.4

fastcgi, php and execution of external commands (convert mostly) not working


I am getting crazy trying to figure out a problem that I am having since I switched to fast-cgi using apache 2.4 and php.

I used to run convert without problems with mod_php but since I switched I am not getting any errors and the script just doesn't work properly.

for example if I try to run the following command under mod_php it works, under fast-cgi it doesn't:

( ulimit -u 500 ; ulimit -m 307200 ; ulimit -v 614400 ; ulimit -f 307200 ; ulimit -t 30 ; nice -n 10 /usr/local/bin/convert -limit time 10 -limit memory 512Mb -background transparent  "/path_a/1568/15684771/uploaded_as/_web_upload_21fmupicf34eekkuer5cm7hic0.pdf_20160218_56c61b5383271_web_safe.png"  -background transparent -interlace PLANE  -resize "305x300"  -strip +repage  -depth 6 -quality 80 /path_b/tmp/cached_images/30/72/li15684771684e24cdc9648c33e9ccc880b0356c8455652d0e697171e627f3b729b90aa4a3.png 2>/dev/null )

now, this other command that touches the same paths with the same permissions, works properly:

( ulimit -u 500 ; ulimit -m 307200 ; ulimit -v 614400 ; ulimit -f 307200 ; ulimit -t 30 ; nice -n 10 /usr/local/bin/convert -limit time 10 -limit memory 512Mb -quiet -background transparent  "/path_a/1568/15684767/uploaded_as/CC.PNG_20160218_56c61d465e8a3_web_safe.png"  -background transparent -interlace PLANE  -resize "311x300"  -strip +repage  -depth 6 -quality 80 /path_b/tmp/cached_images/11/24/li156847675bcf6aa92d8ce5e08b8a0d995fbd59ff590ab3d7cd543a2c1392c803547904f6.png 2>/dev/null )

I was thinking that it had to do with the ulimit and so I have created a simple sh script and tried to execute the same commands and they both work.

the difference between the apache configs when I test them are the following:

#FAST CGI
LoadModule fastcgi_module     libexec/apache24/mod_fastcgi.so
LoadModule mpm_event_module libexec/apache24/mod_mpm_event.so

#MOD_PHP
LoadModule mpm_prefork_module libexec/apache24/mod_mpm_prefork.so
LoadModule php5_module        libexec/apache24/libphp5.so

the I have a specific apache-php file that I do not change:

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
<IfModule fastcgi_module>
    #fast-cgi
    FastCgiConfig -maxClassProcesses 100 -maxProcesses 100 -idle-timeout 200
    ScriptAlias /cgi-bin/ "/my_path_to/cgi-bin/"
    AddHandler php5-fastcgi .php .html
    Action php5-fastcgi /cgi-bin/php.cgi
    <Location "/cgi-bin/php.cgi">
       Order Deny,Allow
       Deny from All
       Allow from env=REDIRECT_STATUS
       Options ExecCGI
       SetHandler fastcgi-script
    </Location>
</IfModule>

<IfModule php5_module>
    #mod-php
    AddHandler php5-script .php .html
</IfModule>

and finally this is my php-cgi file:

#!/bin/sh
# Shell Script To Run PHP5 using mod_fastcgi under Apache 2.x
# Tested under FreeBSD 6.x and 7.x
### Set PATH ###
PHP_CGI=/usr/local/bin/php-cgi
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=5000

### no editing below ###
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
#export USE_ZEND_ALLOC=0
exec $PHP_CGI

the mpm directives for apache are:

<IfModule mpm_prefork_module>
    MaxMemFree           10000
    StartServers           100
    MinSpareServers         10
    MaxSpareServers         50
    GracefulShutDownTimeout  2
    MaxRequestsPerChild   1000
    MaxClients            1000
    MaxRequestWorkers     1000
</IfModule>

<IfModule mpm_event_module>
    MaxMemFree           10000
    StartServers            10
    MinSpareThreads         75
    MaxSpareThreads        250
    ThreadsPerChild         25
    MaxRequestWorkers     1000
    MaxConnectionsPerChild   0
    GracefulShutDownTimeout  2
    MaxClients            1000
</IfModule>

I even start having problems running this:

ulimit -u 500 ; ulimit -m 307200 ; ulimit -v 614400 ; ulimit -f 307200 ; ulimit -t 30 ; nice -n 10 /usr/local/bin/convert -list format | grep -E ' +r[w+-]{2} +' | awk '{print $1}' | sed 's/\*//g

but if I run one at the time it runs without problems

ulimit -u 500 ; ulimit -m 307200 ; ulimit -v 614400 ; ulimit -f 307200 ; ulimit -t 30 ; nice -n 10 /usr/local/bin/convert -list format >file.a
ulimit -u 500 ; ulimit -m 307200 ; ulimit -v 614400 ; ulimit -f 307200 ; ulimit -t 30 ; nice -n 10 grep -E ' +r[w+-]{2} +' file.a > file.b
ulimit -u 500 ; ulimit -m 307200 ; ulimit -v 614400 ; ulimit -f 307200 ; ulimit -t 30 ; nice -n 10 awk '{print $1}' file.b
/*
* here I just simply grab the content and do a str_replace
* it is not permissions on sed as I did it with a 4th line and
*   sed but it was a bit of an overkill)
*/

I have tried to execute the commands with:

proc($cmd, 'r');
`$cmd`;
exec($cmd);
etc...

they all work with proc under mod_php

php.ini has the memorylimit set to 256M in case that might matter. I have tried to increase it to 512M and even 1G just to test if that was the problem, and it didn't change. it is also not a timeout issue, as it takes less than 1 second to execute via command line and via the php script


Solution

  • I figured it out !!! the path for execution was different so I assume that some library could not be called and included by convert or other used programs.

    I changed my php-cgi file to include the following:

    PATH=$PATH:/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin
    export PATH
    

    my current php-cgi file is as follow:

    #!/bin/sh
    # Shell Script To Run PHP5 using mod_fastcgi under Apache 2.x
    # Tested under FreeBSD 6.x and 7.x
    ### Set PATH ###
    PHP_CGI=/usr/local/bin/php-cgi
    
    ### no editing below ###
    PATH=$PATH:/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin
    export PATH
    export PHP_FCGI_CHILDREN=0
    export PHP_FCGI_MAX_REQUESTS=10000
    #export USE_ZEND_ALLOC=0
    exec $PHP_CGI
    

    and now everything works.

    you probably have to adjust your paths to your OS