简体   繁体   中英

Setting up Symfony2 to work with apache + php-fpm

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!).

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM