Blog header image

Transferring a Laravel application from one host to another using Digital Ocean and Laravel Forge

Posted on Aug 14th, 2017

Currently, I have 3 [Digital Ocean](https://digitalocean.com) droplets (servers) and really only need 2. So I need to move some [Laravel](https://laravel.com) apps from one droplet to another. Moving a Laravel application can be a breeze if you use the right tools. One of my favorite tools for setting up Larave/PHP projects is [Laravel Forge](https://laravel.com). Laravel Forge takes care of building your server on hosts like DO, Linode, & AWS for Laravel/PHP applications. It includes installs of GIT, composer and of course PHP 7+. A couple other tools that I use frequently is FTP & MySQL clients, [Transmit](https://panic.com/transmit/) and [SequelPro](http://www.sequelpro.com) respectively. When transferring websites, I have these clients connected to both the original and the destination server. Below is an overview of the process I go through for transferring a single application. This outline assumes the client isn't making DB/content updates during the transfer process. The entire process can take as little as 10 minutes but this definitely varies depending on the size of project. * Lower the TTL on the domain (~1 minutes) * Add the new domain on the destination server in Laravel Forge (~1 minute) * Clone the master branch from my project's repository (~1-3 minutes depending on internet speeds and project size) * Set local environment settings in .env (~3 minutes) * Create a new database and then import data (~1-5 minutes depending on size of DB and internet speeds) * Transfer non-tracked assets such as user uploaded photos/files (totally dependent on quantity/size of files) * Deploy and run composer install, migrations, etc (~2-5 minutes) * Update DNS settings to point to new server (~1 minute) ## Lower the TTL The very first thing is to make sure we lower the TTL on the domain. This will reduce the domain setting's propagation time and provide the ability to "quickly" switch back to our orignal server if we run into issues.
## Add domain First, I'll add the domain to the destination server in Laravel Forge. ## Install Repository After adding the domain, I'll select it to start the setup process. Then, I'll choose repository and set this domain to install from the project's repository located on Bitbucket. Since this is my production server, I'll have it pull the master branch. I usually don't select the "Install Composer Dependencies" and prefer to perform this task after setting up the DB and .env. ## Set .env Once I've installed the repository, I'll set the .env settings for this production environment. The most basic updates include DB credentials and maybe mail settings. As for the "APP_KEY", I generally wait until the project is deployed and then use Laravel's Artisan to set it. ``` php artisan key:generate ``` ## Import untracked assets and data Using Sequel Pro, I make the new database on the destination server. Then export a fresh copy of the DB from the original source and import it. Then using Transmit, I will transfer all user uploads/assets such as images and files. For most of my projects, this is a folder located in "/storage/app". These are usually not tracked in version control and should be moved manually. Alternatively, you can transfer between your DO droplets using scp. I would definitely recommend this method if you have a large quantity of assets to move. ## Deploy and update DNS By default, Forge provides the following deploy script. ``` cd /home/forge/philmareu.com git pull origin master echo "" | sudo -S service php7.0-fpm reload ``` This is great, however I'll need to pull all the vendor libraries and dependencies using composer. Plus, I'm good with running composer install and migrations automatically on deployment. So I'll add the composer install and migration to the deploy script. ``` cd /home/forge/philmareu.com git pull origin master echo "" | sudo -S service php7.0-fpm reload composer install --no-interaction --no-dev --prefer-dist php artisan migrate --force ``` Perfect. After clicking "Deploy", there should be no errors and the vendor folder should appear in the project. Finally, I'll update the DNS settings to point to the new location and then wait for settings to propagate and check the website and make sure it loads successfully.