Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Production #181

Merged
merged 8 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# django-nextjs-template
# Tech Stack

Django + Nextjs Template: Standardised CFC Tech Stack
Django + Nextjs +Tailwind CSS + PostgreSQL

## Get started

Expand Down Expand Up @@ -32,3 +32,39 @@ If you modify anything in the `docker` folder, you need to add the `--build` fla
### Custom env vars

Edit the `.env` file in the respective directory (client or server).

### Production

#### [0] Install git and docker on the VPS

#### [1] Modify client environment parameter

1. make a copy of "./client/.env.example" and rename the copy file to `.env.prod`:
2. modify the value in `.env.prod`: `APP_ENV=PRODUCTION`; `NEXT_PUBLIC_BACKEND_URL="http://{YOUR PUBLIC IP ADRESS}:8000/api"`

#### [2] Modify server environment parameter

1. make a copy of "./server/.env.example" and rename the copy file to `.env.prod`:
2. modify the value in `.env.prod`: `API_ALLOWED_HOSTS=".localhost 127.0.0.1 [::1] {YOUR PUBLIC IP ADRESS}"` and remain the APP_ENV as "DEVELOPMENT"

#### [3] Modify the URL in `./client/src/lib/api.ts`

Change the value of `LocalBaseURL` from "http://localhost:8000/api" to "http://{YOUR PUBLIC IP ADRESS}:8000/api"

```bash
-- const LocalBaseURL = "http://localhost:8000/api";
++ const LocalBaseURL = "http://{YOUR PUBLIC IP ADRESS}/api";
```

#### [4] Change the `./docker-compose.yml` to production mode:

1. delete `./docker-compose.yml`
2. change the name of file `./docker-compose.prod.yml` to `./docker-compose.yml`

#### [5] Compose up

Run the command below and it will start 3000 port as the frontend port:

```bash
docker compose up
```
3 changes: 2 additions & 1 deletion client/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const api = axios.create({ baseURL: process.env.NEXT_PUBLIC_BACKEND_URL });

export default api;

const LocalBaseURL = "http://localhost:8000/api"; // local use
const LocalBaseURL =
process.env.NEXT_PUBLIC_LOCAL_BASE_URL || "http://localhost:8000/api"; // local use
export { LocalBaseURL };

const axiosInstance = axios.create({
Expand Down
2 changes: 1 addition & 1 deletion docker/client/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ if [ "${APP_ENV^^}" = "DEVELOPMENT" ]; then
exit
fi

if [ "${APP_ENV^^}" = "PRODUCTION"]; then
if [[ "${APP_ENV^^}" = "PRODUCTION" ]]; then
npm install
echo " "
echo "======= Starting inbuilt nextjs webserver ==================================================================="
Expand Down
20 changes: 17 additions & 3 deletions docker/server/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,21 @@ fi
if [ "${APP_ENV^^}" = "PRODUCTION" ]; then

# Run Gunicorn / Django
printf "\n" && echo " Running Gunicorn / Django"
echo "Running: gunicorn api.wsgi -b 0.0.0.0:8000 --workers=6 --keep-alive 20 --log-file=- --log-level debug --access-logfile=/var/log/accesslogs/gunicorn --capture-output --timeout 50"
gunicorn api.wsgi -b 0.0.0.0:8000 --workers=6 --keep-alive 20 --log-file=- --log-level debug --access-logfile=/var/log/accesslogs/gunicorn --capture-output --timeout 50
# printf "\n" && echo " Running Gunicorn / Django"
# echo "Running: gunicorn api.wsgi -b 0.0.0.0:8000 --workers=6 --keep-alive 20 --log-file=- --log-level debug --access-logfile=/var/log/accesslogs/gunicorn --capture-output --timeout 50"
# gunicorn api.wsgi -b 0.0.0.0:8000 --workers=6 --keep-alive 20 --log-file=- --log-level debug --access-logfile=/var/log/accesslogs/gunicorn --capture-output --timeout 50



# a workaround to run gunicorn with poetry
printf "\n" && echo "Installing dev dependencies for $APP_ENV"
poetry install

# Run developments
printf "\n" && echo "Starting inbuilt django webserver"
echo "Running: python manage.py runserver 0.0.0.0:8000"
python manage.py runserver 0.0.0.0:8000
exit


fi
Loading