【问题标题】:Configuring Symfony 5 Application on Apache server在 Apache 服务器上配置 Symfony 5 应用程序
【发布时间】:2021-01-22 21:40:12
【问题描述】:

我需要一些关于在 apache2 Web 服务器上配置 Symfony 的帮助。

我目前的项目类别结构是:

var/www/domain.com/public_html

public_html 内部是我的 Symfony 应用程序所在的位置。

我一直在弄清楚我在 .htaccess 或 /etc/apache2/sites-available/domain.com.conf 文件中做错了什么。

我正在使用 symfony/apache-pack。根据文档,我的 .htaccess 文件位于 /public 文件夹中。这里是:

DirectoryIndex index.php

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
    RewriteRule .* - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} .+
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]

    RewriteCond %{ENV:REDIRECT_STATUS} =""
    RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 307 ^/$ /index.php/
    </IfModule>
</IfModule>

这是我的 /etc/apache2/sites-available/domain.com.conf

<VirtualHost *:80>
    ServerAdmin someemail@example.com
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    DocumentRoot /var/www/mydomain.com/public_html/public
    <Directory /var/www/mydomain.com/public_html/public>
        AllowOverride All
        Order allow,deny
        Allow from All
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.mydomain.com [OR]
RewriteCond %{SERVER_NAME} =mydomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

我还阅读了this 并更新了 000-default.conf 如下:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerAlias www.mydomain.com
        DocumentRoot /var/www/mydomain.com/public_html/public

        <Directory /var/www/domain.com/public_html/public>
            Options FollowSymlinks MultiViews
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
            Allow from All
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <IfModule mod_dir.c>
            DirectoryIndex index.php index.pl index.cgi index.html index.xhtml >
        </IfModule>

</VirtualHost>

目前我仍然看到 symfony 项目文件夹结构。

我试过了:

  1. 在 .htaccess 中将 DirectoryIndex 更改为 /public/index.php
  2. 在 .conf 文件中将 DocumentRoot 更改为 /va/www/domain.com/public_html/public
  3. 已尝试清除缓存

到目前为止,似乎没有任何帮助。

对任何建议都非常有帮助。

【问题讨论】:

  • DocumentRootDirectory 应该指向 .../public_html/public。当你这样做时会发生什么,它如何“无济于事”?您是否看到空白页或...?服务器日志也会很有帮助。
  • @msg,我试过了。将 public 添加到 DocumentRoot 和 Directory,重新启动 apache 并清除缓存。获得相同的 symfony 项目结构
  • 相同的结构?使用srcpublic 等?然后我只能想到两件事:浏览器缓存或您的第二个VirtualHost(您要重定向到的 https 上的那个)仍然配置有public_html。这是what I usually do,只需处理一个VHost 配置。
  • @msg,我已经更新了帖子。是的,同样的 symfony 项目结构。浏览器缓存被清除

标签: php apache .htaccess symfony


【解决方案1】:

我看到您正在编辑以 &lt;VirtualHost *:80&gt;......

这意味着您的网站应该可以通过 80 端口或换句话说通过 http 协议访问。但!如果实际上您是通过 https 协议访问您的网站,那么请确保您正在编辑正确的 Apache2 .conf 文件。

如果该网站可以通过 https 协议访问(尤其是如果您使用 LetEncrypt),则尝试定位 /etc/apache2/sites-available/....-le-ssl.conf 配置文件(或类似的具有相应域名和 ssl 前缀或后缀的配置文件名称)。

编辑该文件后,不要忘记使用$sudo service apache2 restart 重新启动 Apache2 网络服务器并再次测试网站。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-09
    • 2020-05-29
    • 2017-04-12
    • 2019-01-14
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多