下面由laravel教程欄目給大家介紹Laravel項(xiàng)目部署到線(xiàn)上需要注意的問(wèn)題? ,希望對(duì)需要的朋友有所幫助!
準(zhǔn)備部署 laravel 應(yīng)用到生產(chǎn)環(huán)境時(shí),卻出現(xiàn)了以下一些問(wèn)題,你在本地上基本不會(huì)出現(xiàn)問(wèn)題,但是到線(xiàn)上很多問(wèn)題都出來(lái)了。整理了一些問(wèn)題與bug,希望在你部署laravel項(xiàng)目的時(shí)候,如果出現(xiàn)類(lèi)似問(wèn)題,可以用得到吧! 部署不出現(xiàn)任何問(wèn)題,那就再再好不過(guò)了。
首先,我們?cè)僮稣{(diào)試的時(shí)候,請(qǐng)先開(kāi)啟php顯示錯(cuò)誤,以便做調(diào)試
vim /usr/local/php/etc/php.ini 修改 display_errors = Off 改為 display_errors = On
改完后記得要重啟服務(wù)器。
1 目錄權(quán)限問(wèn)題
為了運(yùn)行 Laravel,我們需要為一些項(xiàng)目目錄配置權(quán)限.
Laravel 項(xiàng)目需要對(duì)目錄 storage/, bootstrap/cache, ?賦予讀寫(xiě)權(quán)限
//賦予三個(gè)目錄讀寫(xiě)權(quán)限chmod -R 777 bootstrap/chmod -R 777 storage/
如果你用的是一鍵安裝包lnmp,請(qǐng)注意,LNMP 一鍵安裝包中含有.user.ini,權(quán)限會(huì)被拒絕。
需使用:
chattr -i /{目錄}/.user.ini
并刪除:
rm .user.ini
?
2 nginx的配置文件的問(wèn)題
假設(shè)你的nginx.conf文件的路徑是放在這里:/usr/local/nginx/conf/nginx.conf文件,找到 server{}字段中
如下代碼
#include enable-php.conf;
你的nginx里存不存在這個(gè)文件,請(qǐng)注釋?zhuān)驗(yàn)檫@個(gè)會(huì)導(dǎo)致500錯(cuò)誤。原因是:
引入了 php 配置,其中有句 try_files 開(kāi)啟就有報(bào)錯(cuò).
#新增 支持laravel 優(yōu)雅鏈接,在laravel 文檔里有說(shuō)明 location / { try_files $uri $uri/ /index.php?$query_string;}#新增 支持php 的配置 location ~ .php$ {#不能有下面這句 try_files ,不然報(bào)錯(cuò)500# try_files $uri /index.php =404;fastcgi_split_path_info ^(.+.php)(/.+)$;#這句注意 后面是.sock 不是127.0.0..1fastcgi_pass unix:/tmp/php-cgi.sock;fastcgi_index index.php;include fastcgi_params;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;}
附件:給一個(gè)laravel的nginx配置
server{ listen 80; server_name 網(wǎng)站域名; index index.php index.html index.htm default.html default.htm default.php; root /var/www/html/act/public; //網(wǎng)站存放目錄,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擴(kuò)展要記得開(kāi)啟
部署項(xiàng)目之前要先確保php.ini里的擴(kuò)展已經(jīng)開(kāi)啟,開(kāi)啟的擴(kuò)展有:php_fileinfo, php_mbstring, php_openssl,這幾個(gè)都是laravel需要的。
不管是修改了nginx還是php.ini,修改完后,請(qǐng)記得要重啟nginx與php-fpm。
4 laravel項(xiàng)目在git上clone到線(xiàn)上可能會(huì)缺少一下核心庫(kù),開(kāi)啟php錯(cuò)誤顯示會(huì)看到類(lèi)似以下的問(wèn)題
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 22Warning: require(/home/wwwroot/***/bootstrap/autoload.php): failed to open stream: Operation not permitted in /home/wwwroot/***/public/index.php on line 22Fatal 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
此時(shí)你需要composer 更新第三方 vendor 組件
在項(xiàng)目目錄下執(zhí)行composer update,請(qǐng)自行更新composer到最新版本。
如果在更新中出錯(cuò),請(qǐng)網(wǎng)上查找相應(yīng)的composer錯(cuò)誤,這個(gè)很好解決的。
5 laravel從git上clone到線(xiàn)上目錄出現(xiàn)app_key的錯(cuò)誤的話(huà)的,請(qǐng)?jiān)?env文件里加app_key。
//生成key,在項(xiàng)目根目錄下執(zhí)行命令來(lái)獲取laravel項(xiàng)目app_key php artisan key:generate //或者可以修改配置文件.env中的APP_KEY參數(shù) APP_KEY=base64:akjIOLlieujKw0yEUbwjJdP5lPWHkk3uw39CnAhfdasfsaddfggghssda+
6 laravel上傳到線(xiàn)上出現(xiàn)The cipher and / or key Length are invalid 的
這個(gè)問(wèn)題很多都是讀取.env的時(shí)候?yàn)?a href="http://m.babyishan.com/tag/null">NULL造成的。
首先你應(yīng)該檢查config的app.php里是否有存在key與cipher的配置
'key' => env('APP_KEY'), 'cipher' => 'AES-256-CBC',
有存在也要查找.env里是否有app_key。有存在的話(huà),請(qǐng)操作:
php artisan config:cache
因?yàn)槭莈nv失效,所以接下來(lái)你要做的是清除緩存,重新來(lái)過(guò),重要的一步就是要重新啟動(dòng)nginx,php-fpm
7 Laravel 中 seeder 執(zhí)行失敗
-
當(dāng)?shù)谝淮螆?zhí)行完 php artisan db:seed 后,增加新的 seeder 文件時(shí)執(zhí)行會(huì)報(bào)錯(cuò)。錯(cuò)誤信息如下 [ReflectionException] class ***TableSeeder does not exist
-
確保新的 seeder 文件和全局 database seeder 是在同一個(gè) seeder 目錄下了,仍然會(huì)出現(xiàn)這個(gè)問(wèn)題的原因是: 我們需要清理下之前執(zhí)行生成的 classmap 信息。
-
在控制臺(tái)中執(zhí)行 composer dump-autoload,然后再執(zhí)行 php artisan db:seed
部署到線(xiàn)上的經(jīng)常會(huì)出現(xiàn)的,我遇到的就這么些問(wèn)題,也許你會(huì)遇到更多的問(wèn)題,或許你不會(huì)遇到問(wèn)題。或許上面我遇到的問(wèn)題能給予你一些幫助吧!