3

How does one set up Symfony2 to work on Apache (Apache 2.4) combined with php-fpm?

The aim is to get rid of the .htaccess files and move everything into the apache config.

Without worrying about PHP-FPM, the config looks something like this:

<VirtualHost *:80>
  ServerName mysymfonyproject.com
  ServerAlias www.mysymfonyproject.com
  ErrorLog  /path/to/logs/mysymfonyproject_error.log
  CustomLog /path/to/logs/mysymfonyproject_access.log combined

  DocumentRoot "/path/to/sites/mysymfonyproject/web"
  <Directory "/path/to/sites/mysymfonyproject/web">
    AllowOverride None
    Require all granted

    ## Start directives copied from standard Sf .htaccess
    DirectoryIndex app.php
    <IfModule mod_rewrite.c>
        RewriteEngine On

        RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
        RewriteRule ^(.*) - [E=BASE:%1]

        RewriteCond %{ENV:REDIRECT_STATUS} ^$
        RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

        RewriteCond %{REQUEST_FILENAME} -f
        RewriteCond %{REQUEST_URI} .*\.php

        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule .? - [L]

        RewriteRule .? %{ENV:BASE}/app.php [L]
    </IfModule>
    ## End directives copied from standard Sf .htaccess
  </Directory>
</VirtualHost>

This thread is for me to post my own findings (and perhaps be corrected if I got it wrong!).

2 Answers 2

3

I prefer to just use ProxyPassMatch to make this work. By using this method, your rewrite rules in .htaccess don't need to be changed.

Here is my Apache2 vhost file:

<VirtualHost 192.168.100.51:80>                                                     
ServerName grae.labs.brainglove.com                                             
ServerAlias www.grae.labs.brainglove.com                                        

DocumentRoot /srv/symfony2/2.4.4/grae/web                                       
DirectoryIndex app_dev.php                                                      

ProxyPassMatch  ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9001/srv/symfony2/2.4.4/grae/web/$1                                                                             

<Directory /srv/symfony2/2.4.4/grae/web>                                        
    AllowOverride All                                                           
    Options FollowSymLinks                                                      
    Require all granted
</Directory>

ErrorLog /var/log/httpd/grae.labs.brainglove.com_error.log
CustomLog /var/log/httpd/grae.labs.brainglove.com_access.log combined

The only line you need to add is the ProxyPassMatch. Everything else stays the same.

Don't forget to change the IP address and path to your symfony2 web directory.

Sign up to request clarification or add additional context in comments.

1 Comment

I find with this configuration that all in page URLs and redirects include the complete folder path to the web folder. So from your example, a redirect to app_dev.php would take you to www.grae.labs.brainglove.com/srv/symfony2/2.4.4/grae/web/app_dev.php, instead of www.grae.labs.brainglove.com/app_dev.php. Is there something else I need to change?
0

To pass a request through to PHPFPM, the suggested RewriteRule was:

RewriteRule ^(.*\.php(/.*)?)$ fcgi://uds=%2fpath%2fto%2fwww.sock/%{REQUEST_FILENAME} [P,END]

Here's what I ended up. It seems to be working so far, but perhaps there's a better way?

<VirtualHost *:80>
  ServerName mysymfonyproject.com
  ServerAlias www.mysymfonyproject.com
  ErrorLog  /path/to/logs/mysymfonyproject_error.log
  CustomLog /path/to/logs/mysymfonyproject_access.log combined

  DocumentRoot "/path/to/sites/mysymfonyproject/web"
  <Directory "/path/to/sites/mysymfonyproject/web">
    AllowOverride None
    Require all granted

    ## Start directives copied from standard Sf .htaccess
    DirectoryIndex app.php
    <IfModule mod_rewrite.c>
        RewriteEngine On

        RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
        RewriteRule ^(.*) - [E=BASE:%1]

        RewriteCond %{ENV:REDIRECT_STATUS} ^$
        RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

        RewriteCond %{REQUEST_FILENAME} -f
        RewriteCond %{REQUEST_URI} .*\.php
        # Need to add the phpfpm call here so that php files (including app_dev.php) are passed to FPM
        RewriteRule ^(.*\.php(/.*)?)$ fcgi://uds=%2fpath%2to%2fwww.sock/%{REQUEST_FILENAME} [P,END]

        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule .? - [L]

        RewriteRule .? %{ENV:BASE}/app.php [L]

        # The main phpfpm call is added at the end to pass php requests through to FPM
        RewriteRule ^(.*\.php(/.*)?)$ fcgi://uds=%2fpath%2to%2fwww.sock/%{REQUEST_FILENAME} [P,END]
    </IfModule>
    ## End directives copied from standard Sf .htaccess
  </Directory>
</VirtualHost>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.