๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Side project

[์‚ฝ์งˆ๋กœ๊ทธ] Heroku๋กœ Strapi(with PostgreSQL) ๋ฐฐํฌํ•˜๊ธฐ 1/2

์ด ๊ธ€์€ Heroku Free tier๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜ธ์ŠคํŒ…ํ•˜๊ธฐ ์œ„ํ•œ ํŠœํ† ๋ฆฌ์–ผ์ธ๋ฐ 2022๋…„ 11์›” 28์ผ์ž๋กœ Heroku์—์„œ Free tier๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ฌด๋ฃŒ ๋ฐฐํฌ๋ฅผ ํ•˜๊ธฐ ์›ํ•˜๋Š” ๋ถ„๋“ค์€ ๋‹ค๋ฅธ ํŠœํ† ๋ฆฌ์–ผ๋“ค์„ ์ฐธ๊ณ ํ•ด๋ณด๊ธธ ๋ฐ”๋ž€๋‹ค. 

(์ด ๊ธ€์€ 2021๋…„ 4์›” 14์ผ ๊ธฐ์ค€ ๊ธ€์ด๋‹ค.)

๐Ÿคจ ์‚ฝ์งˆ์˜ ๋ฐœ๋‹จ

์ง€๋‚œ๋‹ฌ์— ์ƒˆ๋กœ์šด ์‚ฌ์ด๋“œ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ–ˆ๋‹ค. ํ”„๋ก ํŠธ๋Š” Next.js๋ฅผ ์‚ฌ์šฉํ•˜์˜€๊ณ  ๋ฐฑ์—”๋“œ๋Š” Github์— jsonํŒŒ์ผ์„ ์˜ฌ๋ ค gh-pages๋กœ ํ˜ธ์ŠคํŒ…์„ ํ•œ ์ •์„์ด ์•„๋‹Œ(?) ๋ฃจํŠธ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. ํ”„๋กœ์ ํŠธ๋ฅผ ๋Œ€๋žต ์™„์„ฑํ•˜๊ธฐ๋Š” ํ–ˆ์ง€๋งŒ ๋ฐฑ์—”๋“œ๊ฐ€ ์˜... ๋„ˆ๋ฌด ์ฐœ์ฐœํ–ˆ๋‹ค.
๋ˆ์€ ๋“ค์ด๊ธฐ ์‹ซ๊ณ , API๋Š” ๋งŒ๋“ค๊ณ  ์‹ถ๊ณ , ์ด๊ฒƒ์ €๊ฒƒ ์ฐพ์•„๋ณด๋‹ค ์•Œ๊ฒŒ ๋œ Strapi!
COMMUNITY PLAN์€ ๋ฌด๋ฃŒ๋ผ๊ธฐ์— ํŠœํ† ๋ฆฌ์–ผ์„ ์ฐพ์•„์„œ ๋ฐ”๋กœ ์„ค์น˜ํ–ˆ๋‹ค. ์‚ฌ์‹ค ์„ค์น˜ํ•œ์ง€๋Š” ๋ฒŒ์จ 3์ฃผ ์ „?์ด๋‹ˆ ์‹œ๊ฐ„์ด ๊ทธ๋ž˜๋„ ํ˜๋ €์ง€๋งŒ... Strapi๋ฅผ ์ฒ˜์Œ ๋งŒ๋‚˜๊ณ ๋„ ์•ฝ๊ฐ„์˜ ์šฐ์—ฌ๊ณก์ ˆ์ด ์žˆ์—ˆ๋‹ค. (์šฐ์—ฌ๊ณก์ ˆ์€ ์ƒ๋žตํ•œ๋‹ค.) ๋ฌดํŠผ ๊ทธ๋Ÿฌ๋‹ค ์–ด์ œ ์ œ๋Œ€๋กœ ๊ฐ์žก๊ณ  ์•‰์•„์„œ ๋ฐฐํฌ๊นŒ์ง€ ๋๋‚ด๋ฒ„๋ฆฌ๋ฆฌ ๊ฒฐ์‹ฌํ•˜๊ณ  ์ง„์งœ ๋๋ƒˆ๋‹ค.
์™œ ๋งค๋ฒˆ ์„ค์ •ํ•˜๋Š” ๊ฑด ์ด๋ ‡๊ฒŒ๋„ ํž˜๋“ค๊ณ  ์‚ฝ์งˆ์„ ํ•ด์•ผ๋งŒ ํ•˜๋Š”์ง€... ๋‚ด๊ฐ€ ๋„ˆ๋ฌด ๋ชจ๋ฅด๋Š” ๊ฒŒ ๋งŽ์•„์„œ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ ์•„๋Š” ๊ฐœ๋ฐœ์ž ๋ถ„๊ป˜ ํ•œ๋ฒˆ ์—ฌ์ญค๋ณด๋‹ˆ ๋ณธ์ธ๋„ ๊ทธ๋Ÿฌ์‹œ๋‹จ๋‹ค... ๊ทธ๋ž˜์„œ ๊ธฐ๋กํ•ด๋‘ฌ์•ผ ํ•œ๋‹ค๊ณ ... (์œ„๋กœ๊ฐ€ ๋๋‹ค.)
ํ˜น์‹œ Strapi๋ฅผ ๋˜ ์‚ฌ์šฉํ•  ๋ฏธ๋ž˜์˜ ๋‚˜๋ฅผ ์œ„ํ•ด ์ด ๊ธ€์„ ์ž‘์„ฑํ•œ๋‹ค. ํ˜น ์ด ๊ธ€์ด ๋‹ค๋ฅธ ๋ถ„๊ป˜๋„ ๋„์›€์ด ๋œ๋‹ค๋ฉด.... (๋” ํฐ ์‹œ๋ จ์„ ์•ˆ๊ฒจ๋“œ๋ฆฌ์ง€ ์•Š๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„) ์ข‹๊ฒ ๋‹ค.

๐Ÿ›  ์ค€๋น„์‚ฌํ•ญ

Strapi ๊ณต์‹ ๋ฌธ์„œ ์ฐธ๊ณ 

 

Heroku

The headless CMS developers love.

strapi.io

๐Ÿš€ ์‹œ์ž‘

1. Heroku ๋กœ๊ทธ์ธํ•˜๊ธฐ (์ค€๋น„์‚ฌํ•ญ์ด ๋ชจ๋‘ ์ค€๋น„ ๋˜์–ด์žˆ์–ด์•ผํ•œ๋‹ค.) 

heroku login

์œ„์˜ ๋ช…๋ น์–ด๋ฅผ ํ„ฐ๋ฏธ๋„์— ์ž…๋ ฅํ•˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๋กœ Heroku Login ํ™”๋ฉด์ด ๋œจ๊ณ  Login ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค. ์„ฑ๊ณต์ ์œผ๋กœ ๋กœ๊ทธ์ธ๋˜๋ฉด ํ„ฐ๋ฏธ๋„์˜ Heroku CLI์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

Logged in as <๋‚ด ๊ณ„์ • ์ด๋ฉ”์ผ>

 

2. PostgreSQL ๋กœ์ปฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑํ•˜๊ธฐ

์ƒˆ๋กœ์šด ํ„ฐ๋ฏธ๋„ ์ฐฝ์„ ํ•˜๋‚˜ ๋„์›Œ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰ํ•œ๋‹ค. 

# 1. postgres ์‹คํ–‰
psql postgres

# 2. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ
CREATE DATABASE <๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ช…>;

# 3. User role ์ƒ์„ฑ
CREATE ROLE <๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์šฉ์ž๋ช…> WITH LOGIN PASSWORD '<ํŒจ์Šค์›Œ๋“œ>' CREATEDB;

# 4. ๊ถŒํ•œ ๋ถ€์—ฌ
GRANT ALL PRIVILEGES ON DATABASE <๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ช…> TO <๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์šฉ์ž๋ช…>;

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ช…, ์‚ฌ์šฉ์ž๋ช…, ํŒจ์Šค์›Œ๋“œ๊ฐ€ Strapi ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ์‹œ ํ•„์š”ํ•˜๋‹ˆ ๊ธฐ์–ตํ•ด๋‘˜ ๊ฒƒ! Heroku ๋กœ๊ทธ์ธ์ด ๋˜์–ด ์žˆ๋Š” ์ฐฝ์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค. 

 

3. Strapi ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ

ํ„ฐ๋ฏธ๋„์—์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ์€ ํด๋”๋กœ ์ด๋™ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด "test-api"๋ผ๋Š” Strapi ํ”„๋กœ์ ํŠธ๋ฅผ "sideproject" ํด๋” ์•„๋ž˜์— ์ƒ์„ฑํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, "sideproject" ํด๋”๋กœ ์ด๋™ํ•˜์—ฌ ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. yarn/npx ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์— ๋งž์ถฐ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.  

# yarn
yarn create strapi-app <ํ”„๋กœ์ ํŠธ ์ด๋ฆ„>

#npx
npx create-strapi-app <ํ”„๋กœ์ ํŠธ ์ด๋ฆ„>

์œ„์˜ ๋ช…๋ น์–ด์—์„œ --quickstart๋ผ๋Š” ํ”Œ๋ž˜๊ทธ๋ฅผ ๋งจ ๋’ค์— ๋ถ™์ด๋ฉด ๋กœ์ปฌ์—์„œ๋Š” SQLite ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ Heroku์—์„œ๋Š” ํ˜ธํ™˜๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹ค. --quickstart๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑํ•ด์ค€ ํ”„๋กœ์ ํŠธ๋Š” ๋‚˜์ค‘์— Heroku์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋”ฐ๋กœ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— --quickstart๋ผ๋Š” ํ”Œ๋ž˜๊ทธ ์—†์ด ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์—ˆ๋‹ค. 

--quickstart๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์ง€ ์•Š์•„๋„ ์œ„์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณด์ธ๋‹ค.

? Choose your installation type (Use arrow keys)
  Quickstart (recommended)
> Custom (manual settings)

ํ™”์‚ดํ‘œ ํ‚ค๋กœ Custom์œผ๋กœ ์ด๋™ ํ›„ ์„ ํƒํ•ด์ฃผ๋ฉด(์—”ํ„ฐํ‚ค) ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. 

? Choose your default database client (Use arrow keys)
  sqlite
> postgres
  mysql
  mongo

์œ„์™€ ๊ฐ™์ด ํ™”์‚ดํ‘œ ํ‚ค๋กœ postgres๋กœ ์ด๋™ ํ›„ ์„ ํƒํ•ด์ฃผ๋ฉด(์—”ํ„ฐํ‚ค) ์•„๋ž˜์™€ ๊ฐ™์ด ์ „ ๋‹จ๊ณ„์—์„œ ์ƒ์„ฑํ•ด์ค€ postgres ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•œ๋‹ค. 

? Database name: <๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ช…>
? Host: (127.0.0.1)				# ์—”ํ„ฐ์น˜๊ณ  ๋„˜์–ด๊ฐ
? Port: (5432) 					# ์—”ํ„ฐ์น˜๊ณ  ๋„˜์–ด๊ฐ
? Username: <๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์šฉ์ž๋ช…>
? Password: <ํŒจ์Šค์›Œ๋“œ>
? Enable SSL connection: N			# N ์ž…๋ ฅ, y๋Š” ์„ค์ •์ด ๋”ฐ๋กœ ํ•„์š”ํ•œ๋“ฏ...

๊ธฐ๋‹ค๋ฆฌ๋ฉด ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์„ ์‹œ์ž‘ํ•˜๊ณ  ์„ค์ •ํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์•ฝ๊ฐ„ ๊ฑธ๋ฆฐ๋‹ค. ์ƒ์„ฑ์ด ์™„๋ฃŒ๋˜๋ฉด ์ƒ์„ฑ๋œ ํ”„๋กœ์ ํŠธ ํด๋”๋กœ ์ด๋™ํ•ด์„œ yarn develop/start/build ๋ช…๋ น์–ด๋กœ ๋กœ์ปฌ๋กœ ๋Œ๋ฆด ์ˆ˜ ์žˆ์ง€๋งŒ Heroku์— ๋ฐฐํฌํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ ๋‹จ๊ณ„๋กœ ์ง„ํ–‰ํ•œ๋‹ค. 

 

4. .gitignore ์—…๋ฐ์ดํŠธํ•˜๊ธฐ

.gitignore ํŒŒ์ผ์„ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž์‹ ์ด ์‚ฌ์šฉํ•˜๋Š” ํŽธ์ง‘๊ธฐ๋กœ ํด๋”๋ฅผ ์—ฐ๋‹ค. 
.gitignore ํŒŒ์ผ ๋งจ ๋์— package-lock.json์„ ์ถ”๊ฐ€ ํ›„ ์ €์žฅํ•ด์ค€๋‹ค. (115์ค„ ์ฐธ๊ณ )

 

5. ํ•ด๋‹น ํ”„๋กœ์ ํŠธ git init์œผ๋กœ ์ดˆ๊ธฐํ™” ๋ฐ ์ €์žฅ์†Œ์— ์ปค๋ฐ‹

ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์˜ ์œ„์น˜(./strapi ํ”„๋กœ์ ํŠธ๋ช…/)์—์„œ ์•„๋ž˜์˜ git ๋ช…๋ น์–ด๋“ค์„ ์‹คํ–‰ํ•ด์ค€๋‹ค. 

# path: ./Strapi ํ”„๋กœ์ ํŠธ๋ช…/
git init
git add .
git commit -m "Initial Commit"

 

6. Heroku ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ

5๋ฒˆ๊ณผ ๊ฐ™์€ ๊ฒฝ๋กœ์—์„œ ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ด์ค€๋‹ค. 

# ์ž๋™์ƒ์„ฑ๋˜๋Š” ๋žœ๋ค ํ”„๋กœ์ ํŠธ ๋ช…์„ ์‚ฌ์šฉํ•ด๋„ ๊ดœ์ฐฎ์€ ๊ฒฝ์šฐ
heroku create

# ์ž์‹ ์ด ํ”„๋กœ์ ํŠธ ๋ช…์„ ์ž…๋ ฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ: <์ปค์Šคํ…€-ํ”„๋กœ์ ํŠธ-๋ช…>.heroku.com์œผ๋กœ URL์ด ์ƒ์„ฑ๋จ
heroku create <์ปค์Šคํ…€-ํ”„๋กœ์ ํŠธ-๋ช…>

์œ„์˜ ๋ช…๋ น์–ด์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋กœ Heroku ํ”„๋กœ์ ํŠธ URL์ด ์ƒ์„ฑ๋œ๋‹ค. ๊ณ„์†ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์ •์„ ์ง„ํ–‰ํ•ด๋ณด์ž.

 

7. Heroku ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์ •

์—ฌ๊ธฐ์„œ๋Š” PostgreSQL ๊ธฐ์ค€์œผ๋กœ ์„ค์ •์„ ์ง„ํ–‰ํ•œ๋‹ค.

- Heroku Postgres addon ์„ค์น˜ํ•˜๊ธฐ

Hobby Dev ํ”Œ๋žœ์—์„œ๋Š” ๋ฌด๋ฃŒ๋กœ ์ œ๊ณต๋œ๋‹ค. ์‹ค์ œ๋กœ ์šด์˜ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฐํฌํ•˜๋Š” ๊ฒฝ์šฐ ์œ ๋ฃŒ ํ”Œ๋žœ์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค๊ณ  ๋ฌธ์„œ์— ์ ํ˜€์žˆ๋‹ค. 
๊ฒฝ๋กœ๋Š” 5๋ฒˆ๊ณผ ๊ฐ™์€ ./Strapi ํ”„๋กœ์ ํŠธ๋ช…/ ์ด๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅํ•ด์ค€๋‹ค. 

heroku addons:create heroku-postgresql:hobby-dev

์„ฑ๊ณต์ ์œผ๋กœ ์„ค์น˜๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ "Database has been created and is available"์ด๋ผ๋Š” ๋ฌธ๊ตฌ์™€ ํ•จ๊ป˜ ๋ช‡ ๋งˆ๋””๊ฐ€ ๋” ์ถœ๋ ฅ๋œ๋‹ค. 

- ์ƒ์„ฑ๋œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ™•์ธํ•˜๊ธฐ

heroku config

์œ„์™€ ๊ฐ™์ด ์ž…๋ ฅํ•˜๋ฉด, postgres://USERNAME:PASSWORD@HOST:PORT:DATABASE_NAME ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค URL์ด ์ถœ๋ ฅ๋œ๋‹ค. 
์ด ๋‚ด์šฉ์€ dashboard.heroku.com/apps ์—์„œ ๋ณธ์ธ์ด ์ƒ์„ฑํ•œ Heroku ํ”„๋กœ์ ํŠธ๋ฅผ ์„ ํƒ ํ›„ > Resources ํƒญ > Add-ons์˜ Heroku Postgres ํด๋ฆญ 

์„ธ ๋ฒˆ์งธ ํƒญ Settings > Database Credentials์—์„œ๋„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ณ€์ˆ˜ ์ž๋™ ์„ค์ •ํ•˜๊ธฐ

Strapi๋Š” ์œ„์—์„œ ์ƒ์„ฑ๋œ URL์„ pg-connection-string ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. Heroku์—์„œ ์œ„์˜ URL์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€๊ฒฝ๋œ URL์—์„œ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์ž๋™์œผ๋กœ ๋ถ„์„ํ•˜๋„๋ก pg-connection-string ํŒจํ‚ค์ง€๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. 

# npm 
npm install pg-connection-string --save

# yarn
yarn add pg-connection-string

- ์šด์˜ํ™˜๊ฒฝ ๋ฐฐํฌ ์œ„ํ•œ Heroku ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค config ํŒŒ์ผ ์ƒ์„ฑํ•˜๊ธฐ

config ํด๋” ํ•˜์œ„์— /env/production ํด๋”๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๊ณ  production ํด๋” ์•ˆ์— database.js ๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค. ๊ฒฝ๋กœ๋Š” ./config/env/producton/database.js ์ด์™€ ๊ฐ™์•„์•ผ ํ•œ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ ์ฐธ๊ณ 

database.js ํŒŒ์ผ์—๋Š” ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ ๋Š”๋‹ค. 

const parse = require('pg-connection-string').parse;
const config = parse(process.env.DATABASE_URL);

module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: 'postgres',
        host: config.host,
        port: config.port,
        database: config.database,
        username: config.user,
        password: config.password,
        ssl: {
          rejectUnauthorized: false,
        },
      },
      options: {
        ssl: true,
      },
    },
  },
});

์ƒˆ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ตฌ์„ฑ ํŒŒ์ผ์ด ์‚ฌ์šฉ๋˜๋„๋ก Heroku์˜ NODE_ENV๋ฅผ production์œผ๋กœ ์„ค์ •ํ•ด์ค€๋‹ค. ๊ฒฝ๋กœ๋Š” Strapi ํ”„๋กœ์ ํŠธ๊ฐ€ ์œ„์น˜ํ•œ ๊ณณ์ด๋‹ค.

heroku config:set NODE_ENV=production

- ์šด์˜ํ™˜๊ฒฝ์„ ์œ„ํ•ด์„œ Strapi server config ์ƒ์„ฑํ•˜๊ธฐ

database.js๋ฅผ ์ƒ์„ฑํ•œ ํด๋”์— server.js๋„ ์ƒ์„ฑํ•ด์ค€๋‹ค. 

ํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. 

module.exports = ({ env }) => ({
  url: env('HEROKU_URL'),
});

 

ํ„ฐ๋ฏธ๋„์—์„œ Heroku์— ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค. ๊ฒฝ๋กœ๋Š” Strapi ํ”„๋กœ์ ํŠธ๊ฐ€ ์œ„์น˜ํ•œ ๊ณณ์ด๋‹ค.

heroku config:set HEROKU_URL=$(heroku info -s | grep web_url | cut -d= -f2)

HEROKU_URL: https://<HEROKU-ํ”„๋กœ์ ํŠธ-๋ช…>.herokuapp.com/ ๊ฐ™์ด ๋ณด์ธ๋‹ค. 

- (์„ ํƒ) ๋กœ์ปฌ Strapi ํ”„๋กœ์ ํŠธ๊ฐ€ Postgres๋กœ ์„ค์ •๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด pg node modules ์„ค์น˜ ํ•„์š”

์œ„์—์„œ ์ง„ํ–‰ํ•œ ๋ฐฉ๋ฒ•์€ ๋กœ์ปฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ Postgres๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š”ํ•˜์ง€ ์•Š์Œ. 
๋งŒ์•ฝ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์น˜

npm install pg --save

 

8. ๋ณ€๊ฒฝ์‚ฌํ•ญ ์ปค๋ฐ‹ํ•˜๊ธฐ

ํ„ฐ๋ฏธ๋„ ํ˜น์€ IDE์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ปค๋ฐ‹ํ•œ๋‹ค. ๊ฒฝ๋กœ๋Š” Strapi ํ”„๋กœ์ ํŠธ๊ฐ€ ์œ„์น˜ํ•œ ๊ณณ์ด๋‹ค.

git add .
git commit -m "Update database config" # ์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€๋Š” ์•Œ์•„์„œ...

 

9. Yarn lockfile ์—…๋ฐ์ดํŠธ

ํ„ฐ๋ฏธ๋„์—์„œ ๊ฒฝ๋กœ๋Š” Strapi ํ”„๋กœ์ ํŠธ๊ฐ€ ์œ„์น˜ํ•œ ๊ณณ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅํ•œ๋‹ค. 

yarn install

 

10. ๋ณ€๊ฒฝ์‚ฌํ•ญ ์ปค๋ฐ‹ํ•˜๊ธฐ

ํ„ฐ๋ฏธ๋„ ํ˜น์€ IDE์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ปค๋ฐ‹ํ•œ๋‹ค. ๊ฒฝ๋กœ๋Š” Strapi ํ”„๋กœ์ ํŠธ๊ฐ€ ์œ„์น˜ํ•œ ๊ณณ์ด๋‹ค.

git add yarn.lock
git commit -m "Updated Yarn lockfile" # ์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€๋Š” ์•Œ์•„์„œ...

 

11. ๋ฐฐํฌ

ํ„ฐ๋ฏธ๋„ ํ˜น์€ IDE์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘ธ์‹œํ•œ๋‹ค. ๊ฒฝ๋กœ๋Š” Strapi ํ”„๋กœ์ ํŠธ๊ฐ€ ์œ„์น˜ํ•œ ๊ณณ์ด๋‹ค.

git push heroku HEAD:main

๋ฐฐํฌ๋Š” ๋ช‡ ๋ถ„ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ๋ฐฐํฌ๊ฐ€ ๋๋‚˜๊ณ  ํ”„๋กœ์ ํŠธ URL์ด ๋ณด์ด๋ฉด ์—ด์–ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํ„ฐ๋ฏธ๋„์— heroku open์„ ์ž…๋ ฅํ•ด๋„ ๋œ๋‹ค. 
https://<HEROKU-ํ”„๋กœ์ ํŠธ-๋ช…>.herokuapp.com/์—์„œ Strapi ์›ฐ์ปด ํŽ˜์ด์ง€๊ฐ€ ๋ณด์ธ๋‹ค๋ฉด ์„ฑ๊ณต์ด๋‹ค! ๐Ÿ™Œ 

๋‹ค์Œ ํŽธ์—์„œ๋Š” Github๊ณผ ์—ฐ๊ฒฐํ•ด์„œ ์ž๋™ ๋ฐฐํฌ๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋‹ค๋ค„๋ณด๊ฒ ๋‹ค. 

 

๐Ÿ“ ์ฐธ์กฐ

https://strapi.io/documentation/developer-docs/latest/setup-deployment-guides/deployment/hosting-guides/heroku.html
https://tute.io/install-configure-strapi-postgresql