Search code examples
phpapachelaravellaravel-5.3

Laravel 5.3 not saving sessions via apache


I am getting a CSRF Token mismatch but that is not the underlying issue. When I add an exception to the page I get a 500 error.

I have found that this is being caused by a problem creating sessions.

When I serve everything works fine. When I put it into production it fails to create the sessions.

Has anyone experienced this issue before?

Error I am receiving:

1/1
TokenMismatchException in VerifyCsrfToken.php line 68:
in VerifyCsrfToken.php line 68
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Router.php line 655
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 629
at Router->dispatchToRoute(object(Request)) in Router.php line 607
at Router->dispatch(object(Request)) in Kernel.php line 268
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 150
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 117
at Kernel->handle(object(Request)) in index.php line 53

Then I add the login route to the CSRF exceptions and I get a 500 error.

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
        '/login/',
    ];
}

Google chrome error message:

This page isn’t working

'address' is currently unable to handle this request.
HTTP ERROR 500

Solution

  • Running CentOS 7.0 and SElinux was preventing the apache user from writing to storage. Even if you have chmod 777 set for this directory it will not allow you to write to the storage directory.

    The storage folder is where your sessions are stored so it throws a csrf token mismatch because it has nothing to compare to with session variables shut off.

    I want to stress that this is a configuration issue not a laravel issue. I searched far and wide on the internet and this is what finally solved my issue.

    setenforce 0
    

    This should allow writing, but you've turned off added security server-wide. That's bad. Turn SELinux back

    setenforce 1
    

    Then finally use SELinux to allow writing of the file by using this command from the project root directory.

    chcon -R -t httpd_sys_rw_content_t storage
    

    I want to thank everyone in the comments section your line of questioning got me on the right track to a solution!