• Home
  • Laravel
  • Migrate Laravel Application: Tips and Best Practices

Migrate Laravel Application: Tips and Best Practices

Migrate Laravel application from one server to another can be daunting, but it doesn’t have to be. With the right tools and knowledge, it can be a straightforward process. In this tutorial, we’ll guide you through the entire process step by step, covering everything from preparing your new server to transferring your application files and updating your configuration files.

Prerequisites

Before you begin, you’ll need to prepare your new server for hosting Laravel application.

  1. Choose a web host or a VPS provider that meets your requirements.
  2. Install a web server (such as Apache or Nginx), PHP, and MySQL on your new server.
    • In this article, I’m not recommending any server. You may go with Apache or Nginx, whatever fits your requirements the most.
  3. Create a new database on your new server and grant the necessary permissions to your MySQL user.
  4. Install Composer on your new server. You can install it by running the following command –
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

After you have installed composer, it is time to start the process. Mainly we need to perform two tasks perfectly for a successful migration. First, bakup our application from the old server. Second, move the backup to the new server properly.

Backing up Laravel application requires us to export the application database and zip all the Laravel app files.

Migrate Laravel Application to New Server

1. Exporting App Database

On the old server, run the following command to export the application’s database –

mkdir /home/$USER/laravelBackup
cd /home/$USER/laravelBackup
mysqldump -u username -p laravel_database_name > database_backup.sql

Depending on the size of database, it may take a few seconds or minutes.

Once you’ve exported the app database, it’s time to backup your Laravel application files. Here are the steps you should take –

Compress your Laravel application files on your old server. You can do this by running the following command –

tar -czvf application.tar.gz /path/to/your/laravel/application

Once the compression processs has completed, we will compress the entire /home/$USER/laravelBackup file. We are doing it to keep our backup file clean and more understandable.

tar -czvf laravelBackup.tar.gz /home/$USER/laravelBackup

Finally can transfer our backup file to the new server. Use SCP or SFTP to transfer the compressed file to your new server. You can do this by running the following command from your old server –

scp laravelBackup.tar.gz username@new_server_ip:/path/to/destination

You can transfer the file using an SFTP client like FileZilla. Although SFTP clients are GUI-based and seem easier to use but requires us to set up FTP on the server. In case you’re using a VPS or dedicated server without cPanel, I recommend you use scp.

2. Import database on new server

Once the file has been transferred, extract it on your new server. Login to your new server through SSH and extract the files. You can do this by running the following command –

tar -xzvf laravelBackup.tar.gz -C /path/to/your/new/laravel/application
migrate laravel application extract laravel backup

It will also extract database_backup.sql file and application.tar.gz. Extract application.tar.gz (the actualy laravel files).

tar -xzvf application.tar.gz /server/root/directory

The second part of the process is to import the database on our new server. The following command will import your old database to new server.

mysql -u username -p newlaraveldatabase < database_backup.sql

Update Configuration Files

With your files transferred, it’s time to update your Laravel configuration files. Here are the steps you should take:

Navigate to your Laravel application directory on your new server –

cd /path/to/your/new/laravel/application

Update the .env file with your new server’s configuration settings. You’ll need to update the following values –

APP_URL=https://domain.com
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=newlaraveldatabase
DB_USERNAME=username
DB_PASSWORD=password

Generate a new application key by running the following command –

php artisan key:generate

Finally, run the following command to clear your application cache –

php artisan config:cache

Fix permissions

In order for the server to properly server content to users, we need to set correct permissions to laravel files and directories. Here is how to fix Laravel file permissions –

sudo chown -R www-data:www-data /path/to/your/laravel/root/directory

sudo find /path/to/your/laravel/root/directory -type f -exec chmod 644 {} \;

sudo find /path/to/your/laravel/root/directory -type d -exec chmod 755 {} \;
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

You’re now ready to test your Laravel application on your new server!

Conclusion

Migrating a Laravel application from one server to another can be a complex process, but it can be done easily with the right knowledge and tools. In this tutorial, we’ve covered all the necessary steps, from preparing your new server to transferring your files, updating your configuration files and setting correct Laravel file permissions. Following these steps, you can successfully migrate your Laravel application without any hassle.

If you have any issue with the steps, let me know in the comment section below. If you want my help making the transfer for you, you can contact me via Contact Us.

SHARE THIS POST

MassiveGRID Banner

Leave a Reply

Your email address will not be published. Required fields are marked *