Apache+php-fpm the right way

The simplest, and probably the correct, way to configure php-fpm with apache in Centos 6/7 is by using SetHandler and ProxySet directives.

The snippet bellow show a simple Virtual Host example, just change lines 2 3 and 6 and you are good to go.


<VirtualHost *:80>
    ServerAdmin [email protected]
    Servername example.com
    Options +Indexes
    DirectoryIndex index.php                                                                                                                    
    DocumentRoot /var/www/html
    # Register php-fpm as the handler for (.*).php files.
    <FilesMatch \.php$>
         SetHandler "proxy:fcgi://localhost:9000"
    </FilesMatch>
    # Configure the proxy
    <Proxy fcgi://localhost:9000>
        ProxySet connectiontimeout=5 timeout=240
    </Proxy>

    # If the php file doesn't exist, disable the proxy handler
    # so we can gracefully fail.
    RewriteCond %{REQUEST_FILENAME} \.php$
    RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f
    RewriteRule (.*) - [H=text/html]
</VirtualHost>

 

After saving and restarting/reloading your apache server, you now have a fully functional php installation.
The advantage of this method is that it selectively send requests to your php-fpm server and you gotta keep .htaccess compatibility.

Sources:

https://httpd.apache.org/docs/2.4/handler.html
https://httpd.apache.org/docs/current/mod/mod_proxy.html