PHP之Laravel框架使用問題彙總與解決方式

Laravel作爲市場上最受歡迎的PHP MVC框架之一,有許多開發者用戶在使用Laravel。最近剛剛接觸Laravel框架,也遇到了一些問題,這裏總結經驗並將解決方式記錄如下,希望對遇到同樣問題的開發者有所幫助。

問題一:多環境下Apache服務器無法啓動

使用wamp或xampp等PHP集成安裝環境,遇到Apache服務器無法啓動,錯誤提示如下:

19:30:45 [Apache] Attempting to start Apache app...
19:30:45 [Apache] Status change detected: running
19:30:50 [Apache] Status change detected: stopped
19:30:50 [Apache] Error: Apache shutdown unexpectedly.
19:30:50 [Apache] This may be due to a blocked port, missing dependencies,
19:30:50 [Apache] improper privileges, a crash, or a shutdown by another method.
19:30:50 [Apache] Press the Logs button to view error logs and check
19:30:50 [Apache] the Windows Event Viewer for more clues
19:30:50 [Apache] If you need more help, copy and post this
19:30:50 [Apache] entire log window on the forums

從上述錯誤提示可以看出是端口占用導致的問題,從apache/conf/httpd.conf配置文件查看監聽端口,默認爲80端口,即瀏覽器默認端口。

Windows系統下解決方式:

第一步:打開命令提示符,使用命令netstat -ano 來查看端口的佔用情況。
命令:netstat -ano|findstr “80” 查看指定端口80的佔用情況。

查看端口占用

從圖中我們可以看到監聽80端口狀態爲Lisening的進程PID爲118856。

第二步:知道了進程號,可以通過查看任務管理器服務比對PID號找出對應進程,或者使用命令tasklist來列出進程進行確認。
命令:tasklist|findstr “118856”

查看進程號

httpd.exe是Apache相關程序,由於我機器同時安裝了wamp,所以httpd.exe進程實際爲wamp的Apache進程,其佔用了80端口,所以xampp的Apache服務便無法再啓動。

第三步:找到佔用Apache端口的進程後,使用命令終止該進程。
命令:taskkill /f /t /im httpd.exe
強制終止映像名爲httpd.exe的進程和任何由此啓動的子進程。
或者使用命令:taskkill /pid 118856
通過pid號來終止進程。

終止進程

打開Xampp控制面板重啓啓動Apache服務,啓動成功,如圖:

Apache重啓成功

Linux系統下,可以使用如下命令操作(此處用nginx舉例)

查看端口占用情況:

netstat -anp | grep 80

lsof -i:80

Linux系統netstat命令

查看進程號對應進程的安裝目錄

ps –ef|grep 33308

查看進程的位置

查看nginx進程的端口使用情況

ps –aux|grep nginx

ps –aux|grep 42355

確定進程號後使用kill命令終止進程。命令格式:kill [信號或選項] PID(s)

kill –l可查看所有信號列表

SIGTERM - 此信號請求一個進程停止運行。此信號是可以被忽略的。進程可以用一段時間來正常關閉,一個程序的正常關閉一般需要一段時間來保存進度並釋放資源。換句話說,它不是強制停止。

SIGKILL - 此信號強制進程立刻停止運行。程序不能忽略此信號,而未保存的進度將會丟失。

默認信號(當沒有指定的時候)是SIGTERM。當它不起作用時,可以使用下面的命令來強制kill掉一個進程:

kill SIGKILL PID

或者

kill -9 PID

(這裏”-9”代表着SIGKILL信號)

同時終止多個進程:

kill -9 PID1 PID2 PID3

上述nginx進程的終止命令爲:

kill -9 42355.

除了查詢佔用端口後殺死進程的方式,還可以採用添加、修改監聽端口的方式,這樣不用終止相關端口的進程,不影響現有程序進程的正常使用。Apache需要修改的配置文件具體如下:

默認80端口,修改配置文件/apache/conf/httpd.conf
HTTPS默認443端口,修改配置文件/apache/conf/extra/httpd-ssl.conf

nginx的端口號修改、設置就更簡單了,這裏無須多說了。

問題二:頁面出現循環重定向

首先要保證相關文件配置沒有問題,依然出現循環重定向提示。先看一下配置文件。
配置文件/apache/conf/extra/httpd-vhosts.conf配置內容:

NameVirtualHost *:80

<VirtualHost *:80>
   DocumentRoot "F:/xampp/htdocs"
   ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "F:/xampp/htdocs/bi.xxx.com/public"
    ServerName www.bitest.com
    ErrorLog "logs/webapp-host2.example.com-error.log"
    CustomLog "logs/webapp-host2.example.com-access.log" common
</VirtualHost>

接着本地host文件設置對應關係 127.0.0.1 www.bitest.com,配置完畢。然後訪問該域名,報錯如下圖:

重定向循環

重定向循環提示

從入口文件public/index.php開始,步步跟蹤程序,發現是由於權限問題導致。具體問題定位到文件位置:
F:\xampp\htdocs\bi.feiliu.com\vendor\laravel\framework\src\Illuminate\Foundation\ ProviderRepository.php

代碼塊:

    /**
     * Write the service manifest file to disk.
     *
     * @param  array  $manifest
     * @return array
     */
    public function writeManifest($manifest)
    {
        $path = $this->manifestPath.'/services.json';

        $this->files->put($path, json_encode($manifest, JSON_PRETTY_PRINT));

        return $manifest;
    }

文件目錄不存在或者文件夾沒有可寫權限,使得writeManifest方法寫配置失敗,從而拋出異常錯誤(app/controllers/ErrorController),我的項目中異常錯誤處理方式爲跳轉到/error?code=201。

解決方法:

檢查app\storage\meta\services.json 目錄是否存在,檢查目錄的寫權限。如果在Linux環境下首先要保證寫入權限,還要確保SELinux允許在此目錄寫入。操作步驟如下:
1)更改 app/storage目錄的屬主

chown apache:apache app/storage

2)更改 app/storage 目錄權限

chmod -R 775 app/storage

3)禁止 SELinux 對app/storage目錄的權限限制

su -c "chcon -R -h -t httpd_sys_script_rw_t [fullpath]/app/storage"

OR

setsebool -P httpd_unified 1

問題三:Error in exception handler 或failed to open stream:permission denied in storage/meta/services.json

導致該問題的原因是文件目錄權限不足的問題。有些服務器出於安全考慮可能會禁止一些目錄的權限。解決方式可參考:

chgrp -R www-data app/storage (chgrp -R apache app/storage)

Or with chown.

chown -R :www-data app/storage

On Mac, the above commands did not work. However, this command did:

sudo chown -R _www app/storage 

(replace _www with your Apache server name if necessary)

Or with chmod.

chmod -R 775 app/storage

改變服務器屬主(通常爲apache 或 www-data,可能因操作系統不同而名稱不同)的方式比將文件所有權限開放給用戶安全的多,一般情況775的權限已經足夠了。

From the Laravel web site : http://laravel.com/docs/4.2/installation

Laravel may require one set of permissions to be configured: folders within app/storage require write access by the web server.

最後,有一點需要特別注意的是儘量安裝較新版本的 PHP安裝包。Laravel 5.0對 PHP 版本的要求是 >=5.4,Laravel 5.1 要求 PHP 版本 >=5.5.9。 PHP 5.4 是最後一個支持 Windows XP 和 Windows 2003 的版本了。已證實從PHP5.5開始不再支持Windows XP了,你可能不得不升級Windows7/8了。

參考文章:

httpd_selinux
services.json failed to open stream: Permission denied in Laravel 4
Error in exception handler. - Laravel
在 Windows 上快速安裝並運行 Laravel 5.x
4 Effective Methods to Disable SELinux Temporarily or Permanently
Disable SELinux for only Apache / httpd in Linux

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章