下面由laravel框架教程欄目給大家總結Laravel項目部署到線上需要注意的一些問題,希望對需要的朋友有所幫助!
準備部署 Laravel 應用到生產環境時,卻出現了以下一些問題,你在本地上基本不會出現問題,但是到線上很多問題都出來了。整理了一些問題與bug,希望在你部署laravel項目的時候,如果出現類似問題,可以用得到吧! 部署不出現任何問題,那就再再好不過了。
首先,我們再做調試的時候,請先開啟php顯示錯誤,以便做調試
vim /usr/local/php/etc/php.ini 修改display_errors = Off 改為display_errors = On
改完后記得要重啟服務器。?
1 目錄權限問題
為了運行 Laravel,我們需要為一些項目目錄配置權限.
Laravel 項目需要對目錄 storage/, bootstrap/cache, public / 賦予讀寫權限
//賦予三個目錄讀寫權限 chmod?-R?777?bootstrap/ chmod?-R?777?storage/ chmod?-R?777?public/
如果你用的是一鍵安裝包lnmp,請注意,LNMP 一鍵安裝包中含有.user.ini,權限會被拒絕。
需使用:
chattr -i /{目錄}/.user.ini
并刪除:
rm .user.ini
2 nginx的配置文件的問題
假設你的nginx.conf文件的路徑是放在這里:/usr/local/nginx/conf/nginx.conf文件,找到 server{}字段中
如下代碼
#include enable-php.conf;
你的nginx里存不存在這個文件,請注釋,因為這個會導致500錯誤。原因是:
引入了 php 配置,其中有句 try_files 開啟就有報錯.
#新增?支持laravel?優雅鏈接,在laravel?文檔里有說明 location?/?{ ????try_files?$uri?$uri/?/index.php?$query_string; } #新增?支持php?的配置 location?~?.php$?{ #不能有下面這句?try_files?,不然報錯500 #?try_files?$uri?/index.php?=404; fastcgi_split_path_info?^(.+.php)(/.+)$; #這句注意?后面是.sock?不是127.0.0..1 fastcgi_pass??unix:/tmp/php-cgi.sock; fastcgi_index?index.php; include?fastcgi_params; fastcgi_param?SCRIPT_FILENAME?$document_root$fastcgi_script_name; }
附件:給一個laravel的nginx配置
server{ listen 80; server_name 網站域名; index index.php index.html index.htm default.html default.htm default.php; root /var/www/html/act/public; //網站存放目錄,laravel的入口文件在public里 #include rewrite/none.conf; #error_page 404 /404.html; # Deny access to PHP files in specific directory #location ~ /(wp-content|uploads|wp-includes|images)/.*.php$ { deny all; } #include enable-php-pathinfo.conf; #添加以下這句就好了 location / { try_files $uri $uri/ /index.php?$query_string; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } location ~ .php$ { root /var/www/html/act/public; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }# if (!-e $request_filename){# rewrite ^/(mo_bile|admin|physician|home|seller)/(.*)$ /$1/index.php?$2;# } location ~ .php$ { fastcgi_param PATH_INFO $request_uri; } access_log /home/wwwlogs/hd.log;}
3 PHP擴展要記得開啟
部署項目之前要先確保php.ini里的擴展已經開啟,開啟的擴展有:php_fileinfo, php_mbstring, php_openssl,這幾個都是laravel需要的。
不管是修改了nginx還是php.ini,修改完后,請記得要重啟nginx與php-fpm。
4 laravel項目在git上clone到線上可能會缺少一下核心庫,開啟php錯誤顯示會看到類似以下的問題
Warning:?require():?open_basedir?restriction?in?effect.? File(/home/wwwroot/***/bootstrap/autoload.php)?is?not?within?the?allowed?path(s):? (/home/wwwroot/***/public/:/tmp/:/proc/)?in?/home/wwwroot/***/public/index.php?on?line?22 Warning:?require(/home/wwwroot/***/bootstrap/autoload.php):?failed?to?open?stream:?Operation? not?permitted?in?/home/wwwroot/***/public/index.php?on?line?22 Fatal?error:?require():?Failed?opening?required? '/home/wwwroot/***/public/../bootstrap/autoload.php'? (include_path='.:/usr/local/php/lib/php')?in?/home/wwwroot/***/public/index.php?on?line?22
此時你需要composer 更新第三方 vendor 組件
在項目目錄下執行composer update,請自行更新composer到最新版本。
如果在更新中出錯,請網上查找相應的composer錯誤,這個很好解決的。?
5 laravel從git上clone到線上目錄出現app_key的錯誤的話的,請在.env文件里加app_key。
//生成key,在項目根目錄下執行命令來獲取laravel項目app_key php?artisan?key:generate //或者可以修改配置文件.env中的APP_KEY參數APP_KEY=base64:akjIOLlieujKw0yEUbwjJdP5lPWHkk3uw39CnAhfdasfsaddfggghssda+
6 laravel上傳到線上出現The cipher and / or key Length are invalid 的
這個問題很多都是讀取.env的時候為NULL造成的。
首先你應該檢查config的app.php里是否有存在key與cipher的配置
'key' => env('APP_KEY'),' cipher' => 'AES-256-CBC',
有存在也要查找.env里是否有app_key。有存在的話,請操作:
php artisan config:cache
因為是env失效,所以接下來你要做的是清除緩存,重新來過,重要的一步就是要重新啟動nginx,php-fpm
7 Laravel 中 seeder 執行失敗
-
當第一次執行完 php artisan db:seed 后,增加新的 seeder 文件時執行會報錯。錯誤信息如下 [ReflectionException] class ***TableSeeder does not exist
-
確保新的 seeder 文件和全局 database seeder 是在同一個 seeder 目錄下了,仍然會出現這個問題的原因是: 我們需要清理下之前執行生成的 classmap 信息。
-
在控制臺中執行 composer dump-autoload,然后再執行 php artisan db:seed
部署到線上的經常會出現的,我遇到的就這么些問題,也許你會遇到更多的問題,或許你不會遇到問題。或許上面我遇到的問題能給予你一些幫助吧!