您好,登錄后才能下訂單哦!
小編給大家分享一下Laravel項(xiàng)目部署到線上需要注意的問題有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
準(zhǔn)備部署 Laravel 應(yīng)用到生產(chǎn)環(huán)境時,卻出現(xiàn)了以下一些問題,你在本地上基本不會出現(xiàn)問題,但是到線上很多問題都出來了。整理了一些問題與bug,希望在你部署laravel項(xiàng)目的時候,如果出現(xiàn)類似問題,可以用得到吧! 部署不出現(xiàn)任何問題,那就再再好不過了。
首先,我們再做調(diào)試的時候,請先開啟php顯示錯誤,以便做調(diào)試
vim /usr/local/php/etc/php.ini 修改 display_errors = Off 改為 display_errors = On
改完后記得要重啟服務(wù)器。
1 目錄權(quán)限問題
為了運(yùn)行 Laravel
,我們需要為一些項(xiàng)目目錄配置權(quán)限.
Laravel 項(xiàng)目需要對目錄 storage/
, bootstrap/cache
, 賦予讀寫權(quán)限
//賦予三個目錄讀寫權(quán)限chmod -R 777 bootstrap/chmod -R 777 storage/
如果你用的是一鍵安裝包lnmp,請注意,LNMP 一鍵安裝包中含有.user.ini
,權(quán)限會被拒絕。
需使用:
chattr -i /{目錄}/.user.ini
并刪除:
rm .user.ini
?
2 Nginx的配置文件的問題
假設(shè)你的nginx.conf文件的路徑是放在這里:/usr/local/nginx/conf/nginx.conf
文件,找到 server{}字段中
如下代碼
#include enable-php.conf;
你的nginx里存不存在這個文件,請注釋,因?yàn)檫@個會導(dǎo)致500錯誤。原因是:
引入了 php 配置,其中有句 try_files 開啟就有報錯.
#新增 支持laravel 優(yōu)雅鏈接,在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..1fastcgi_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 網(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ò)展要記得開啟
部署項(xiàng)目之前要先確保php.ini里的擴(kuò)展已經(jīng)開啟,開啟的擴(kuò)展有:php_fileinfo
, php_mbstring
, php_openssl
,這幾個都是laravel需要的。
不管是修改了nginx還是php.ini,修改完后,請記得要重啟nginx與php-fpm。
4 laravel項(xiàng)目在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 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
此時你需要composer
更新第三方 vendor
組件
在項(xiàng)目目錄下執(zhí)行composer update
,請自行更新composer
到最新版本。
如果在更新中出錯,請網(wǎng)上查找相應(yīng)的composer
錯誤,這個很好解決的。
5 laravel從git上clone到線上目錄出現(xiàn)app_key的錯誤的話的,請?jiān)?env文件里加app_key。
//生成key,在項(xiàng)目根目錄下執(zhí)行命令來獲取laravel項(xiàng)目app_key php artisan key:generate //或者可以修改配置文件.env中的APP_KEY參數(shù) APP_KEY=base64:akjIOLlieujKw0yEUbwjJdP5lPWHkk3uw39CnAhfdasfsaddfggghssda+
6 laravel上傳到線上出現(xiàn)The cipher and / or key length are invalid 的
這個問題很多都是讀取.env的時候?yàn)閚ull造成的。
首先你應(yīng)該檢查config
的app.php
里是否有存在key
與cipher
的配置
'key' => env('APP_KEY'), 'cipher' => 'AES-256-CBC',
有存在也要查找.env
里是否有app_key
。有存在的話,請操作:
php artisan config:cache
因?yàn)槭莈nv失效,所以接下來你要做的是清除緩存,重新來過,重要的一步就是要重新啟動nginx,php-fpm
7 Laravel 中 seeder 執(zhí)行失敗
當(dāng)?shù)谝淮螆?zhí)行完 php artisan db:seed
后,增加新的 seeder 文件時執(zhí)行會報錯。錯誤信息如下 [ReflectionException] Class ***TableSeeder does not exist
確保新的 seeder 文件和全局 database seeder
是在同一個 seeder
目錄下了,仍然會出現(xiàn)這個問題的原因是: 我們需要清理下之前執(zhí)行生成的 classmap 信息。
在控制臺中執(zhí)行 composer dump-autoload
,然后再執(zhí)行 php artisan db:seed
部署到線上的經(jīng)常會出現(xiàn)的,我遇到的就這么些問題,也許你會遇到更多的問題,或許你不會遇到問題?;蛟S上面我遇到的問題能給予你一些幫助吧!
以上是“Laravel項(xiàng)目部署到線上需要注意的問題有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。