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服務,啟動成功,如圖:
Linux系統下,可以使用如下命令操作(此處用nginx舉例)
檢視端口占用情況:
netstat -anp | grep 80
或
lsof -i:80
檢視程序號對應程序的安裝目錄
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