-
Notifications
You must be signed in to change notification settings - Fork 0
feat: deploy the app to the VM #47
Merged
+187
−9
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9f33631
fix: add VITE_API_URL for deployment
martakam 93418b5
docs: add deployment instructions
martakam 1c8d5f6
fix: use base path for favicons and name the web manifest
johahor fa97e05
docs: document VM setup and Apache fallback config
johahor d127b2e
docs: adjust command for updating frontend deployment
martakam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| # Deployment | ||
|
|
||
| ## Install dependencies | ||
|
|
||
| Start by installing our dependencies: | ||
|
|
||
| - Install [node 24](https://nodejs.org/en/download) | ||
| - Install [pnpm](https://pnpm.io/installation) | ||
| - Install [PostgreSQL 18](https://www.postgresql.org/download/) | ||
| - Install [pm2](https://pm2.keymetrics.io/) | ||
|
|
||
| The binaries provided by PostgreSQL may not be added to path automatically. | ||
| If the commands `pg_ctl` and `initdb` fail, do the following: | ||
|
|
||
| ```sh | ||
| cd ~ | ||
| touch .profile | ||
| nano .profile | ||
| # And add the following line: PATH=$PATH:/usr/lib/postgresql/18/bin/ | ||
|
|
||
| # Apply the changes | ||
| source .profile | ||
| ``` | ||
|
|
||
| ## Clone the project and set it up | ||
|
|
||
| Clone the project: | ||
|
|
||
| ```sh | ||
| git clone https://git.ntnu.no/IT2810-H26/T37-Project-2.git | ||
| cd T37-Project-2 | ||
| ``` | ||
|
|
||
| ### Environment variables | ||
|
|
||
| This step needs to be done only whenever environment variables are updated (`.env`). | ||
|
|
||
| Set the correct environment values for the frontend: | ||
|
|
||
| ```sh | ||
| cd frontend | ||
| cp .env.example .env | ||
| nano .env # and change the VITE_API_URL | ||
| ``` | ||
|
|
||
| And do the same for the backend: | ||
|
|
||
| ```sh | ||
| cd backend | ||
| cp .env.example .env | ||
| nano .env # and change variables if needed | ||
| ``` | ||
|
|
||
| ### Build the frontend | ||
|
|
||
| Build the frontend and move the content to the correct folder: | ||
|
|
||
| ```sh | ||
| cd frontend | ||
| pnpm build | ||
| sudo mv build/client/ /var/www/html/project2 | ||
martakam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| > [!CAUTION] | ||
| > We only move the `client/` folder from the build! | ||
| > We do not use React Router's server-side functionality, because our frontend does not use server-side rendering (SSR). | ||
| > See [react-router.config.ts](./frontend/react-router.config.ts) | ||
| The move must be executed as sudo because the `/var` folders are owned by `root`. | ||
|
|
||
| ### Building the backend | ||
|
|
||
| Build the backend like so: | ||
|
|
||
| ```sh | ||
| cd backend | ||
| pnpm build | ||
| ``` | ||
|
|
||
| ### Prepare the database | ||
|
|
||
| We need the seed book data file. | ||
| For more details, please check the [backend README.md](./backend/README.md). | ||
|
|
||
| Let's download the dataset, unzip and place it in the correct folder: | ||
|
|
||
| ```sh | ||
| curl -L -o comprehensive-literary-greats-dataset.zip https://www.kaggle.com/api/v1/datasets/download/thedevastator/comprehensive-literary-greats-dataset | ||
| unzip comprehensive-literary-greats-dataset.zip | ||
| # After unzipping, a file named "books_1.Best_Books_Ever.csv" will be placed | ||
| # in the current working directory. | ||
|
|
||
| mv books_1.Best_Books_Ever.csv ./backend/src/db/seed/book-data.csv | ||
| rm comprehensive-literary-greats-dataset.zip | ||
| ``` | ||
|
|
||
| Set up the database as usual: | ||
|
|
||
| ```sh | ||
| cd backend | ||
| pnpm db:setup | ||
| ``` | ||
|
|
||
| > [!NOTE] | ||
| > If you receive permission errors, add your user to the `postgres` group by running the following command: | ||
| > `sudo usermod -aG postgres $USER` | ||
| ## Running the project | ||
|
|
||
| The frontend is already handling by the Apache web server by being placed in `/var/www/html/project2`. | ||
|
|
||
| In order to run the backend, we use `pm2`. | ||
|
|
||
| The first time project is run: | ||
|
|
||
| ```sh | ||
| cd backend | ||
| pm2 start --name "backend" --interpreter="/bin/bash" pnpm -- start | ||
| ``` | ||
|
|
||
| Afterwards, after changing the backend and building it, it's enough to restart it: | ||
|
|
||
| ```sh | ||
| # This command can be done in any directory on the machine! | ||
| pm2 restart backend | ||
| ``` | ||
|
|
||
| ## Our VM setup | ||
|
|
||
| This is what is set up on our VM (`it2810-37.idi.ntnu.no`, Ubuntu 26.04). | ||
| Apache was already installed and running. | ||
|
|
||
| ### Installed packages | ||
|
|
||
| ```sh | ||
| # Node 24 from NodeSource | ||
| curl -fsSL https://deb.nodesource.com/setup_24.x | sudo bash - | ||
| sudo apt install -y nodejs | ||
|
|
||
| # pnpm, installed globally | ||
| sudo npm install -g pnpm@12 | ||
|
|
||
| # PostgreSQL 18 | ||
| sudo apt install -y postgresql | ||
| ``` | ||
|
|
||
| ### Apache config for client-side routes | ||
|
|
||
| React Router handles routes like `/project2/book/5` in the browser. Without | ||
| this config, opening such a URL directly or reloading the page gives a 404, | ||
| because Apache looks for a file with that path. `FallbackResource` makes | ||
| Apache serve `index.html` instead, so the app can load and show the right page. | ||
| It only applies to the `project2` folder. | ||
|
|
||
| ```sh | ||
| sudo tee /etc/apache2/conf-available/project2.conf > /dev/null <<'CONF' | ||
| <Directory /var/www/html/project2> | ||
| FallbackResource /project2/index.html | ||
| </Directory> | ||
| CONF | ||
| sudo a2enconf project2 | ||
| sudo apache2ctl configtest # should print "Syntax OK" | ||
| sudo systemctl reload apache2 | ||
| ``` | ||
|
|
||
| This was only done once, and should only need to be run once. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Must be changed to http://it2810-37.idi.ntnu.no:3001/ for production | ||
| VITE_API_URL = http://localhost:3001 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.