PHP-FPM, pretty URLs, and Apache configuration

Hi all.

I have a LAMP stack running yii2. Trying to enable pretty URLs and I think my php-fpm proxy rule in Apache vhost, or .htaccess is not catching it for some reason.

Any suggestions how to fix this so I can use pretty URLs? php-fpm is throwing an error. Regular urls work fine. This php-fpm configuration also works for other php url rewriting apps like wordpress.




    ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/var/run/php-fpm/|fcgi://localhost/path/to/document/root/web/


here is my Yii .htaccess:

RewriteEngine on

# If a directory or a file exists, use it directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward it to index.php

RewriteRule . index.php

error from logs:

[Thu Dec 01 16:10:22.192123 2016] [proxy_fcgi:error] [pid 11610] [client] AH01071: Got error ‘Primary script unknown\n’, referer:

For example, site/about results in a file not found error by php-fpm

Yii urlmanager section

'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

            'rules' => [

	            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',

	            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',

	            '<controller:\w+>/<id:\d+>' => '<controller>/view',





In case anyone else wants to do Apache, php-fpm sockets, and Yii2 pretty urls together, here is the configuration that I used to get everything working:

Substitute your own IP address, host names, and directories below in your apache virtual host config



 DocumentRoot /var/www/html/vhost_root/web


 ErrorLog logs/

 CustomLog logs/ common

 # redirect everything to https if you want, otherwise copy the config from below to here

 RewriteEngine On

 RewriteCond %{SERVER_PORT} 80

 RewriteRule ^(.*)$$1 [R,L]


<VirtualHost *:443>

 DocumentRoot "/var/www/html/vhost_root/web"

 ServerName glyphbio.local:443

 ErrorLog logs/

 TransferLog logs/

 LogLevel debug

 SSLEngine on

 SSLProtocol all -SSLv2


 # enter your own SSL certificates 

 SSLCertificateFile /etc/pki/httpd/

 SSLCertificateKeyFile /etc/pki/httpd/

 CustomLog logs/ "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

 Options -Indexes +FollowSymLinks +ExecCGI +MultiViews

 RewriteEngine On

 RewriteCond %{REQUEST_FILENAME} !-f

 # allow direct loading of urls matching these, everythign else goes to php-fpm

 RewriteCond %{REQUEST_URI} !^/(css|js|images|assets)/ [NC]

 RewriteRule "^(.*)" "/index.php" [L,PT,QSA]

 ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/var/run/php-fpm/|fcgi://localhost/var/www/html/vhost_root/web


be sure to restart apache service

Comment the following lines out of your web/.htaccess file, if you have them.

### this will be ignored by php-fpm ###

#RewriteEngine on

# If a directory or a file exists, use it directly

#RewriteCond %{REQUEST_FILENAME} !-f

#RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward it to index.php

#RewriteRule . index.php

Using this method, you can run Yii2 pretty urls on Apache with php-fpm on a shared server with unix sockets, securely. Took me a while to piece these together so hopefully this helps someone else

1 Like