5

I have a digitalocean server and I have already deployed my Django backend server using gunicorn and nginx.

How do I deploy the React app on the same server?

dracarys
  • 1,071
  • 1
  • 15
  • 31
  • What do you mean by the words *on the same server*? On the same server using subdomain, on the same domain using some URI prefix, etc? – Ivan Shatsky Dec 03 '20 at 11:12

1 Answers1

8
  • You can build React app and serve its static files with Nginx
  • You can use docker-compose to manage Nginx and Django. What is more, you can build React static files during docker build.

Here is my article: Docker-Compose for Django and React with Nginx reverse-proxy and Let's encrypt certificate.

Below my Nginx configuration:

server {
    listen 80;
    server_name _;
    server_tokens off;
    client_max_body_size 20M;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }

    location /api {
        try_files $uri @proxy_api;
    }
    location /admin {
        try_files $uri @proxy_api;
    }

    location @proxy_api {
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Url-Scheme $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass   http://backend:8000;
    }

    location /django_static/ {
        autoindex on;
        alias /app/backend/server/django_static/;
    }
}

Nginx dockerfile:

# The first stage
# Build React static files
FROM node:13.12.0-alpine as build

WORKDIR /app/frontend
COPY ./frontend/package.json ./
COPY ./frontend/package-lock.json ./
RUN npm ci --silent
COPY ./frontend/ ./
RUN npm run build

# The second stage
# Copy React static files and start nginx
FROM nginx:stable-alpine
COPY --from=build /app/frontend/build /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"]
pplonski
  • 5,023
  • 1
  • 30
  • 34
  • Thank you, this will probably be helpful. I do not understand much about hosting and various different kinds of servers available on cloud. Can you point me to some resource or guide where I can learn this from, if you know of a good one? Thanks – dracarys Dec 07 '20 at 08:00
  • The simplest (for me) is Digital Ocean (prices starting with 5$/mo). But for long term projects/deployments that will be live in years, I prefer to use AWS EC2 service (a little expensive than Digital Ocean). – pplonski Dec 07 '20 at 10:10
  • 1
    Thanks, I was looking for some resources to understand the concepts of dev ops, cloud and deployment. – dracarys Dec 07 '20 at 10:18
  • when we go to domain/api django complaints that /api url is not present. Ideally it should point domain/api to the home page of django . – Smith Nov 13 '22 at 17:29