How to Implement HTTPS access for Laravel Web Application via Apache Webserver Reverse Proxy

Posted on

Introduction

This is an article where the main focus is to describe and to share an experience for implementing HTTP access where there is a web application using Laravel framework using it. Actually, running a Laravel-based application in a server can end in a certain error condition. In this context, the error appear when there is a change in the access protocol for accessing the Laravel-based application. The application cannot render the correct page for the web page. It is because changing the access protocol from HTTP to HTTPS has a deep impact to the Laravel-based application. All the static file is not available or pointing out the 404 error code upon checking it with the inspect tool from the web browser. Those static files include images, javascript files, etc. If the URL address in the beginning is using ‘http://www.mylaravelsites.com’ for an example, it will suddenly end in a failure if the URL change into ‘https://www.mylaravelsites.com’. The following image is the description of the scenario above :

How to Implement HTTPS access for Laravel Web Application via Apache Webserver Reverse Proxy
How to Implement HTTPS access for Laravel Web Application via Apache Webserver Reverse Proxy

Solution

So, how can a solution appear for the error above ?. The solution is very simple, the following are the steps for implementing the solution :

  1. Just access the server where the application is running.

  2. Following after, just go to the root folder of the Laravel-based web application. Before getting start, for an appropriate and exact solution for the problem, check the version of the Laravel-based web application first by typing the following command :

    [root@server Providers]# php artisan --version
    Laravel Framework 5.6.40
    [root@server Providers]#
    
  3. Actually, the solution is available by editing the ‘AppServiceProvider.php’ file. So, open the file with the name of ‘AppServiceProvider.php’. It exist in the following path :

    [root@server Providers]# pwd
    /var/www/html/laravel/app/Providers
    [root@server Providers]#
    

    The following is the actual content of the ‘AppServiceProvider.php’ file :

    <?php
    
    namespace App\Providers;
    use Illuminate\Support\ServiceProvider;
    
    class AppServiceProvider extends ServiceProvider
    {
          /**
            * Bootstrap any application services.
            *
            * @return void
            */
    
            public function boot()
            {
                   //
            }
    
          /**
            * Register any application services.
            *
            * @return void
            */
    
            public function register()
            {
                   //
            }
    }
    
  4. The solution is very simple. Just add the specific snippet code in that file. Precisely, insert the specific snippet code in the boot() function. After searching it through the search engine for a proper solution, there are several solution available. In this context, there are two snippet code which is possible to be the proper solution. Why is that ?, it is because the script for solving the error message depends on the version of the Laravel framework of the web application.

    Actually, the first alternative solution exist as follows :

    if(config('app.env') === 'production') {
             \URL::forceScheme('https');
    }
    

    Moreover, the second solution exist as follows :

    If (env('APP_ENV') !== 'production') {
           $this->app['request']->server->set('HTTPS', true);
    }
    
    
  5. But the compatible one for Laravel with the version of ‘5.6.’ is the latter or the second solution. Add it to the boot() function as follows :

    public function boot() { 
           // If (env('APP_ENV') !== 'production') { 
                  $this->app['request']->server->set('HTTPS', true);      
              } 
    }
    
  6. So, the content of the AppServiceProvider.php after inserting the above snippet code into the boot() function, it will have the following as follows :
    <?php
    namespace App\Providers;
    use Illuminate\Support\ServiceProvider;
    
    class AppServiceProvider extends ServiceProvider
    {
          /**
            * Bootstrap any application services.
            *
            * @return void
            */
    
            public function boot()
            {
                  //
                  If (env('APP_ENV') !== 'production') {
                          $this->app['request']->server->set('HTTPS', true);
                  }
            }
    
          /**
            * Register any application services.
            *
            * @return void
            */
    
            public function register()
            {
                  //
            }
    }
    
  7. Last but not least, just access the Laravel-based web page again once more. If there are no more errors appear, the Laravel-based web page will appear normally.

Leave a Reply