【问题标题】:How can i deploy my website on Heroku properly?如何在 Heroku 上正确部署我的网站?
【发布时间】:2021-07-26 08:45:07
【问题描述】:

我正在尝试在 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/

【问题讨论】:

    标签: php apache heroku model-view-controller deployment


    【解决方案1】:

    这些是配置基于 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 文件,每个文件都显示一个简单的"你好” 消息。所有 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 文件中,或
    • 在虚拟主机定义文件中。

    如果使用虚拟主机定义文件,设置必须包含在标签&lt;VirtualHost&gt;和&lt;/VirtualHost&gt;之间:

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

    注意:不要忘记在每次更改配置设置后重新启动网络服务器。

    一些资源:

    【讨论】:

    • 非常感谢,达基斯。似乎问题来自 .htaccess 和我关于 Apache 的配置。所以,你的答案是如此完整,并帮助我解决这个问题。感谢您的支持。问候。
    • @ÁngelCruz 嗨。不客气。我很高兴能帮上忙。祝你好运。
    猜你喜欢
    • 2015-05-15
    • 2016-12-21
    • 2021-08-21
    • 2018-12-11
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    相关资源
    最近更新 更多