How to upload a Laravel project to a VPS
In this tutorial we are going to be delving deeper in the nature of Laravel in order to show you how to manually upload your project to your Laravel VPS server. Alternatively, you can also use Laravel Forge in order to automate the process.
Transfer the project files to the server
Before configuring anything, you will first need to securely transfer all of your Laravel project files from your local environment to the cloud server. You can do this by using either SFTP or FTP. SFTP is the more secure option but you will also need to have root access to your server with either a root password or active SSH keys set up.
We recommend the SFTP option as it is far more secure and you probably already have SSH access to your server. Some popular clients that are widely used and support both SFTP and FTP include FileZilla and CyberDuck. Just make sure you have your credentials like the hostname, password and your server’s IP address as you will need them in order to connect through SFTP. You can always contact your provider if you don’t know any of the aforementioned data.
IMPORTANT: Make sure to keep the same project directory structure so that you can avoid any configuration issues!
Generate a new application key
Once you finish transferring the files, you will need to generate a new application key which consists of 32 randomized characters for your Laravel project. The key is used for encryption and authentication, which will protect your data from anyone that tries to access your information. This mechanism is particularly useful for protecting any data which will be collected, stored and utilized by your project. Generating a new key ensures that any data encrypted in your application is secured with a key that has not been exposed or potentially compromised in any other environment.
To generate a new key you will need to open up the terminal and go to the root directory of your Laravel project. Next, run the following command:
php artisan key:generate
This command automatically generates a new application key and updates the APP_KEY value in your project's .env file. After the key has been generated and the .env file updated, it is recommended to restart your Laravel application to ensure the new key will be used.
Install any dependencies
Next, you will also need to install all required project dependencies such as libraries and frameworks in order for your application to work fully. You can install everything by navigating to your project’s directory where your composer.json file is stored and using the following command:
composer install
To make sure everything is up to date, you can also run this command:
composer update
It is generally advisable to run this command in a staging environment and if everything works fine, then you can proceed with upgrading the production environment.
Further Laravel configurations
You can now finally proceed to configuring Laravel. You will need to set environment variables, application secrets, API keys and you will also need to establish a database connection. Additionally, you will also have to create a symbolic link between your project’s public folder and your server’s root directory, which will allow the server to actually access and serve static files from your application.
Connect to a database
In order for your project to work, you will need to connect to your database and set up a database user (and don’t forget to set the correct permissions) as well as the required tables. You will then need to migrate your data from your local environment to the new database. Depending on your local database, this could be accomplished in various ways, for example with the tools mysqldump and mysqlrestore for MySQL databases or pg_dump for PostgreSQL.