I'm trying to run an Apache server that executes some scripts over fast CGI, but I can't figure out for the life of me how to actually get it to work. My problem in particular is the "Error 500: End of script output before headers".

I've been reading a lot about this, following every suggestion I can find but still no luck. There were some about IOTimeout and BusyTimeout and every other kind of Fcgid timeout available, but those did nothing and I don't think my problem is related to it.

Starting from a fresh linux installation this is what I've done:

  1. Installed apache and mod_fcgid.
  2. Created a new empty web at /var/www/test with some empty files and a script. I've set the permissions accordingly and just to be sure changed the owner and group to http.

    $ ls -l /var/www
    drwxrwxr-x 1 http http 122 Nov  8 16:08 test/
    $ ls -l /var/www/test
    -rw-rw-r-- 1 http http   0 Nov  8 15:29 file01.txt
    -rw-rw-r-- 1 http http   0 Nov  8 15:29 file02.txt
    -rw-rw-r-- 1 http http   0 Nov  8 15:29 file03.txt
    -rwxr-xr-x 1 http http 107 Nov  8 16:08 run.fcgi*
    $ cat /var/www/test/run.fcgi 
    use strict;
    use warnings;
    print "Content-type: text/html\n\n";
    print "Hello world.\n";
  3. I've appended this to /etc/httpd/conf/httpd.conf:

    LoadModule fcgid_module modules/mod_fcgid.so
    <IfModule fcgid_module>
      AddHandler fcgid-script .fcgi
    <VirtualHost *:80>
      DocumentRoot /var/www/test
      <Directory /var/www/test>
        Options +Indexes +ExecCGI
        Require all granted

Now, I can run the script perfectly fine from the terminal. When I then head over to http://localhost I get a list of the directory files as expected, but when I then open the script I get the error 500 mentioned above. The apache's error log shows this (snipped unnecessary parts):

Content-type: text/html

Hello world.
(...) Connection reset by peer (...) mod_fcgid: error reading data from FastCGI server, referer: (...)                                                                                  
(...) End of script output before headers: run.fcgi, referer: (...)

Why is it that the script's output got logged into the error log? I think this is related to permissions and ownership, but I can't figure out how, I think I've set all of them accordingly. Do you have any idea what I can do to get this to run?

I'm trying to run all this in a 64-bit Arch linux box.

Thank you all!


Your Apache configuration is one of many different ways to setup FastCGI. This specific config may not work on all distros and flavors of Apache configurations. This config is working for you.

Because the 500 error indicates that apache thought your script was executing, and since your code is in the log we know apache found the file. However, a FastCGI server needs an extra layer of communication with apache as opposed to a CGI script. Your FastCGI code must wait for requests from apache. In perl, the CGI::Fast module handles that extra layer.

  1. Use the original Apache configuration in your question.
  2. Make sure you have the perl module: CGI::Fast
  3. Modify your FastCGI server script like this:

    use strict;
    use warnings;
    use CGI::Fast;
    while (my $q = CGI::Fast->new) {
        print "Content-type: text/html\n\n";
        print "Hello world.\n";

This code constitutes a FastCGI server. The CGI::Fast->new waits for and receives the requests and the environment from apache and sets up your perl runtime environment to easily function as a FastCGI server. The environment contains important information that CGI code needs.


  1. https://httpd.apache.org/mod_fcgid/
  2. perldoc CGI::Fast
  3. perldoc CGI
  • Awesome, that did it! I can't believe I looked everywhere but the apache reference page... shame on me. Thank you a lot though! – elpato Nov 9 '15 at 11:13
  • Great! No shame. Live and learn, learning is more important than making mistakes. There's no failure only feedback. As I implied, you didn't tell and I didn't ask, the specifics of OS, and software versions, etc. So I looked at some of the wrong issues. Nobody knows everything. And I think we've come up with a great answer here! To clean up this answer I'm going to delete most of my comments on this answer, because they are irrelevant and just extra noise at this point. I ask you to do the same, delete irrelevant comments on this answer. Thanks! Votes on answers help too! – RobertL Nov 9 '15 at 21:56

You propably will need a FCGI wrapper, executing the whole stuff via php-cgi command. Specify this in your vhost:

AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch AddHandler fcgid-script .php AddHandler fcgid-script .php5 FCGIWrapper /home/.../fcgi-bin/php5.fcgi .php FCGIWrapper /home/.../fcgi-bin/php5.fcgi .php5

Then prepare the script /home/.../fcgi-bin/php5.fcgi

#!/bin/bash PHPRC=$PWD/../etc/php5 export PHPRC umask 022 export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=99999 export PHP_FCGI_MAX_REQUESTS SCRIPT_FILENAME=$PATH_TRANSLATED export SCRIPT_FILENAME exec /usr/bin/php5-cgi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.