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.




<VirtualHost 1.2.3.4:80>

    ...

    ServerName yii2.mysite.com

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

</VirtualHost>

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 4.3.2.1:48516] AH01071: Got error ‘Primary script unknown\n’, referer: https://yii2.mysite.com/

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',

            ],

        ],



Thanks!

SOLVED

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


<VirtualHost 127.0.0.1:80>

 ServerAdmin example.com

 DocumentRoot /var/www/html/vhost_root/web

 ServerName example.com

 ErrorLog logs/example.com-error_log

 CustomLog logs/example.com-access_log common

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

 RewriteEngine On

 RewriteCond %{SERVER_PORT} 80

 RewriteRule ^(.*)$ https://example.com$1 [R,L]

</VirtualHost>

<VirtualHost *:443>

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

 ServerName glyphbio.local:443

 ErrorLog logs/example.com-error_log

 TransferLog logs/example.com-access_log

 LogLevel debug

 SSLEngine on

 SSLProtocol all -SSLv2

 SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5

 # enter your own SSL certificates 

 SSLCertificateFile /etc/pki/httpd/example.com/server.crt

 SSLCertificateKeyFile /etc/pki/httpd/example.com/server.key

 CustomLog logs/example.com-access_log "%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/example.com.sock|fcgi://localhost/var/www/html/vhost_root/web

</VirtualHost>

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