我该如何正确地在Heroku上部署我的网站?

3

我正在尝试将我的网站部署到Heroku上,我使用的实现方式是使用PHP的Model View Controller。但我不知道发生了什么事情,当我尝试访问主页(或索引)时,它可以正常工作,但当我尝试访问我的网站中的其他页面时,会出现这样的情况:

进入图像描述

我知道这种情况发生的原因之一是我在我的路由器中使用了以下内容:

$currentURL = $_SERVER['PATH_INFO'] ?? '/';
    //var_dump($_SERVER);
    
    $method = $_SERVER['REQUEST_METHOD'];

    if($method === 'GET'){
        $fn = $this->routesGET[$currentURL] ?? null;
    } else{
        $fn = $this->routesPOST[$currentURL] ?? null;
    }

因此,我在我的网站上显示了 PHP 的全局变量 $_SERVER,并注意到其中的 $_SERVER['PATH_INFO'] 没有出现。所以,我猜测问题来自于 Apache 的配置,因为我正在使用 Apache2 和 PHP。但这是我第一次这样做,我不知道如何配置,如果你能帮我,我会非常感激。
这是我的目录: 进入图像描述 最后是我的 procfile:
web: vendor/bin/heroku-php-apache2 public/
1个回答

0

下面是配置基于MVC的Web应用程序的一般适用步骤。假定以下设置适用于Web服务器版本:Apache HTTP Server v2.4

1)阻止访问所有目录和文件:

首先,在Apache的配置文件中,默认应该阻止所有目录和文件的访问:

# Do not allow access to the root filesystem.
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

# Prevent .htaccess and .htpasswd files from being viewed by Web clients.
<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

2) 允许访问默认目录:

应该允许访问默认目录(这里是/var/www/),该目录通常用于项目:

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

我的建议:出于安全考虑,此位置应仅包含一个index.php和一个index.html文件,它们分别显示一个简单的"Hello"消息。所有Web项目都应在其他目录中创建,并应分别设置对它们的访问权限,如下所述。

3)设置对独立项目目录的访问权限:

假设您将项目创建在另一个位置(例如目录/path/to/my/sample/mvc/)而不是默认位置(/var/www/),则考虑到只有子文件夹public可以从外部访问,请为其创建Web服务器配置,如下所示:

ServerName www.my-sample-mvc.com
DocumentRoot "/path/to/my/sample/mvc/public"

<Directory "/path/to/my/sample/mvc/public">
    Require all granted

    # When Options is set to "off", then the RewriteRule directive is forbidden!
    Options FollowSymLinks
    
    # Activate rewriting engine.
    RewriteEngine On
    
    # Allow pin-pointing to index.php using RewriteRule.
    RewriteBase /
    
    # Rewrite url only if no physical folder name is given in url.
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # Rewrite url only if no physical file name is given in url.
    RewriteCond %{REQUEST_FILENAME} !-f
    
    # Parse the request through index.php.
    RewriteRule ^(.*)$ index.php [QSA,L]
</Directory>

请注意,上述设置可以在以下位置定义:
  • 在Apache的配置文件中,或者
  • 在项目内的.htaccess文件中,或者
  • 在虚拟主机定义文件中。

如果使用虚拟主机定义文件,则必须在标签<VirtualHost></VirtualHost>之间包含设置:

<VirtualHost *:80>
    ... here come the settings ...
</VirtualHost>

注意:在更改配置设置后,请不要忘记重新启动 Web 服务器。

一些资源:


非常感谢你,Dakis。看起来问题是由于我的.htaccess和Apache配置引起的。因此,你的答案非常完整,帮助我解决了这个问题。感谢你的支持。问候。 - Ángel Cruz
@ÁngelCruz 你好。不用谢,我很高兴能帮到你。祝你好运。 - PajuranCodes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接