Error 500, apache + php + fcgi + php_gd

luis_chiang used Ask the Experts™
Hi all

I got a problem, when I tried to run a php script. I have configured apache + php using fcgi

but When I try to run a php code that call some php_gd functions it fails

un Jan 31 12:45:23 2010] [warn] (104)Connection reset by peer: mod_fcgid: read data from fastcgi server error.
[Sun Jan 31 12:45:23 2010] [error] [client] Premature end of script headers: image.php

I was searching a lot about configuring, suexec, fcgi, etc etc.. but I dont have any answer.

here is my apache config:

DocumentRoot /home/amatamoros/public_html
ErrorLog /var/log/virtualmin/
CustomLog /var/log/virtualmin/ combined
ScriptAlias /cgi-bin/ /home/amatamoros/cgi-bin/
ScriptAlias /awstats /home/amatamoros/cgi-bin
DirectoryIndex index.html index.htm index.php index.php4 index.php5
<Directory /home/amatamoros/public_html>
Options -Indexes IncludesNOEXEC FollowSymLinks ExecCGI
allow from all
AllowOverride All
AddHandler fcgid-script .php
AddHandler fcgid-script .php5
FCGIWrapper /home/amatamoros/fcgi-bin/php5.fcgi .php
FCGIWrapper /home/amatamoros/fcgi-bin/php5.fcgi .php5
<Directory /home/amatamoros/cgi-bin>
allow from all
RewriteEngine on
RewriteCond %{HTTP_HOST}
RewriteRule ^(.*) [R]
RewriteCond %{HTTP_HOST}
RewriteRule ^(.*) [R]
RemoveHandler .php
RemoveHandler .php5
Alias /dav /home/amatamoros/public_html
Alias /pipermail /var/lib/mailman/archives/public
<Location /dav>
AuthType Basic
AuthUserFile /home/amatamoros/etc/dav.digest.passwd
Require valid-user
ForceType text/plain
Satisfy All
RemoveHandler .php
RemoveHandler .php5
AuthName " statistics"
AuthType Basic
AuthUserFile /home/amatamoros/.awstats-htpasswd
require valid-user
RedirectMatch /cgi-bin/mailman/([^/\.]*)(.cgi)?(.*)$1.cgi$3
RedirectMatch /mailman/([^/\.]*)(.cgi)?(.*)$1.cgi$3
SuexecUserGroup "#505" "#506"

and my fcgi configuration:

export PHPRC
umask 022
exec /usr/bin/php-cgi

Im using centos.

please help me!!
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Richard QuadlingSenior Software Developer

Let's run through the usual suspects...

1 - Can you create info.php containing

<?php phpinfo(); ?>

and show the results.

Specifically, I'm looking for the "Configure Command", "Configuration File (php.ini) Path", "Loaded Configuration File", "Scan this dir for additional .ini files" and "additional .ini files parsed"

as well as ...

GD Support      enabled
GD Version      bundled (2.0.34 compatible)

2 - Assuming that is all OK. Create a prove_gd.php file with ...

<pre><?php print_r(gd_info()); ?></pre>

That should produce something similar to the output as part of phpinfo().

3 - Let's see the code you are using?

4 - In the code, remove any headers() you use to send the image to the client. Can you run the URL to the php script directly? What errors do you get?


Thanks for your reply,

I'm guessing that the problem is like configuration, because sometimes(1 of 300 tries) runs and another not.

The same code is running in another server fine, but in my server have problems.

here the output of <pre><?php print_r(gd_info()); ?></pre>

Array ( [GD Version] => bundled (2.0.28 compatible) [FreeType Support] => 1 [FreeType Linkage] => with freetype [T1Lib Support] => [GIF Read Support] => 1 [GIF Create Support] => 1 [JPG Support] => 1 [PNG Support] => 1 [WBMP Support] => 1 [XPM Support] => [XBM Support] => 1 [JIS-mapped Japanese Font Support] => )

the result of phpinfo();

Configure Command
      './configure' '--build=i686-redhat-linux-gnu' '--host=i686-redhat-linux-gnu' '--target=i386-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--cache-file=../config.cache' '--with-libdir=lib' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php.d' '--disable-debug' '--with-pic' '--disable-rpath' '--without-pear' '--with-bz2' '--with-curl' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--enable-gd-native-ttf' '--without-gdbm' '--with-gettext' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-png' '--with-pspell' '--with-expat-dir=/usr' '--with-pcre-regex=/usr' '--with-zlib' '--with-layout=GNU' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-sysvmsg' '--enable-track-vars' '--enable-trans-sid' '--enable-yp' '--enable-wddx' '--with-kerberos' '--enable-ucd-snmp-hack' '--with-unixODBC=shared,/usr' '--enable-memory-limit' '--enable-shmop' '--enable-calendar' '--enable-dbx' '--enable-dio' '--with-mime-magic=/usr/share/file/magic.mime' '--without-sqlite' '--with-libxml-dir=/usr' '--with-xml' '--with-system-tzdata' '--enable-force-cgi-redirect' '--enable-pcntl' '--with-imap=shared' '--with-imap-ssl' '--enable-mbstring=shared' '--enable-mbstr-enc-trans' '--enable-mbregex' '--with-ncurses=shared' '--with-gd=shared' '--enable-bcmath=shared' '--enable-dba=shared' '--with-db4=/usr' '--with-xmlrpc=shared' '--with-ldap=shared' '--with-ldap-sasl' '--with-mysql=shared,/usr' '--with-mysqli=shared,/usr/lib/mysql/mysql_config' '--enable-dom=shared' '--with-dom-xslt=/usr' '--with-dom-exslt=/usr' '--with-pgsql=shared' '--with-snmp=shared,/usr' '--enable-soap=shared' '--with-xsl=shared,/usr' '--enable-xmlreader=shared' '--enable-xmlwriter=shared' '--enable-fastcgi' '--enable-pdo=shared' '--with-pdo-odbc=shared,unixODBC,/usr' '--with-pdo-mysql=shared,/usr/lib/mysql/mysql_config' '--with-pdo-pgsql=shared,/usr' '--with-pdo-sqlite=shared,/usr' '--enable-dbase=shared'

Configuration File (php.ini) Path       /home/amatamoros/etc/php5/php.ini

Scan this dir for additional .ini files       
additional .ini files parsed       /etc/php.d/Fileinfo.ini, /etc/php.d/bcmath.ini, /etc/php.d/dba.ini, /etc/php.d/dbase.ini, /etc/php.d/dom.ini, /etc/php.d/gd.ini, /etc/php.d/imap.ini, /etc/php.d/ldap.ini, /etc/php.d/mbstring.ini, /etc/php.d/mcrypt.ini, /etc/php.d/memcache.ini, /etc/php.d/mhash.ini, /etc/php.d/mssql.ini, /etc/php.d/mysql.ini, /etc/php.d/mysqli.ini, /etc/php.d/ncurses.ini, /etc/php.d/odbc.ini, /etc/php.d/pdo.ini, /etc/php.d/pdo_mysql.ini, /etc/php.d/pdo_odbc.ini, /etc/php.d/pdo_pgsql.ini, /etc/php.d/pdo_sqlite.ini, /etc/php.d/pgsql.ini, /etc/php.d/readline.ini, /etc/php.d/snmp.ini, /etc/php.d/soap.ini, /etc/php.d/tidy.ini, /etc/php.d/xmlreader.ini, /etc/php.d/xmlrpc.ini, /etc/php.d/xmlwriter.ini, /etc/php.d/xsl.ini

GD Support       enabled
GD Version       bundled (2.0.28 compatible)

my code is attached.

and my php.ini too

please give me a recommendation
Richard QuadlingSenior Software Developer


1 - The header you are sending is image/jpeg, but you are generating a PNG file (imagepng()).

Try changing...

header("Content-type: image/jpeg");


header("Content-type: image/png");

2 - You are not sending any cache avoidance headers, so maybe you are getting a cached file each time.

Try adding ...

header("Cache-Control: no-cache, must-revalidate, , post-check=0, pre-check=0");
header("Expires: 0");

just after the content-type header.
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.



I try your recommendation, but It doesn't works... I was trying before with the png or jpeg content-tye and doesn't works too now, something rare is that the code runs well on another server.

sometimes I comment some lines like :

and just open the file and flush it out to the stream, It's works fine. I don't know if it is any memory limit problem.

Richard QuadlingSenior Software Developer

When it doesn't work, do you get an error message? Can you see your errors log? Are you logging errors?

There must be something...

error_reporting(-1); // Turn on ALL errors/warnings/notices.
ini_set('display_errors', 0); // Don't show them to the client.
ini_set('log_errors', 1); // Log errors.
ini_set('error_log', './errors.log'); // Log errors to this file.
set_time_limit(0); // Allow the program to run forever.


Yes, I look at my logs files, and only can see these message,

[Tue Feb 02 13:56:21 2010] [warn] (104)Connection reset by peer: mod_fcgid: read data from fastcgi server error.
[Tue Feb 02 13:56:21 2010] [error] [client] Premature end of script headers: americana.php

and I added your lines, but nothing, still sending the same Error 500, and the same log.  and the file errors.log is empty



I guess I found more information about the error, I change FCGI to CGI.. and here is the error log...  I'm running my server virtualized by hyper-v... but here is the output of the log, no integration tools installed

and still getting the error 500, but now its send me a file with extension .php and its contains the html of the error 500.

[Tue Feb 02 14:44:48 2010] [error] [client] *** glibc detected *** /usr/bin/php-cgi: malloc(): memory corruption: 0x094b7828 ***

do you have any recommendation?
Senior Software Developer
Right. So it is not your code. It is your PHP installation.

As this isn't windows, I'm out. Sorry.

If you are able to build from the source, then update and rebuild.

If not, then see if you can update any related packages to their most recent versions.


After I update to php 5.2 everything is well

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial