Laravel Deployment

We are going to discuss basic changes in our template before deploying it.


Introduction

Laravel deployment differs from server to server and also if you deploy on root domain or subdomains. There are no special steps of deployment specifically for Sneat, it's the same for all the projects built with Laravel.


Root Folder Deployment

There should be some minor changes when you deploy your project on root folder or sub folder.

  1. First you need to change your .env variables value:
  2. APP_ENV=production
    APP_DEBUG=false
  3. Now, run the following command to generate the production build for deployment.
  4. yarn build
  5. Our template is ready to deploy. Here, we assume a simple folder structure to deploy the laravel app on the server. It may differ from server to server..
  6. We have two folders on the server.

    • public_html
    • laravel

    We move all public files inside the public_html folder, and the rest of our application resides in the laravel folder.

    πŸ“‚ public_html/ -> (folder for public accessible)
    β”œβ”€β”€πŸ“‚ assets
    β”‚ Β  β”œβ”€β”€ πŸ“‚ audio
    β”‚ Β  β”œβ”€β”€ πŸ“‚ img
    β”‚ Β  β”œβ”€β”€ πŸ“‚ json
    β”‚ Β  β”œβ”€β”€ πŸ“‚ svg
    β”œβ”€β”€πŸ“‚ build
    β”‚ Β  β”œβ”€β”€ πŸ“‚ assets
    β”‚ Β  β”œβ”€β”€ πŸ“„ index.html
    β”‚ Β  β”œβ”€β”€ πŸ“„ manifest.json
    β”œβ”€β”€ πŸ“„ .htaccess
    β”œβ”€β”€ πŸ“„ favicon.ico
    β”œβ”€β”€ πŸ“„ index.php
    └── πŸ“„ robots.txt
    
    πŸ“‚laravel/ -> (folder where laravel live)
    β”œβ”€β”€ πŸ“‚ app
    β”œβ”€β”€ πŸ“‚ bootstrap
    β”œβ”€β”€ πŸ“‚ config
    β”œβ”€β”€ πŸ“‚ database
    β”œβ”€β”€ πŸ“‚ lang
    β”œβ”€β”€ πŸ“‚ resources
    β”œβ”€β”€ πŸ“‚ routes
    β”œβ”€β”€ πŸ“‚ storage
    β”œβ”€β”€ πŸ“‚ tests
    β”œβ”€β”€ πŸ“‚ vendor
    β”œβ”€β”€ πŸ“„ .editorconfig
    β”œβ”€β”€ πŸ“„ .env
    β”œβ”€β”€ πŸ“„ .gitattributes
    β”œβ”€β”€ πŸ“„ .gitignore
    β”œβ”€β”€ πŸ“„ .styleci.yml
    β”œβ”€β”€ πŸ“„ artisan
    β”œβ”€β”€ πŸ“„ composer.json
    β”œβ”€β”€ πŸ“„ package.json
    β”œβ”€β”€ πŸ“„ phpunit.xml
    └── πŸ“„ vite.config.js
  7. As per the above structure, we have to change some paths in the index.php file in the public_html folder. Also, we have to bind the document root to the current file path, where index.php exists.
  8. <?php
    
    use Illuminate\Contracts\Http\Kernel;
    use Illuminate\Http\Request;
    
    define('LARAVEL_START', microtime(true));
    
    /*
    |--------------------------------------------------------------------------
    | Check If The Application Is Under Maintenance
    |--------------------------------------------------------------------------
    |
    | If the application is in maintenance / demo mode via the "down" command
    | we will load this file so that any pre-rendered content can be shown
    | instead of starting the framework, which could cause an exception.
    |
    */
    
    if (file_exists($maintenance = __DIR__.'/../laravel/storage/framework/maintenance.php')) {
        require $maintenance;
    }
    
    /*
    |--------------------------------------------------------------------------
    | Register The Auto Loader
    |--------------------------------------------------------------------------
    |
    | Composer provides a convenient, automatically generated class loader for
    | this application. We just need to utilize it! We'll simply require it
    | into the script here so we don't need to manually load our classes.
    |
    */
    
    require __DIR__.'/../laravel/vendor/autoload.php';
    
    /*
    |--------------------------------------------------------------------------
    | Run The Application
    |--------------------------------------------------------------------------
    |
    | Once we have the application, we can handle the incoming request using
    | the application's HTTP kernel. Then, we will send the response back
    | to this client's browser, allowing them to enjoy our application.
    |
    */
    
    $app = require_once __DIR__.'/../laravel/bootstrap/app.php';
    
    //--- binding your app ---//
    $app->bind('path.public', function() {
      return base_path('/../public_html/demo/');
    });
    
    $kernel = $app->make(Kernel::class);
    
    $response = $kernel->handle(
        $request = Request::capture()
    )->send();
    
    $kernel->terminate($request, $response);
  9. Congratulation! You have successfully deployed your laravel app on the server. πŸ₯³

Sub Folder Deployment

Mainly, subfolder deployment means deploying your project inside some nested folder on server. For example: Public_html -> your_folder -> another_folder -> your_project_goes_here

  1. Follow step 1 and step 2 of the above root deployment steps.
  2. Change the file path as per your sub folder structure in index.php file.
  3. <?php
    
    use Illuminate\Contracts\Http\Kernel;
    use Illuminate\Http\Request;
    
    define('LARAVEL_START', microtime(true));
    
    /*
    |--------------------------------------------------------------------------
    | Check If The Application Is Under Maintenance
    |--------------------------------------------------------------------------
    |
    | If the application is in maintenance / demo mode via the "down" command
    | we will load this file so that any pre-rendered content can be shown
    | instead of starting the framework, which could cause an exception.
    |
    */
    
    if (file_exists($maintenance = __DIR__.'/../../laravel/storage/framework/maintenance.php')) {
        require $maintenance;
    }
    
    /*
    |--------------------------------------------------------------------------
    | Register The Auto Loader
    |--------------------------------------------------------------------------
    |
    | Composer provides a convenient, automatically generated class loader for
    | this application. We just need to utilize it! We'll simply require it
    | into the script here so we don't need to manually load our classes.
    |
    */
    
    require __DIR__.'/../../laravel/vendor/autoload.php';
    
    /*
    |--------------------------------------------------------------------------
    | Run The Application
    |--------------------------------------------------------------------------
    |
    | Once we have the application, we can handle the incoming request using
    | the application's HTTP kernel. Then, we will send the response back
    | to this client's browser, allowing them to enjoy our application.
    |
    */
    
    $app = require_once __DIR__.'/../../laravel/bootstrap/app.php';
    
    //--- binding your app ---//
    $app->bind('path.public', function() {
      return base_path('/../public_html/demo/');
    });
    
    $kernel = $app->make(Kernel::class);
    
    $response = $kernel->handle(
        $request = Request::capture()
    )->send();
    
    $kernel->terminate($request, $response);
  4. Congratulation! You have successfully deployed your laravel app on the sub folder. πŸ₯³
© 2017- ThemeSelection, Hand-crafted & Made with ❀️