Laravel app deployment guide
How to deploy a Laravel application with zero downtime
This guide will walk you through deploying a Laravel application to your server and shows what a simple but very effective workflow looks like.
Because of how Laravel applications work, deploying them using the SSH strategy is much preferred in comparison with FTP. Using FTP is possible, but for this guide we’ll assume you have a (shared) host or server with SSH access enabled.
Let's automate your deployment process for your Laravel projects!
Deploying with Launchdeck
Launchdeck is an automated code deployment tool that allows you to easily deploy Laravel to your server. You can write configuration files, define custom commands and deploy automatically whenever you push.
Our basic plan is free, includes all features, and allows you to deploy 1 project as often as you'd like!
Create a project
Let’s get started by creating a new project and linking your Git account. All repositories within your account will be retrieved and shown as a list, from which you can now select the one that you’d wish to use.
Setup your build
The next step allows us to define commands which will be executed in an isolated docker container - this is the build step. Hit the button “Auto suggest build automation” and we will automatically suggest a build config based on the contents of your repository - your Laravel project.
With a basic Laravel set-up Launchdeck will suggest the following build commands. Note that there also is a
output file specified. This will be used to cache the
node_modules folder and prevent you from installing the dependencies from scratch with every deployment. This will save you a ton of time!
build: - composer install --ignore-platform-reqs --optimize-autoloader --no-progress - npm install - npm run production
All files generated using these build commands will be uploaded to your server, except for the directories and files specified under
purge. This is a list of globs to be “purged” after the command runner stage has completed. In this case, Launchdeck deletes the directories specified before sending the build off to your remote server.
purge: - resources/js - storage/framework/sessions/ - storage/app/ - storage/logs/ - node_modules
With our Laravel project there also be some files and folders that need to be persisted throughout builds, think of your Laravel cache and sessions.
shared: - storage/framework/sessions/ - storage/app/ - storage/logs/
You’ll find more information on how to set up and use build commands on our docs page about build automation.
Configure a server
Now let’s tell Launchdeck where to upload your Laravel application. In this example, we’ll upload the Laravel files to your server using SSH.
A major advantage of SSH is that it allows for commands to be executed on your server, including the creation of symlinks. This enabled Launchdeck to publish new versions of your project without any downtime at all. The way this works is by adding an entirely new directory for each new version, to which a symlink from your server’s webroot will be created or updated once you wish to publish this new version. Find out more on how zero downtime atomic deployments work.
For security, we recommend creating a separate user to use for deployments.
sudo adduser launchdeck sudo usermod -a -G www-data launchdeck
How to configure your server can be found in our SSH server configuration docs.
Make sure the 'Webroot path on repository' field is filled in with the value 'public', so this path within the repository would be linked to the webroot on your remote server.
To figure out whether the server connection data is correct, run the server connection test and we’ll try to connect to your server and check the provided path(s).
Setup SSH commands
In addition to the build commands, we also need a number of Laravel commands that are executed on your server after every deployment.
Within the server configuration modal you will find the SSH commands editor. In the 'After publish' tab you can add the following commands, one command per line.
php artisan migrate --force
php artisan config:cache
--force flag is simply used to bypass the confirmation prompt.
Add a config file
The last step in the server configuration modal is adding the configuration file, which contains sensitive data such as the database credentials. You want this file to be uploaded during deployment, but not tracked in version control.
In your Laravel project this is the
.env file. Create the file in the config file editor and copy the contents from the
.env.example and paste it into the config file content field. Adjust the values and then close the server configuration modal by pressing the 'Save' button.
Deploy for the first time
We’re now ready to start our first deployment. Click ‘create new project’ to confirm the settings and add the project. In the project overview, you can now start the build. The code will be cloned from your repository and prepared for you to publish it. Hit ‘Publish’ and your Laravel app is live!
If we then commit changes to our repository, the commit will automatically appear in your project overview where you’ll be able to upload and publish your new version with the push of a button. If anything goes wrong, you can simply roll back to the latest working version.
That’s all! By using Launchdeck to deploy your Laravel application, you’ll never have to upload a new version or run commands manually. Just hit “publish”, and Launchdeck will deploy your app with zero downtime.