Maison  >  Article  >  cadre php  >  Résumez certains problèmes auxquels il faut prêter attention lors du déploiement de projets Laravel en ligne

Résumez certains problèmes auxquels il faut prêter attention lors du déploiement de projets Laravel en ligne

藏色散人
藏色散人avant
2020-12-01 16:17:513829parcourir

下面由Laravel框架教程栏目给大家总结Laravel项目部署到线上需要注意的一些问题,希望对需要的朋友有所帮助!

Résumez certains problèmes auxquels il faut prêter attention lors du déploiement de projets Laravel en ligne

准备部署 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失效,所以接下来你要做的是清除缓存,重新来过,重要的一步就是要重新启动nginxphp-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

部署到线上的经常会出现的,我遇到的就这么些问题,也许你会遇到更多的问题,或许你不会遇到问题。或许上面我遇到的问题能给予你一些帮助吧!

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer