【问题标题】:How to deploy a Django and React project on the same Ubuntu 18.04 server using gunicorn and nginx?如何使用 gunicorn 和 nginx 在同一个 Ubuntu 18.04 服务器上部署 Django 和 React 项目?
【发布时间】:2022-11-03 16:39:49
【问题描述】:

我有一个 Django 项目,我已经使用本教程通过 gunicorn 和 nginx 在我的 Ubuntu 18.04 服务器上成功部署了该项目。

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04

该项目使用 Django Rest Framework,我可以通过 Web 浏览器访问它的端点。但是,我还想在同一台服务器上部署一个单独的 react 项目,以便它可以向 Django 应用程序发送 http 请求并显示从 REST API 接收到的数据。我该怎么做呢?

这是我目前的gunicorn.service

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/my_project/coffeebrewer
ExecStart=/home/ubuntu/my_project/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/ubuntu/my_project/coffeebrewer/coffeebrewer.sock coffeebrewer.wsgi:application

[Install]
WantedBy=multi-user.target

这是我当前的 nginx 配置

server {
    listen 80;
    listen [::]:80;
    server_name my_ipv6_address;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
       root root /home/ubuntu/my_project/coffeebrewer;

    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

【问题讨论】:

    标签: reactjs django nginx web-deployment gunicorn


    【解决方案1】:

    我建议你首先在本地尝试作为生产模式通过安装whitenoise django package 并在您的settings.py 文件中添加这一行SECURE_CROSS_ORIGIN_OPENER_POLICY = None

    从那里,您可以向前导航

    【讨论】:

      猜你喜欢
      • 2013-12-08
      • 2021-10-06
      • 2021-09-24
      • 2020-04-06
      • 2016-08-06
      • 1970-01-01
      • 2017-01-07
      • 2020-12-26
      • 1970-01-01
      相关资源
      最近更新 更多