2016-11-13 17 views
0

我目前在TeamTreehouse.com上了解Slim框架,並遇到了一個我無法解決的問題。Slim 2.6.2 render()僅適用於Index.html

在這個項目中,我們已經安裝了Slim via Composer,並且在我們的文檔根目錄下安裝了.htaccess和index.php文件(在我的電腦上是/ home/daniel/src/public_html/treehouse/build_websites_php /) 。 在模板文件夾中,我們有index.html和contact.html。這是文件夾的佈局。

  • 的DocumentRoot(/家/丹尼爾/ src目錄/的public_html /樹/ build_websites_php /)
    • 的index.php
    • 的.htaccess
    • composer.json
    • composer.lock
    • 供應商/
    • 模板/
      • 的index.html
      • contact.html

在index.php文件,我實例化一個新的修身對象:

$app = new \Slim\Slim(); 

,然後調用get()方法然後在URL分別爲localhost/treehouse/build_websites /和localhost/treehouse/build_websites/contact時調用render()來呈現index.html和contact.html頁面。

$app->get('/', function() use($app) { 
    $app->render('index.html'); 
}); 

$app->get('/contact', function() use($app) { 
    $app->render('contact.html'); 
}); 

然後運行該應用程序:

$app->run(); 

我的index.html頁面顯示了罰款,但我得到一個404錯誤(不通過修身,只是服務器的默認設置),當我嘗試訪問/ contact url。下面是從我的系統中的一些規格:

  • 的Ubuntu 16.04.1 LTS
  • 的Apache 2.4.18
  • 修身2.6.2
  • PHP 7.0.8

任何在我的/ home/daniel/src/public_html /目錄可以被Apache訪問,因爲我已經在那裏運行PHP腳本了一年。

我試過here(並在每次更新conf.d或其他文件後重新啓動服務器)的建議,並沒有運氣。

任何幫助將不勝感激,我只使用PHP/Ubuntu/Apache大約一年,所以我可能錯過了一些明顯的東西!

這裏是索引。PHP文件:

<?php 

require 'vendor/autoload.php'; 

$app = new \Slim\Slim(); 

$app->get('/', function() use($app) { 
    /* When using render(), the url localhost/treehouse/build_websites_php/ to 
    gets you the home page. */ 
    $app->render('index.html'); 
}); 

/* This SHOULD bring up the contact page at url 
localhost/treehouse/build_websites_php/contact, but it doesn't! */ 
$app->get('/contact', function() use($app) { 
    $app->render('contact.html'); 
}); 

$app->run(); 

?> 

這裏是.htacess文件:

RewriteEngine On 

# Some hosts may require you to use the `RewriteBase` directive. 
# If you need to use the `RewriteBase` directive, it should be the 
# absolute physical path to the directory that contains this htaccess file. 
# 
# RewriteBase /home/daniel/src/public_html/treehouse/build_websites_php/ 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule^index.php [QSA,L] 

這裏有各種conf.d文件阿帕奇:

的/ etc /的Apache2/Apache2的。 conf

# Sets the default security model of the Apache2 HTTPD server. It does 
# not allow access to the root filesystem outside of /usr/share and /var/www. 
# The former is used by web applications packaged in Debian, 
# the latter may be used for local directories served by the web server. If 
# your system is serving content from a sub-directory in /srv you must allow 
# access here, or in any related virtual host. 
<Directory /> 
    Options FollowSymLinks 
    AllowOverride None 
    Require all denied 
</Directory> 

<Directory /usr/share> 
    AllowOverride None 
    Require all granted 
</Directory> 

<Directory /home/daniel/src/public_html> 
    Order allow,deny  
    Allow from all 
    Require all granted 
</Directory> 

我試着添加AllowOverride All作爲建議here到最後的指令,然後我無法從服務器訪問PHP文件,得到500錯誤而不是404錯誤。

/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80> 
    # The ServerName directive sets the request scheme, hostname and port that 
    # the server uses to identify itself. This is used when creating 
    # redirection URLs. In the context of virtual hosts, the ServerName 
    # specifies what hostname must appear in the request's Host: header to 
    # match this virtual host. For the default virtual host (this file) this 
    # value is not decisive as it is used as a last resort host regardless. 
    # However, you must set it for any further virtual host explicitly. 
    #ServerName www.example.com 

    ServerAdmin [email protected] 
    DocumentRoot /var/www/html 

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, 
    # error, crit, alert, emerg. 
    # It is also possible to configure the loglevel for particular 
    # modules, e.g. 
    #LogLevel info ssl:warn 

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


    # For most configuration files from conf-available/, which are 
    # enabled or disabled at a global level, it is possible to 
    # include a line for only one particular virtual host. For example the 
    # following line enables the CGI configuration for this host only 
    # after it has been globally disabled with "a2disconf". 
    #Include conf-available/serve-cgi-bin.conf 
</VirtualHost> 

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet 

/etc/apache2/sites-available/mysite.conf

<VirtualHost *:80> 
    # The ServerName directive sets the request scheme, hostname and port that 
    # the server uses to identify itself. This is used when creating 
    # redirection URLs. In the context of virtual hosts, the ServerName 
    # specifies what hostname must appear in the request's Host: header to 
    # match this virtual host. For the default virtual host (this file) this 
    # value is not decisive as it is used as a last resort host regardless. 
    # However, you must set it for any further virtual host explicitly. 
    #ServerName www.example.com 

    ServerAdmin [email protected] 
    DocumentRoot /home/daniel/src/public_html 

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, 
    # error, crit, alert, emerg. 
    # It is also possible to configure the loglevel for particular 
    # modules, e.g. 
    #LogLevel info ssl:warn 

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

    # For most configuration files from conf-available/, which are 
    # enabled or disabled at a global level, it is possible to 
    # include a line for only one particular virtual host. For example the 
    # following line enables the CGI configuration for this host only 
    # after it has been globally disabled with "a2disconf". 
    #Include conf-available/serve-cgi-bin.conf 
</VirtualHost> 

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet 

回答

0

OK哇我終於明白了,這要感謝Mika指着我朝着正確的方向。事實證明,我的/etc/apache2/sites-available/mysite.conf文件所需的下列指令:

<Directory /home/daniel/src/public_html > 
    AllowOverride All 
</Directory> 

我曾嘗試添加該指令/etc/apache2/apache2.conf除了其他指令,像這樣:

<Directory /> 
    Options FollowSymLinks 
    AllowOverride None 
    Require all denied 
</Directory> 

<Directory /usr/share> 
    AllowOverride None 
    Require all granted 
</Directory> 

<Directory /home/daniel/src/public_html> 
    Order allow,deny  
    Allow from all 
    Require all granted 
    AllowOverride All #THIS DIDN'T WORK 
</Directory> 

AllowOverride All從上面通過來自服務器的500錯誤。顯然,它必須自己在/etc/apache2/sites-available/mysite.conf文件中,誰知道!

我也跑sudo a2enmod rewrite && sudo /etc/init.d/apache2 restart確保mod_rewrite的是發現該錯誤消息後加載(感謝米卡爲指出,檢查日誌文件!):

[Sun Nov 13 10:37:51.054347 2016] [core:alert] [pid 10979] [client ::1:51900] /home/daniel/src/public_html /treehouse/build_websites_php/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration 

我這樣做,加入AllowOverride All指令之前,所以我不確定它是否解決了這個問題,但我想我會爲任何感興趣的人記錄它。

這些網站就如何最終解決問題的有價值的信息:

0

它看起來像你的Apache設置不允許.htaccess文件覆蓋任何設置。向apache2.conf添加如下內容。

<Directory /home/daniel/src/public_html> 
    AllowOverride All 
    Order allow,deny  
    Allow from all 
    Require all granted 
</Directory> 
+0

當我加*的AllowOverride所有*指令apache2.conf服務器返回500錯誤,我根本無法運行任何PHP腳本。 –

+1

檢查您的錯誤日誌。 –

+0

我做了,發現以下錯誤:[Sun Nov 13 10:37:51。054347 2016] [core:alert] [pid 10979] [client :: 1:51900]/home/daniel/src/public_html /treehouse/build_websites_php/.htaccess:無效的命令'RewriteEngine',可能是拼寫錯誤或模塊定義包括在服務器配置 ' 我試過這個解決方案'sudo a2enmod rewrite && sudo service apache2 restart' from [here](http://stackoverflow.com/questions/10144634/htaccess-invalid-command-rewriteengine-perhaps-misspelled - 或 - 定義),但沒有骰子,我仍然得到404錯誤。 –