【问题标题】:Setting up Symfony2 to work with apache + php-fpm设置 Symfony2 以使用 apache + php-fpm
【发布时间】:2014-02-02 16:42:56
【问题描述】:

如何设置 Symfony2 以在 Apache (Apache 2.4) 和 php-fpm 上工作?

目的是摆脱 .htaccess 文件并将所有内容移至 apache 配置中。

不用担心 PHP-FPM,配置看起来像这样:

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

这个帖子是让我发布我自己的发现(如果我弄错了,也许会更正!)。

【问题讨论】:

    标签: apache symfony config php


    【解决方案1】:

    我更喜欢只使用 ProxyPassMatch 来完成这项工作。使用此方法,您在.htaccess 中的重写规则无需更改。

    这是我的 Apache2 vhost 文件:

    <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
    

    您需要添加的唯一行是 ProxyPassMatch。其他一切都保持不变。

    不要忘记更改 symfony2 网络目录的 IP 地址和路径。

    【讨论】:

    • 我发现通过这种配置,所有页面 URL 和重定向都包含到 web 文件夹的完整文件夹路径。因此,从您的示例中,重定向到 app_dev.php 会将您带到 www.grae.labs.brainglove.com/srv/symfony2/2.4.4/grae/web/app_dev.php,而不是 www.grae.labs.brainglove .com/app_dev.php。还有什么我需要改变的吗?
    【解决方案2】:

    要将请求传递给 PHPFPM,建议的 RewriteRule 是:

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

    这就是我最终的结果。到目前为止它似乎有效,但也许有更好的方法?

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

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 2016-10-25
      • 2023-03-10
      • 2015-08-14
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 2016-11-06
      相关资源
      最近更新 更多