Deploy Laravel Projects With Database For Free!

Good day everyone đź‘‹

In this article i’m going to explain how to host and deploy your Laravel project free on Heroku step by step, and it will work.

Let’s get to it?

0- I’m using:

Laravel 8.8
PHP 8
Windows Terminal
GitHub
Git C…


This content originally appeared on DEV Community and was authored by Mostafa Said

Good day everyone đź‘‹

In this article i'm going to explain how to host and deploy your Laravel project free on Heroku step by step, and it will work.

Let's get to it?

0- I'm using:

  • Laravel 8.8
  • PHP 8
  • Windows Terminal
  • GitHub
  • Git Commands
  • Heroku Toolbelt

1- Install Nodejs and npm:

  • First thing you need to make sure that you have Node and npm installed on your machine.
  • You can check that by opening up the terminal and type those two commands:

npm -v
node -v

PS C:\Users\Moose> node -v
v16.13.2
PS C:\Users\Moose> npm -v
8.4.0

2- Start your Laravel Project:

  • If you already have a project, you can skip this step 👇.
  • I use composer to create Laravel projects.
  • You can download composer from here: https://getcomposer.org/
  • Open up your terminal and create your Laravel project in the desired directory.
  • I already have Laravel project created but here is the terminal command to create a new project via composer.
  • Instead of 'example-app' just name your project.
PS C:\Users\Moose> composer create-project laravel/laravel example-app

3- Create Account and Download Heroku Toolbelt:

  • Sign up for a free account on Heroku from HERE. If you already have an account, move on to next step 👇
  • For the sake of keeping everything simple and smooth, download Heroku Toolbelt for free from đź”— HERE.
  • Just keep pressing next and leave everything to default.

4- Procfile Creation:

  • Now we need to add a new file to our project in the main directory.
  • Head to your project folder and create new file and name it Procfile without extension and insure that first letter is in uppercase.
  • Inside the file, leave the below code and then save.
web: vendor/bin/heroku-php-apache2 public/
  • Just like this 👇 create heroku procfile in laravel project main directory

5- New to GitHub? :

  • I'm sure you have GitHub account but if you don't, you need to create one and connect to it.
  • Since this is not GitHub tutorial, please visit the following LINK for a complete guide how to create GitHub account.
  • Also if you're new to Git, don't forget to setup your username through terminal. Visit this LINK to know how.

6- Git:

  • You can skip this if you already have a Laravel project and it is on GitHub. In this case all you need to do is to push the changes with Procfile to GitHub normally.
  • Assuming you're creating a new repo for your project. Open you terminal in the project directory and type the below commands in the same order one by one.
  • Please note that in my case, PS F:\Projects\Zarafah> is my project main directory path.
PS F:\Projects\Zarafah> git init
PS F:\Projects\Zarafah> git add .
PS F:\Projects\Zarafah> git commit -m "initial release"

7- Create Heroku App:

Please follow carefully for best results.

  • Open you terminal in the project directory and type the below commands in the same order one by one.
  • In command number 4, you'll find that i'm renaming my app to 'zarafah'. You can name it whatever you want but it is recommended to name it exactly the same name on github.
PS F:\Projects\Zarafah> heroku create
PS F:\Projects\Zarafah> git remote rm heroku
PS F:\Projects\Zarafah> heroku create
PS F:\Projects\Zarafah> heroku rename zarafah
  • Time to push this project to heroku.
PS F:\Projects\Zarafah> git push heroku master

8- Setting Up Heroku App:

  • Now, if we try to open app through Heroku it will get us error 500.
  • Open up Heroku Dashboard and follow the below path:

Select your project - Select settings - Scroll down and click on 'Reveal Config Vars' - You will find one input for key and other for value.

Heroku Settings

  • Head to your project main folder and open your .env file.
  • Look for the below. ENV file
  • Copy 'APP_DEBUG' and insert it in key input and copy 'true' and insert it in value input.
  • Do the same for 'APP_KEY' and it's value.

9- Adding to Heroku Config:

  • Have your .env file open.
  • Open the terminal in your project directory and hit the below commands.
  • In the terminal we will write heroku config:add (key value) and then we add a space and give the key and value.
  • Find an example below but keep in mind that the values are set according to your .env file.
  • For app Env, make sure to insert the value 'production'.
PS F:\Projects\Zarafah> heroku config:add APP_NAME=Zarafah
PS F:\Projects\Zarafah> heroku config:add APP_ENV=production
  • In APP_URL, insure to copy the url for your app from Heroku.
  • In my case 👇
PS F:\Projects\Zarafah> heroku config:add APP_URL=https://zarafah.herokuapp.com/

10- Setting up DataBase in Heroku:

By now if you don't have any database connections in your Laravel project, your app show open and work on Heroku normally 🥳🥳

In case you do have Database connections like me, continue 👇

  • Head to Heroku Dashboard.
  • Press on your App.
  • Click on 'Resources'
  • Search for 'heroku postgres'
  • Click 'Provision'. Heroku Postgres Provision

Next step is very important, we're going to get our add-on credentials from heroku and update them one by one.

  • Open up the terminal in your main project folder.
PS F:\Projects\Zarafah> heroku pg:credentials:url
  • You will get a response with all the credentials and we will be adding them one by one.
  • Have your .env file open.
  • We will copy the keys from .env file and the values from the response we got from the terminal.
  • In the below example i'm just going to write down '...' instead of the actual credentials.
PS F:\Projects\Zarafah> heroku config:add DB_CONNECTION=pgsql
PS F:\Projects\Zarafah> heroku config:add DB_HOST= ...
PS F:\Projects\Zarafah> heroku config:add DB_PORT= ...
PS F:\Projects\Zarafah> heroku config:add DB_DATABASE= ...
PS F:\Projects\Zarafah> heroku config:add DB_USERNAME= ...
PS F:\Projects\Zarafah> heroku config:add DB_PASSWORD= ...

11- Migration and seed:

  • Now we can migrate with our new Database.
  • Note that you can't migrate your --seed since Heroku add-on doesn't support Laravel Factory.
  • Run the below code to proceed with your migration
PS F:\Projects\Zarafah> heroku run php artisan migrate

12- Finishing:

Congratulations your Laravel project is now live on Heroku with database for free 🎉🥳

You can visit the app with the url that heroku created.

This is a great opportunity for you to test your app and see it going live and share it with others.

If you have any questions please ask me in the comments below or find me on Twitter

Thank you for reading ❤️


This content originally appeared on DEV Community and was authored by Mostafa Said


Print Share Comment Cite Upload Translate Updates
APA

Mostafa Said | Sciencx (2022-01-30T13:13:14+00:00) Deploy Laravel Projects With Database For Free!. Retrieved from https://www.scien.cx/2022/01/30/deploy-laravel-projects-with-database-for-free/

MLA
" » Deploy Laravel Projects With Database For Free!." Mostafa Said | Sciencx - Sunday January 30, 2022, https://www.scien.cx/2022/01/30/deploy-laravel-projects-with-database-for-free/
HARVARD
Mostafa Said | Sciencx Sunday January 30, 2022 » Deploy Laravel Projects With Database For Free!., viewed ,<https://www.scien.cx/2022/01/30/deploy-laravel-projects-with-database-for-free/>
VANCOUVER
Mostafa Said | Sciencx - » Deploy Laravel Projects With Database For Free!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/30/deploy-laravel-projects-with-database-for-free/
CHICAGO
" » Deploy Laravel Projects With Database For Free!." Mostafa Said | Sciencx - Accessed . https://www.scien.cx/2022/01/30/deploy-laravel-projects-with-database-for-free/
IEEE
" » Deploy Laravel Projects With Database For Free!." Mostafa Said | Sciencx [Online]. Available: https://www.scien.cx/2022/01/30/deploy-laravel-projects-with-database-for-free/. [Accessed: ]
rf:citation
» Deploy Laravel Projects With Database For Free! | Mostafa Said | Sciencx | https://www.scien.cx/2022/01/30/deploy-laravel-projects-with-database-for-free/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.