Skip to content

Instantly share code, notes, and snippets.

@imam932
Last active September 12, 2020 06:02
Show Gist options
  • Save imam932/1d28ead140b2f58119b861a9e958c789 to your computer and use it in GitHub Desktop.
Save imam932/1d28ead140b2f58119b861a9e958c789 to your computer and use it in GitHub Desktop.
Deploy Laravel to Heroku with DB Postgresql
## Intro
Kali ini aku membahas cara deploy project laravel ke heroku, untuk database di local menggunakan mysql dan di heroku menggunakan postgresql, karena yang free dari heroku hanya postgresql. Mengenai database cukup setting di environtment saja.
Sebelum dimulai, pastikan Anda telah memahami:
a. Composser
b. Cara install Laravel, konfigurasi .env variables dan migrasi database
c. Git
d. Punya akun Heroku
## Deploy to Heroku
Asumsikan sudah ada project yang sudah siap di deploy, dan sudah install [Heroku CLI -> Lihat di sini](https://devcenter.heroku.com/articles/heroku-cli)
1. Masuk ke dalam folder project
```
$ cd projectname
```
2. Inisialisasi Git [jika belum init git]
```
$ git init
$ git add .
$ git commit -m "initial commit deploy laravel to heroku"
```
3. Membuat Procfile
```
$ echo web: vendor/bin/heroku-php-apache2 public/ > Procfile
$ git add .
$ git commit -m "procfile for heroku"
```
4. Create variable app name
```
app_name=heroku-laravel-test-deploy
```
5. Create heroku app
```
$ heroku apps:create $app_name
$ heroku addons:create heroku-postgresql:hobby-dev --app $app_name //untuk install addons postgresql
$ heroku config:get DATABASE_URL //ambil informasi koneksi
// Maka akan muncul URL database dengan format:
// postgres://username:password@host:port/database_name
$ heroku buildpacks:add heroku/php --app $app_name //untuk memilih buildpack
```
DB MySQL [masih belum testing]
```
// Jika menggunakan MySQL
$ heroku addons:create cleardb:ignite
// Ambil informasi koneksi
$ heroku config:get CLEARDB_DATABASE_URL
// Maka akan muncul URL database dengan format:
// mysql://username:password@host/database_name?reconnect=true
```
6. Set ENV Variable
```
// DB_CONNECTION ganti sesuai dengan yang Anda pakai
// DB_HOST, DB_PORT, DB_DATABASE,
// DB_USERNAME dan DB_PASSWORD
// diambil dari URL database yang didapatkan ketika install database addons
// APP_LOG= errorlog : disarankan Heroku agar logging tidak ke laravel storage
// tapi ke Heroku
// Heroku juga merekomendasikan Trusting the Load Balancer :
// https://devcenter.heroku.com/articles/getting-started-with-laravel#trusting-the-load-balancer
$ heroku config:set \
APP_ENV=production \
APP_KEY=$(php artisan --no-ansi key:generate --show) \
APP_DEBUG=false \
APP_LOG=errorlog \
DB_CONNECTION=pgsql \
DB_HOST=host \
DB_PORT=port \
DB_DATABASE=database_name \
DB_USERNAME=username \
DB_PASSWORD=password
```
7. Add remote to repo heroku
```
heroku git:remote --app $app_name
```
8. Push to heroku, migrate and seed
```
$ git push heroku master
$ heroku run php artisan migrate
$ heroku run php artisan db:seed (opsional)
```
9. Run App
```
$ heroku open
```
10. Finish
browser akan otomatis terbuka dan menampilkan domain app, jika gagal membuka browser maka akan di tampilkan url domain di cli.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment