Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to update server files without downtime

I thought of this method to update server files without downtime:

  1. Upload new files to new folder e.g. to /www/v1/
  2. In httpd.conf, update DocumentRoot to point to new folder
  3. Restart apache.

Are there any downsides to this? Is there a better way?

Could there be any race condition errors when a file is being accessed at the exact moment of the restart?

like image 423
Code Avatar asked Oct 14 '25 18:10

Code


2 Answers

There is no single way that will always work - even if you prepare and deploy a new server and then change a load balancer to point to the new machine. Someone could have just requested a page from the original server and then be fetching parts of the page from the new copy of the site on a new machine. This is unlikely to cause a big problem however in practice.

On larger sites, with complex needs, things will be more difficult - and at a cost. Most sites have simpler requirements. Even database migrations can be planned to have minimal disruptions - but will also be complicated and have to be worked around with planned downtime.

Since switching between copies of the site on the same machine (or a small number of machines) is easy (effectively moving the Apache, or Nginx DocumentRoot) a technique that is easily available (and does not require multiple machines and a load balancer to achieve) is thus:

  1. While the site is still running happily in its current location on disk...
  2. Check out and prepare an entirely separate copy of the site in a new directory
  3. Run any required composer install or asset fetches/preparation as required
  4. Change a symlink between the old and new website.

In practice, this is what Capistrano (and other similar tools) does. The base directory is laid out like this:

base-directory
   releases
      20151010-0925/
          vendor/  (composer-installed files)
          web/index.php
      20151120-1007/
          vendor/  (composer-installed files)
          web/index.php
   current (symlink to ../releases/20151010-0925/)
   shared/ (files shared between releases)

When deploying a new version of the website - create a new fully-formed release and then when it is complete, change the 'current' symlink.

The Apache document root in this instance would point to .../base/current/web/ going through the symlink to the base of the website.

If, at any point the deployment fails to be ready you can simply abort it, and optionally delete the released version you were trying to deploy, and not alter the symlink.

I've used this technique from trivial, single page, HTML/CSS sites to three-machine clusters running thousands of concurrent users.

like image 184
Alister Bulman Avatar answered Oct 17 '25 09:10

Alister Bulman


The best solution would be to setup a load balancer for high availability (e.g. HAProxy) of your server.

source: laymance.com

Otherwise, you will always have a downtime. Think about the following cases: resetarting apache, database updates, assets updates, unexpected errors during update, etc.

like image 29
Kristian Vitozev Avatar answered Oct 17 '25 07:10

Kristian Vitozev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!