Moving a production Laravel storage directory to a new server

Published on

In a nutshell, these are the steps to move a production Laravel application to a new server:

  • Put the old and new server in maintenance mode
  • Point the DNS to the new server
  • Export the database to the new server
  • Move the storage/app directory to the new server
  • Turn off maintenance mode on the new server

This post describes a good approach for moving the storage directory.

The variables in this post are dynamic. You can edit them below to match the values of your servers.

You should use an absolute path.
Should probably end in `/storage`.
You can't use "~" in this path.
You should use an absolute path.
Should probably end in `/storage`.
You can't use "~" in this path.

Problems with moving the storage directory

If your application doesn't store a lot of files, moving the directory isn't that difficult. You can use tar to create an archive, then use FTP to manually move the archive between servers.

In my experience, if the application does store a lot of files, two problems occur. First, tar has a tendency to appear to be stuck when creating big archives. Usually the archive was created successfully, but because it appears to be stuck, you can't be sure. Second, depending on your internet speed, using FTP to download and upload the archive can take a long time.

Moving the storage directory

This is my approach for moving the storage directory.

First, using tar without the risk of it appearing stuck:

cd "/home/old-user/project/storage"

# tar doesn't have an option to show progress, so we pipe it through `pv` instead.
#
# `pv` probably isn't installed by default
sudo apt install pv

tar -czf - app/ | pv > app.tar.gz

Now we have an app.tar.gz archive that we have to move to the new server. Instead of using your own internet connection and FTP, we make the new server download the file instead.

Add the public key of your new server to your old server. After that, download the file using SCP by running this on your new server:

scp -P 22 "old-user@old-server-ip:/home/old-user/project/storage/app.tar.gz" "/home/new-user/project/storage/app.tar.gz"

Instead of creating an archive, you could also pass the -r flag to SCP to copy the whole directory. If your storage directory contains a lot of small files, creating an archive is significantly faster.

Once the download is done, unpack the archive:

cd "/home/new-user/project/storage"

# Remove any existing files
rm -rf app/

tar -xf app.tar.gz

rm app.tar.gz

And that's it.

Deploy Laravel with GitHub Actions

Check out my Laravel deployment script for GitHub Actions

Check it out