How can I tell Apache2, run mod_php5 by default but run this VH in CGI mode?

好久不见. 提交于 2019-12-19 20:46:12

问题


The Server

I have a development server that I'm using to host my current projects. Here are some stats:

root@myserver:/usr/bin $ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.10
DISTRIB_CODENAME=maverick
DISTRIB_DESCRIPTION="Ubuntu 10.10"
root@myserver:/usr/bin $ apache2 -v
Server version: Apache/2.2.16 (Ubuntu)
Server built:   Nov 18 2010 21:17:43
root@myserver:/usr/bin $ php --version
PHP 5.3.3-1ubuntu9.1 with Suhosin-Patch (cli) (built: Oct 15 2010 14:00:18)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
root@myserver:/usr/bin $ uname -r
2.6.35-22-server

The Problem

I'm running PHP 5.3.3 using mod_php5 and it's working great. But I need to run PHP 5.2.11 for just one VH on the server, so I used phpfarm to compile PHP 5.2.11. I want to configure Apache to use mod_php5 for everything on the server except for this VH. I'll run PHP 5.2.11 for this one VH over FastCGI.

My Test

As a test, I'm using Apache's default site at /var/www. I set up the following directory files to report PHP versions to me:

  • /var/www/phpinfo.php
  • /var/www/php-5.2.11/phpinfo.php

My objective is to have /var/www/phpinfo.php show me version 5.3.3 (mod_php5) and have /var/www/php-5.2.11/phpinfo.php show me 5.2.11 (CGI). This isn't working yet.

I inserted the following code /etc/apache2/httpd.conf:

FastCgiServer /var/www/cgi-bin/php-cgi-5.2.11
ScriptAlias /cgi-bin-php/ /var/www/cgi-bin/

I inserted the following code into the default site's VH definition:

<Directory /var/www/php-5.2.11/>
    AddHandler php-cgi .php
    Action php-cgi /cgi-bin-php/php-cgi-5.2.11
</Directory>

Results

With mod_php5 enabled:

  • /var/www/phpinfo.php ---> 5.3.3 (mod_php5)
  • /var/www/php-5.2.11/phpinfo.php ---> 5.3.3 (mod_php5)

With mod_php5 disabled:

  • /var/www/phpinfo.php ---> no handler; Firefox tries to download the PHP file
  • /var/www/php-5.2.11/phpinfo.php ---> 5.2.11 (CGI)

The Files Involved

http://files.mattalexander.me/apacheconfig.tgz


回答1:


A good alternative is suPHP.

If set up properly, you can have as many handlers, as you wish; even for different directories within a virtual host.

For added security, there's suPHP's "paranoid" mode in which you assign a Unix user and group to a virtual host and the scripts will be run as that user.

My suPHP config looks like that:

...

[handlers]
;Handler for php-scripts
x-httpd-php=php:/usr/bin/php4-cgi
x-httpd-php5=php:/usr/bin/php5-cgi

;Handler for CGI-scripts
x-suphp-cgi=execute:!self

...

Within a simple .htaccess file it is possible to have scripts run using different versions of PHP:

<FilesMatch \.php$>
   SetHandler x-httpd-php
</FilesMatch>

<FilesMatch \.php5$>
   SetHandler x-httpd-php5
</FilesMatch>

# etc...

Hope that helps.




回答2:


The problem is that mod_php still handles .php files, even when the CGI handler is set for a vhost.

To fix that, the <Directory> in the vhost needs to have a SetHandler directive:

  Action php-cgi /cgi-bin-php/php-cgi-5.2.11
  <FilesMatch \.php$>
    SetHandler php-cgi
  </FilesMatch>
</Directory>

I've updated the tutorial.



来源:https://stackoverflow.com/questions/4453078/how-can-i-tell-apache2-run-mod-php5-by-default-but-run-this-vh-in-cgi-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!