If you have a Laravel application deployed on a server and you need to move it to another server connected to Moss, in this article we tell you how 😀

We assume the following names, passwords and IP addresses. Don't forget to use the ones that correspond to your case instead.

  • The site to move is mysite.com 
  • mysite.com is currently hosted on server src 
  • src has IP address 10.0.0.1 
  • src has a server user called user with permissions to read the files of mysite.com 
  • Laravel application mysite.com connects to database db with user db_user and password db_pass 
  • dst is the destination server for mysite.com and has IP address 10.0.0.2 
  • app is the server user that will run mysite.com in dst 
  • site is the name of the site in dst 
  • The name of your local machine is local 

Notes:

  • We assume that the code of mysite.com is hosted on a git repo.
  • We'll use scp to copy files from a server to another one using your local machine as intermediate storage. If you're used to working with an FTP/SFTP client, you may use it instead (remember that Moss sets up your new server and therefore you can upload files using SFTP but not FTP, due to security reasons).
  • We assume your previous database engine is either MySQL or MariaDB. If that's not the case, use the appropriate tools to back up and then restore your database.

Create your new server

If you haven't done it yet, log into Moss and create the new server.

To do this you need to be the admin of your organization. If you're a developer, tell your admin to add the server to a workspace where you belong.

Create the site in the new server

If you haven't done it yet, log into Moss and create your Laravel site.

  • The domain must be mysite.com 
  • Create server user app 
  • Create a connection to database db via user db_user with password db_password 
  • Fill out the remainder of the form according to your needs

Decrease the lifetime of DNS records

DNS records have a lifetime (TTL = Time To Live) that determines for how long a name resolution may be cached. To accelerate the migration process, we recommend that you decrease the TTL of the record that resolves mysite.com to address 10.0.0.1 down to the minimum allowed by your DNS provider. In this way, your users will access your site on the new server sooner when you update mysite.com to point to IP address 10.0.0.2.

In the figure you can see how to update the TTL in Cloudflare. Any other DNS provider will provide you with a similar panel where you'll be able to set the TTL. Just use the minimum value that your provider allows.

Once you apply the change, you should wait for as long as the previous TTL before going on. In this way you can be sure that previous cache entries with the old TTL expire and the new TTL is the one being used.

Copy your database

In this step we'll create a copy of your current database to restore it on the new server afterwards.

Get ready for the copy

In first place, log into your current server src via SSH:
ssh user@10.0.0.1 

Assuming that mysite.com is in production, your users might be using your application and updating your database. If this is not the case, skip this and jump into section Dump your database on your current server.

Usually, you won't want to lose data during the migration process, and therefore you should prevent your users from writing into your database after dumping its content. The most common ways to handle this are:

  1. Enable maintenance mode in your application. In such mode, your users will see a message stating that you're running some maintenance tasks. Enabling the maintenance mode in Laravel is really easy, just run php artisan down.
  2. Enable read-only mode in your application. This requires you to modify your application so that it rejects write operations but allows read operations. Hence your application will be partly available during the migration, but it's harder to implement.
  3. Stop your current web server (e.g. sudo service apache2 stop or sudo service nginx stop). Your users won't be able to access your server while it's down, so you should warn them in advance that it won't be available for some time due to planned maintenance.

Choose the option that better fits your use case and let's dump your database.

Dump your database from your current server

In general, you can dump your database in two ways:

  1. Using a traditional database management tool. In such case, check out the documentation of your favorite tool, e.g. phpMyAdmin or MySQL Workbench.
  2. Running commands from a shell. This is the option we detail in the following.

If you're logged into src via SSH, dump the content of the database your application uses into a compressed file .sql.gz and copy it into your local machine:

src$ mysqldump -u db_user -p --databases db_name | gzip > backup.sql.gz
src$ exit
local$ scp user@10.0.0.1:~/backup.sql.gz .

Restore your database on your new server

Now that you have a copy of your database in your own machine, you can restore it on your new server:

local$ scp backup.sql.gz app@10.0.0.2:~/
local$ ssh app@10.0.0.2
dst$ gunzip -c backup.sql.gz | mysql -u db_user -p
dst$ exit

Copy your 'storage' files

Laravel applications save every persistent file (e.g. logs and user-generated content) inside a directory named storage/. In order to complete the migration, you must copy the content therein into your new server.

local$ ssh user@10.0.0.1
src$ cd <path_to_site>/storage
src$ tar -czf storage.tgz .
src$ exit
local$ scp user@10.0.0.1:<path_to_site>/storage/storage.tgz .
local$ scp storage.tgz app@10.0.0.2:~/
local$ ssh app@10.0.0.2
dst$ cd sites/site/shared/storage/
dst$ tar zxvf ~/storage.tgz
dst$ exit

Deploy the site on the new server

  1. Log into Moss
  2. Head to your site, Deployments tab
  3. Add the git repository that holds the code of mysite.com 
  4. Fine-tune the deployment scripts in section Deployment Flow if needed
  5. Click Deploy

Check the application works on the new server

Before updating your DNS records, you must check that your web application is working fine on your new server. The easiest way to do this is to add a domain alias from Moss.

Once you're sure your application is working all right using the alternative domain name, it's time to update the DNS record of the main domain to put your site live.

Update your DNS records

Finally, update the records in your DNS provider so that mysite.com resolves to the IP address of your new server 10.0.0.2.

In the figure below you can see how to update a type A record in Cloudflare. Any other DNS provider will provide you with a similar panel where you'll be able to change your records.

Now just wait for old DNS cache entries to expire (2 minutes in this example) and your Laravel application will be available to all your users from the new server connected to Moss. Congrats! 👍

Did this answer your question?