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

How can i deploy my website on Heroku properly?

我试图在 Heroku 上部署我的网站,我为此使用的实现是带有 PHP 的模型视图控制器。我不知道发生了什么,但是当我尝试访问主页(或索引)时,这一切正常,当我尝试访问 mi 网站上的其他页面时,会发生如下情况:

enter image description here

我知道发生这种情况的一个原因,我在我的路由器中使用了下一个:

$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。所以,我不知道如何配置,因为我是第一次这样做,如果你能帮助我,我真的很感谢你。

这是我的目录: enter image description here

最后是我的 procfile:

web: vendor/bin/heroku-php-apache2 public/

这些是配置基于 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) 设置访问单独的项目目录:

假设您在默认目录 (/var/www/) 以外的其他位置(如目录 /path/to/my/sample/mvc/)创建项目。然后,考虑到只能从外部访问子文件夹 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 服务器。

一些资源:

  • What is Options +FollowSymLinks? (1)
  • What is Options +FollowSymLinks? (2)
  • RewriteRule Flags
  • Exposed folders in MVC application
  • mod_rewrite: what does this RewriteRule do?