Saturday, December 28, 2024
Google search engine
HomeLanguagesHow to upload Laravel App to Heroku Cloud Application Platform

How to upload Laravel App to Heroku Cloud Application Platform

Prerequisites :

  • Knowledge of PHP (Laravel)
  • A Heroku user account
  • A basic knowledge of Git version control

Setting up Heroku CLI: You can download Heroku CLI from here. We recommend you to view this article to install Heroku CLI.

Creating a Laravel App: In order to create a laravel app goto your command line and write the following command.

$ composer create-project laravel/laravel HerokuApp --prefer-dist

After running above command you’ll see something like this: We are assuming that you have composer installed in your machine. Otherwise you can download it from here. Initializing a Git repository: Heroku requires the app to be a git repository. Go to your root directory of app and enter the following command

$ git init

After initializing add the files to repository and commit changes as

$ git add .
$ git commit -m 'Initial commit'

After running above commands you’ll see something like this: Creating a Procfile: According to the documentation, By default, Heroku serves the files from the root directory. So in order to tell Heroku to serve the files from the public/ directory, you need to create a Procfile. Create a Procfile (there is no extension associated with this file)). Add the following content to the file:

web: vendor/bin/heroku-php-apache2 public/

It should look like this in VS Code: Now, add this file to repository and commit changes as:

$ git add .
$ git commit -m 'Added Procfile'

Deploying to Heroku: Now your app is ready, you can now create a new app on Heroku and push the changes to the server. Type the following command to create a new app from the command line.

$ heroku create

The above command will create a website with a wired name like calm-cliffs-42104.herokuapp.com. Now push the local changes to the server by the following command:

$ git push heroku master

You will see something like below: There is one more thing since your .env file has not been uploaded to the server, therefore, you need to set an environment variable to the Heroku app. You can do that by the following command:

$ heroku config:set APP_KEY={your-app-key}

After running the command you’ll see something like: You can set more config (environment) variables by this command or from the dashboard. Now, you can go to your dashboard and take a look at your website. You can also open your website by the following command:

$ heroku open

After running above command you’ll see laravel’s home page as in the image below: Note:

  • Now every time when you make any changes to your app you have to commit the changes and push it to Heroku as we have done in this tutorial.
  • You can change the name of your app by going to the settings of your app from the dashboard.

Reference: Heroku Documentation

RELATED ARTICLES

Most Popular

Recent Comments