Setting up Apache 2.2 + FastCGI + SuExec + PHP-FPM on Centos 6

I'm trying to follow this very detailed instruction here, I simply changed from www-data user to apache user, and is using /var/www/hosts/sitename/public_html instead of /home/user/public_html

However, I spent the whole day trying to figure out why the php file content is displayed without being parsed correctly. I just cant's seem to figure this out. Below is my current config:

/etc/httpd/conf.d/fastcgi.conf

User apache
Group apache

LoadModule fastcgi_module modules/mod_fastcgi.so

# dir for IPC socket files
FastCgiIpcDir /var/run/mod_fastcgi

# wrap all fastcgi script calls in suexec
FastCgiWrapper On

# global FastCgiConfig can be overridden by FastCgiServer options in vhost config
FastCgiConfig -idle-timeout 20 -maxClassProcesses 1

# sample PHP config
# see /usr/share/doc/mod_fastcgi-2.4.6 for php-wrapper script
# don't forget to disable mod_php in /etc/httpd/conf.d/php.conf!
#
# to enable privilege separation, add a "SuexecUserGroup" directive
# and chown the php-wrapper script and parent directory accordingly
# see also http://www.brandonturner.net/blog/2009/07/fastcgi_with_php_opcode_cache/
#
FastCgiServer /var/www/www-data/php5-fcgi
#AddType application/x-httpd-php .php

AddHandler php-fcgi .php
Action php-fcgi /fcgi-bin/php5-fcgi

Alias /fcgi-bin/ /var/www/www-data/

#FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /tmp/php5-fpm.sock -pass-header Authorization
#DirectoryIndex index.php
#
<Location /fcgi-bin/>
#    Order Deny,Allow
#    Deny from All
#    Allow from env=REDIRECT_STATUS
    SetHandler fcgid-script
    Options +ExecCGI
</Location>

/etc/httpd/conf.d/vhost.conf

<VirtualHost>
                DirectoryIndex index.php index.html index.shtml index.cgi

                SuexecUserGroup www.mysite.com mygroup

                Alias /fcgi-bin/ /var/www/www-data/www.mysite.com/
                DocumentRoot /var/www/hosts/mysite.com/w/w/w/www/

                <Directory /var/www/hosts/mysite.com/w/w/w/www/>
                        Options -Indexes FollowSymLinks                        
                        AllowOverride None
                        Order allow,deny
                        allow from all
                </Directory>
</VirtualHost>

PS: 1. Also, with PHP5.5, do I even need FPM or is it already included? 2. I'm using mod_fastcgi, not sure if this is the problem and it I should switch to mod_fcgid? There seems to be conflicting records on the internet considering which one is better. I have many virtual hosts running on the machine and hope to be able to provide each user with their own opcache


Solution 1:

First of all, you're overkilling it.

You don't need suphp + php-fpm since they basically do the same thing.

If you want multi-user multi-vhost enviroment you should be using the following stack:

apache+mod_fastcgi+php-fpm

php-fpm allows you to define pools that can run under different user with totally different php settings.

For that configuration you would need to revert apache to its basic configuration:

user apache
group apache

I'll assume that you will be running your vhosts in your users' homedirs public_html folder

You will need to chmod /home/%user% to 755 and change group for public_html to apache

chown %user%:apache /home/%user%/public_html
chomod 755 /home/%user%

Then, you will define the php-fpm pool for the user

[%user%]

listen = /var/run/php-fpm/%user%.socket
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = web
listen.group = www
listen.mode = 0666

user = %user%
group = %userg%

#pm.status_path = /fpm-status
pm = ondemand
pm.max_children = 20
#pm.start_servers = 6
#pm.min_spare_servers = 5
#pm.max_spare_servers = 10
pm.max_requests = 500
request_terminate_timeout = 600s
rlimit_files = 131072
rlimit_core = unlimited
catch_workers_output = yes



php_admin_value[error_log] = /home/%user%/fpm-error.log
php_flag[display_errors] = on
php_flag[display_startup_errors] = on
php_admin_flag[log_errors] = on
php_admin_value[memory_limit] = 128M
php_value[session.save_handler] = files
php_value[session.save_path] = /tmp

note that this is a example file, I recommend modifying it before using it on production machine

After you install and configure mod_fastcgi, you will need to add a special php handler for each user, like this

nano /etc/httpd/conf.d/php-fpm.conf

Action fcgi-php-fpm /fcgi-php-fpm virtual
Alias /fcgi-php-fpm /fcgi-php-fpm
FastCgiExternalServer /fcgi-php-fpm -socket /var/run/php-fpm/web.socket -pass-header Authorization -idle-timeout 180

And in your vhost file

<VirtualHost *:80>

 ServerName beforeafter.local
 ServerAlias www.beforeafter.local
 DocumentRoot /home/web/public_html/before
 DirectoryIndex index.htm index.html index.php
 ErrorLog  /home/web/ba.error.log
 CustomLog /home/web/ba.access.log combined
 UseCanonicalName Off

 Options FollowSymlinks +Indexes
 RewriteEngine On

 AddType text/html .php
 AddHandler fcgi-php-fpm .php

 <Directory />
  AllowOverride All
 </Directory>
</VirtualHost>

Your action name and handler name must match in order to work.

After that, each user runs php under its own name.

I recommend to disable all unnecessary apache modules in order to speed it up and turn off SELinux if your not proficent in configuring it.