If you have a Laravel application deployed on a server and you need to move it to another server managed by 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 user user with permissions to read the files of mysite.com 
  • Laravel application mysite.com uses a database named 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
  • dev is the user that will run mysite.com on server 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 database engine is either MySQL or MariaDB. If that's not the case, contact us via our support chat.

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 website in the new server

If you haven't done it yet, log into Moss and create your Laravel application. Ignore the section Deploy your web app, we'll do that later.

  • The root domain must be mysite.com 
  • The git repository must be the one with the code of mysite.com
  • Choose 'MySQL' as the database engine, db as the database name, db_user as the database user, and db_pass as the password (you may change the database password if you want)
  • 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 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 dev@10.0.0.2:~/
local$ ssh dev@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 dev@10.0.0.2:~/
local$ ssh dev@10.0.0.2
dst$ cd sites/site.com/shared/storage/
dst$ tar zxvf ~/storage.tgz
dst$ exit

Deploy the website on the new server

Log into your Moss account again and deploy the application on your new server.

Check the application works on the new server

Before updating your DNS records, you must check that your web applications is working fine on your new server.

We'll test that by making mysite.com resolve to the IP address of the new server (10.0.0.2) in your local machine. To achieve this you must add the following line to your hosts file:

10.0.0.2 mysite.com

Such file is /etc/hosts on Linux and Mac OS X, and c:\Windows\System32\Drivers\etc\hosts on Windows. This article from Rackspace explains this file in greater detail.

Once you've saved the file, open your favorite browser and visit mysite.com.

In case you chose the Let's Encrypt certificate option when you created your site with Moss, your browser will warn you that the certificate isn't valid. This is expected, since DNS records keep pointing to your old server and Let's Encrypt cannot validate the authenticity of the certificate request. Just ignore the warning and check that your application is working.

Test your application thoroughly to guarantee that it's running as expected. Once you're satisfied, delete the line you added to your hosts file and let's put you website live by updating the DNS 🤗 

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 managed by Moss. Congrats! 👍

Did this answer your question?