Deploy a Laravel Application with Continuous Deployment on Koyeb
Edouard Bonlieu
Posted on January 10, 2022
Introduction
Laravel is a popular, simple and flexible PHP application framework that is ideal for modern, full-stack web apps.
Laravel provides a great developer experience with its many built-in features like dependency injections, an expressive database abstraction layer, queues and scheduled jobs, unit and integration testing, and more.
In this guide, we create a basic Laravel application, push it to a GitHub repository, and deploy it on the Koyeb Serverless Platform using git-driven deployment which allows you to push your code without having to take care of the build, deployment, and run process. Koyeb handles all this for you.
By deploying your Laravel application on Koyeb, you benefit from the platform's built-in autoscaling, autohealing, TLS encryption, global load balancing across our edge network, service discovery, and more.
If you want to learn how to Dockerize and Deploy a Laravel Application to Production, make sure to check out the linked guide.
Requirements
To follow and complete this guide successfully, you need:
- A Koyeb account to deploy and run the Laravel web application
- The Koyeb CLI installed to interact with Koyeb from the command line
- A GitHub account
- PHP and Composer installed on your machine
Steps
To deploy a Laravel web application on the Koyeb serverless platform, you need to follow these three steps:
- Create a new Laravel application
- Push the Laravel application to a GitHub repository
- Deploy the Laravel application on Koyeb
Create a new Laravel application
To get started, we will create a new Laravel project using Composer, a PHP package manager. In your terminal, run the following command:
composer create-project --prefer-dist laravel/laravel laravel-on-koyeb
This command will create a new Laravel application in the laravel-on-koyeb
directory and install all the dependencies required by the app to run properly.
You can launch the application in development mode by running the following command in your terminal from the laravel-on-koyeb
directory:
php artisan serve
Open a browser window and navigate to the http://localhost:8000
URL. You land on the Laravel welcome page.
Generate a key for Laravel's encryption services
Generate an APP_KEY to properly secure user sessions and other encrypted data by running:
php artisan key:generate --show
Save this value in a safe place. We will pass it to Koyeb as an ENV_VAR
when we deploy the Laravel app on the platform later.
Add a Procfile
A Procfile
tells Koyeb how to run your application. Create a Profile
in your application's root directory by executing:
echo "web: vendor/bin/heroku-php-apache2 public/" > Procfile
Push the Laravel application to a GitHub Repository
Now that we have created our Laravel application, we will create a git directory to store it. Return to your laravel-on-koyeb
directory and initialize a new git directory for your Laravel project:
git init
Add a .gitignore file
To keep only the necessary files in the repository, add this .gitignore file to exclude all unnecessary Laravel files.
curl https://raw.githubusercontent.com/github/gitignore/master/Laravel.gitignore > .gitignore
Execute the following command to add and commit the changes made in your files to your repository:
git add .
git commit -m "Laravel app initial commit"
Then add these changes to your remote GitHub repository for this app.
git remote add origin git@github.com:YOUR_GITHUB_USERNAME/laravel-on-koyeb.git
Rename the repository default branch to main executing:
git branch -M main
Next, push your changes to the GitHub repository by running:
git push -u origin main
You are now ready to deploy your application on Koyeb.
Deploy the Laravel application on Koyeb
Go to the Koyeb Control Panel and click the Create App button.
- On the App Creation form, select
GitHub
as your deployment method. - Under Repository, select your
laravel-on-koyeb
repository and specify the Branch. For this demo, putmain
. - Thanks to your Procfile, there is no need to specify a Run command. Since this is a basic app, you can use the default settings for container size, regions, horizontal scaling, and port.
- Add an environment variable Secret with the key as
APP_KEY
and the value as the value you generated in the step above. - Finally, give your App a name, i.e
laravel-on-koyeb
, and click Create App.
You land on the deployment page of your service where you can follow the progress of your Laravel application's deployment. Koyeb is building and deploying your application. Once the build and deployment are completed, you can access your application by clicking the App URL ending with koyeb.app
in the Koyeb control panel.
If you want to learn about how Koyeb automatically builds your Laravel applications from git, make sure to read our how we build from git documentation.
Your Laravel application is now running on Koyeb and benefits from native autoscaling, automatic HTTPS (SSL), auto-healing, and global load-balancing across our edge network.
Conclusion
In this guide, you learned how to deploy a Laravel application on Koyeb and benefit from its built-in continuous deployment pipeline. Each change you push to your repository will automatically trigger a new build and deployment on the Koyeb Serverless Platform.
Your changes then go live as soon as the deployment passes all necessary health checks. In case of a failure during one of your deployments, we ensure to keep the latest working deployment active so your application is always up and running.
If you have any questions or suggestions to improve this guide, feel free to reach out to us on Slack.
Posted on January 10, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
January 11, 2023