phpapachevirtualhostmod-fastcgimod-php

Running Apache mod_php and mod_fastcgi in seperate vhosts on one Apache server


So, I need to run multiple php apps/sites on one server. One or many should run using apache and mod_php5, and one or many should be able to run using mod_fastcgi and php-fpm. The mod_php5 ones are currently fine, and the fastcgi ones work if i disable mod_php5, but I'm having difficulty getting them to work at the same time.

I think this can be achieved using some combination of filesmatch, if.mod_fastcgi and if.mod_actions in apache, but I cant work out what it is. We can install any apache modules required to do this if needed.

Super bonus points if there is a different version of the vhost for Apache 2.2 or less and you have that snippet too, or at least know what I should do

Im not going to post the entire vhosts i have as i know they're wrong, but the first, default, mod_php based vhosts are looking like so...

#NameVirtualHost 127.0.0.1:80     < Apache 2.2 or less    
 <VirtualHost 127.0.0.1:80>
   ServerAdmin webmaster@localhost
   ServerName some.site.tld
   DocumentRoot /var/www/some/site/
   <Directory /var/www/some/site/>
     Options Indexes FollowSymLinks MultiViews
     Require all granted
#               Order deny,allow     < Apache 2.2 or less
#               Allow from all     < Apache 2.2 or less
   </Directory>
   ErrorLog /var/log/apache2/error.log
   CustomLog /var/log/apache2/access.log combined
 </VirtualHost>

Ideally, I'd like these to remain untouched, and that we can add more if we need more of these.

The outcome I would like, is to be able to drop in new vhosts configured for mod_php using something like the first configuration, and also drop in vhosts like the following for fast cgi.

# NameVirtualHost 0.0.0.0:80  < Apache 2.2 or Less
<VirtualHost 0.0.0.0:80>
   ServerAdmin webmaster@localhost
   ServerName some.application.tld
   DocumentRoot /opt/some/app/dir/
   ErrorLog /var/log/apache2/error.log
   CustomLog /var/log/apache2/access.log combined
   <IfModule mod_fastcgi.c>
     #     php_admin_flag engine off    < I tried to use this to disable PHP
     AddType application/x-httpd-fastphp5 .php
     Action application/x-httpd-fastphp5 /php5-fcgi
     Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi_someapp
     FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi_someapp -socket /var/run/php5-fpm_ptbuild.sock -pass-header Authorization
     <Directory /usr/lib/cgi-bin>
       Options Indexes FollowSymLinks MultiViews ExecCGI
       Require all granted
     </Directory>
     <Directory /opt/some/app/dir/>
       #Options Indexes FollowSymLinks MultiViews ExecCGI
       Require all granted
     </Directory>         
   </IfModule>
#    I tried to use the below alongside mod_php, didnt work
#    <IfModule mod_php5.c>
#        php_admin_flag engine off
#        AddType application/x-httpd-fastphp5 .php
#       <Directory /opt/some/app/dir/>
#               Options Indexes FollowSymLinks MultiViews ExecCGI
#               Require all granted
#       </Directory>
#    </IfModule>
</VirtualHost>

If you could post the second vhost, or tell me what I'm doing wrong, thanks very much in advance.


Solution

  • We've got a similar setup on our servers to run 2 versions of PHP, but under the same virtual host. Essentially it's for an old version of the software while transitioning to a new version - 1 runs through the original Apache config and the other through CGI. Ours is directory based, so slightly different but I think it should work for your situation still.

    First, we have the fastCGI config in a separate file /etc/apache2/mods-enabled/fastcgi.conf:

    <IfModule mod_fastcgi.c>
        #  AddHandler fastcgi-script .fcgi
        FastCgiWrapper /usr/lib/apache2/suexec
        FastCgiIpcDir /var/lib/apache2/fastcgi
        FastCgiConfig -idle-timeout 110 -killInterval 120 -pass-header HTTP_AUTHORIZATION -autoUpdate
        ScriptAlias /php-fcgi/ /var/www/cgi-bin/
    </IfModule>
    

    Then, in the virtual host config itself we have this:

    <VirtualHost *:80>
        ServerName sitename
        ...
        # Original non-CGI site
        <Directory "/sites/webroot/site1/">
            Options Indexes FollowSymLinks MultiViews
            AllowOverride all
            Order allow,deny
            Allow from all
        </Directory>
    
        # FastCGI version
        <Directory "/sites/webroot/site2/">
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
            AddHandler php-cgi .php
            Action php-cgi /php-fcgi/php562.fcgi
            <FilesMatch "\.php$">
                SetHandler php-cgi
            </FilesMatch>
        </Directory>
    </VirtualHost>
    

    I think the key thing that is required for you is the AddHandler and SetHandler directives which make it work. Then you should be able to add as many separate sites either with different directories or individual virtual hosts.