Summary of some issues that need to be paid attention to when deploying Laravel projects online

When preparing to deploy the Laravel application to the production environment, the following problems occur. You basically have no problems locally, but many problems appear online. I have sorted out some problems and bug s. I hope that when you deploy the laravel project, if there are similar problems, you can use it! If there are no problems in the deployment, it would be better.

First of all, when we do debugging, please enable php to display errors first, so that we can do debugging

vim /usr/local/php/etc/php.ini
 Revise display_errors = Off
 change to display_errors = On

Remember to restart the server after making changes.


1 Directory permission problem

In order to run Laravel, we need to configure permissions for some project directories.

Laravel projects need to give read and write permissions to the directories storage/, bootstrap/cache, public /

//Give three directories read and write permissions
chmod -R 777 bootstrap/
chmod -R 777 storage/
chmod -R 777 public/

If you use the one-click installation package lnmp, please note that the LNMP one-click installation package contains .user.ini, and the permission will be denied.

Need to use:

chattr -i /{Table of contents}/.user.ini

and remove:

rm .user.ini



2 Problems with Nginx configuration files

Assuming the path of your nginx.conf file is put here: /usr/local/nginx/conf/nginx.conf file, find the server{} field

The following code

#include enable-php.conf;

Does this file exist in your nginx, please comment, because this will cause a 500 error. The reason is:

Introduced php configuration, in which there is an error when try_files is turned on.

#Added support for laravel elegant links, described in the laravel documentation
location / {
    try_files $uri $uri/ /index.php?$query_string;
}

#Added support for php configuration
location ~ \.php$ {

#There cannot be the following try_files, otherwise an error of 500 will be reported
# try_files $uri /index.php =404;

fastcgi_split_path_info ^(.+\.php)(/.+)$;

#Note that this sentence is followed by .sock not 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;
}

Attachment: nginx configuration for a laravel

server
{
    listen 80;
    server_name website domain name;
    index index.php index.html index.htm default.html default.htm default.php;
    root  /var/www/html/act/public;   //The website storage directory, the entry file of laravel is in 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;
    #Just add the following
    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 extensions must remember to open

Before deploying the project, make sure that the extensions in php.ini have been enabled. The enabled extensions are: php_fileinfo, php_mbstring, php_openssl, these are all required by laravel.

Regardless of whether you have modified nginx or php.ini, after the modification, please remember to restart nginx and php-fpm.



4 The laravel project may be missing some core libraries when it is clone d on git and online. When the php error display is turned on, you will see problems similar to the following

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

At this point you need composer to update the third-party vendor components
Execute composer update in the project directory, and update composer to the latest version by yourself.

If there is an error in the update, please find the corresponding composer error online, this is a good solution.


5 If laravel has an error of app_key from git clone to online directory, please add app_key to the .env file.

//Generate the key and execute the command in the project root directory to get the laravel project app_key
php artisan key:generate

//Or you can modify the APP_KEY parameter APP_KEY=base64:akjIOLlieujKw0yEUbwjJdP5lPWHkk3uw39CnAhfdasfsaddfggghssda+ in the configuration file.env



6 The cipher and / or key length are invalid appears when laravel is uploaded online

Many of this problem is caused by null when reading .env.

First of all, you should check whether there are key and cipher configurations in app.php of config

'key'             => env('APP_KEY'),
'cipher'          => 'AES-256-CBC',

If it exists, also look for app_key in .env. If it exists, please do:

php artisan config:cache

Because the env is invalid, the next thing you have to do is to clear the cache and start over. The important step is to restart nginx, php-fpm

7 Laravel fails to execute seeder

  • When php artisan db:seed is executed for the first time, an error will be reported when adding a new seeder file. The error message is as follows [ReflectionException] Class ***TableSeeder does not exist

  • Make sure that the new seeder file and the global database seeder are in the same seeder directory, the reason why this problem still occurs is: we need to clean up the classmap information generated by the previous execution.

  • Execute composer dump-autoload in the console, then execute php artisan db:seed

Deploying online often occurs. I have encountered only these problems. Maybe you will encounter more problems, maybe you will not encounter problems. Maybe the above problems I encountered can give you some help!


A good memory is not as good as a bad pen, and you can't be lazy when learning php development. Taking notes is a good habit of learning!

Tags: Laravel

Posted by henka on Fri, 03 Jun 2022 22:22:25 +0530