无法让Symfony在生产服务器上运行

时间:2016-01-14 14:52:58

标签: php apache .htaccess symfony

编辑:对于遇到同样问题的人,严肃地说,只需创建一个子域并将其指向symfony web目录。 Symfony的url重写系统显然是围绕某些的根目录构建的。其他任何事情都不值得头痛。

过去两天我一直试图在我们的服务器上运行一个新的Symfony项目。我安装项目没问题,但是当我尝试转到onCreateOptionsMenu时,我的网站找不到404页错误,而不是Symfony,与$input = Request::all(); 相同。

这是一个如此简单的问题,但它让我头疼几个小时没有头脑。

我没有对我们的网站或新的Symfony项目的.htaccess文件做任何想法。

Symfony .htaccess:

example.net/symfony/web/app_dev.php

网站.htaccess:

app.php

我在Apache error_log中收到这些错误:

# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex app.php

# By default, Apache does not evaluate symbolic links if you did not enable this
# feature in your server configuration. Uncomment the following line if you
# install assets as symlinks or if you experience problems related to symlinks
# when compiling LESS/Sass/CoffeScript assets.
# Options FollowSymlinks

# Disabling MultiViews prevents unwanted negotiation, e.g. "/app" should not resolve
# to the front controller "/app.php" but be rewritten to "/app.php/app".
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Determine the RewriteBase automatically and set it as environment variable.
    # If you are using Apache aliases to do mass virtual hosting or installed the
    # project in a subdirectory, the base path will be prepended to allow proper
    # resolution of the app.php file and to redirect to the correct URI. It will
    # work in environments without path prefix as well, providing a safe, one-size
    # fits all solution. But as you do not need it in this case, you can comment
    # the following 2 lines to eliminate the overhead.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # Sets the HTTP_AUTHORIZATION header removed by Apache
    RewriteCond %{HTTP:Authorization} .
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/app.php`). Only do this redirect on the initial
    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
    # endless redirect loop (request -> rewrite to front controller ->
    # redirect -> request -> ...).
    # So in case you get a "too many redirects" error or you always get redirected
    # to the start page because your Apache does not expose the REDIRECT_STATUS
    # environment variable, you have 2 choices:
    # - disable this feature by commenting the following 2 lines or
    # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
    #   following RewriteCond (best solution)
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule ^ %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 302 ^/$ /app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>

如果您需要任何其他信息,我会尽我所能。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

我仍然有太低的代表只是评论,所以我会发布一个答案。

你的htaccess与404问题关系不大。

如果您使用的是Debian,请转至/ etc / apache2 / sites-available并为Symfony创建一个页面,如此处显示http://symfony.com/doc/current/cookbook/configuration/web_server_configuration.html

确保定义正确的symfony文件夹位置,否则它将继续返回404错误。

看起来应该是这样的

<VirtualHost *:80>
    ServerName YOURDOMAINNAME.DOMAIN
    ServerAlias www.YOURDOMAINNAME.DOMAIN

    DocumentRoot FULL_SYMFONY_PATH
    <Directory FULL_SYMFONY_PATH>
        AllowOverride None
        Order Allow,Deny
        Allow from All

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app.php [QSA,L]
        </IfModule>
    </Directory>

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeScript assets
    # <Directory /var/www/project>
    #     Options FollowSymlinks
    # </Directory>

    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>

此配置还会禁用htaccess。

将此配置命名为YOURDOMAINNAME.conf,然后将其输入到控制台a2ensite YOURDOMAINNAME.conf。

我认为还有一些额外的步骤,但我不在普通计算机上,如果它不起作用,请告诉我。

是的,现在我想我记得,你需要用这行a2enmod重写启用mod_rewrite,然后重启apache。

答案 1 :(得分:0)

不确定它是否有帮助,但是尝试将DocumentRoot更改为symfony / web目录(因此web文件夹是网站的根目录)?