Configuring Apache with FastCGI PHP-FPM for a Slim Application

This article describes how to configure Apache to run a Slim Framework application using PHP-FPM, as a FastCGI application.

I got legacy sitemap running, and wanted to avoid reloading the application with each request. PHP 7 and the Zend OpCache helped, a lot. I’m not sure if FastCGI helped, but, in theory, it’s more efficient. Benchmarks are at the bottom.

After figuring out my configuration, I found out that Symfony had a good article that would have saved me time.

The Magic Configuration Line

In the configuration for my virtual host, I have this one line that enables the magic:

RewriteRule ^/legacy-sitemap/api(.*)$ "unix:/run/php/php7.2-fpm.sock|fcgi://localhost:9000/www/riceball.com/public/legacy-sitemap/api/index.php$1" [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},P]

That won’t work with your installation — look at the paths — but you can tweak it for any Slim app on any host, if you know the paths and have the required modules.

There’s a lot going on in there, and I’ll get into the details below, but these are the problems I had getting this to work:

  • The mod_proxy_fcgi module doesn’t forward the Authorization header, so you need to pass the HTTP_AUTHORIZATION environment variable with the request, or the “Authorization: bearer foboarbaz” doesn’t get to the application.
  • Slim applications rely on the Apache AcceptPathInfo feature. To use it, I used a .htaccess file with a RewriteRule to convert all the URLs like api/foobar into api/index.php/foobar. PHP-FPM doesn’t read the .htaccess file, because it’s not Apache httpd, so all requests got 404 errors. I needed to do the modification in this config line.

The details, background information, and benchmarks are in the next pages.

Pages: 1 2 3 4 5 6

Leave a Reply