Running PHP script through CGI with Apache… how?

Posted on

Running PHP script through CGI with Apache… how? – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about apache-2.2, php, , , .

Our current setup has PHP scripts being served by Apache using mod_php. We have just developed a new script that needs to be run through CGI (it’s using pcntl to fork off some processes) instead of mod_php. Can someone give me an example configuration for this? Ideally we’d be able to have all the scripts run through mod_php except this one, but if we have to run them all through, then I can live with that.

Server details: Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4 with Suhosin-Patch

The current configuration is about as straight-forward as they come (/etc/apache2/sites-available/default):

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
    </Directory>
    <Directory /var/www/>
        Options FollowSymLinks MultiViews
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log
    LogLevel warn
    CustomLog /var/log/apache2/access.log combined
</VirtualHost>

Thanks, in advance, for the help.

Solution :

Don’t use CGI: it works much slower than mod_php. The script that needs to fork can be launched using shell commands (system() function) and fork to it’s heart’s content 🙂

If you still want CGI, here’s a hint how to execute SOME scripts with CGI handler with .htaccess file: Running php4 and php5 along side each other.

CGI is a COMMON GATEWAY INTERFACE … i.e. you don’t need to set it up for particular languages. However, Apache normally determines what to do with a file based on the filename extension (e.g. file.cgi versus file.php)

Your Apache configuratin is most likely already setup to handle CGI programs, you probably only need to do two things

Allow CGIs to be run from whatever directory you’re in

<Directory /path/to/dir>
    Options +ExecCGI
</Directory>

Setup CGI for the file extension you want (e.g. blah.phpcgi)

AddHandler cgi-script .phpcgi

Leave a Reply

Your email address will not be published. Required fields are marked *