Move your app from one server to other without downtime (for reading)

Updated: 8th September 2024
Tags: ubuntu laravel

So, you want to move from one server to another? But it can take time, from few minutes to hours. Or if you are going to move to other hoster, you will need backup database and import which can take few hours.

So what to do if you don't wanna users to stair at black screen and search agents to fail to get pages?

If you use laravel you can block any no-get request with error that site is in read-only mode for few minutes/hours.

  1. Turn off your website / server
  2. Make mysqldump / snapshot of your website
  3. Turn on your website / server with middleware
  4. In app\Http\Kernel.php add BlockPostRequest middleware to all groups (web, api, etc).
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class BlockPostRequest
{
    /**
     * Handle an incoming request.
     *
     * @param  Request  $request
     * @param Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        if ($request->method() !== 'GET') {
            abort(400, "Website is in read-only mode for few minutes");
        }

        return $next($request);
    }
}

If you use other framework I guess there is something similar. Alternatively you can create new database user without write/update/delete rights and temporarily change configuration to use this readonly user.

Now you can move without panic as at least users can read your website, and in most cases (97%) visitors doesn't write anything.