简体   繁体   English

如何在 Elastic Beanstalk 上使用 Django 提供 React 应用程序?

[英]How to Serve a React App with Django on Elastic Beanstalk?

I used to have my app on Heroku and the way it worked there was that I had 2 buildpacks.我曾经在 Heroku 上安装我的应用程序,它的工作方式是我有 2 个构建包。 One for NodeJS and one for Python.一个用于 NodeJS,一个用于 Python。 Heroku ran npm run build and then Django served the files from the build folder. Heroku 运行npm run build ,然后 Django 提供来自build文件夹的文件。

I use Code Pipeline on AWS to deploy a new version of my app every time there is a new push on my GitHub repository.每次在我的 GitHub 存储库上有新推送时,我都会使用 AWS 上的 Code Pipeline 部署我的应用程序的新版本。

Since I couldn't figure out how to run npm run build in a python environment in EB, I had a workaround.由于我不知道如何在 EB 的 python 环境中运行npm run build ,因此我有一个解决方法。 I ran npm run build and pushed it to my repository (removed the build folder from.gitignore) and then Django served the files on EB.我运行npm run build并将其推送到我的存储库(从.gitignore 中删除了build文件夹),然后 Django 在 EB 上提供文件。

However, this is not the best solution and I was wondering if anyone knows how to run npm run build the way Heroku can do it with their NodeJS buildpack for a python app on EB.然而,这不是最好的解决方案,我想知道是否有人知道如何运行npm run build方式 Heroku 可以使用他们的 NodeJS buildpack 来完成它,用于在 EB 上的 python 应用程序。

So I figured out one solution that worked for me.所以我想出了一种对我有用的解决方案。

Since I want to create the build version of my app on the server the way Heroku does it with the NodeJS buildpack, I had to create a command that installs node like this:由于我想以 Heroku 使用 NodeJS buildpack 的方式在服务器上创建我的应用程序的构建版本,因此我必须创建一个安装节点的命令,如下所示:

container_commands:
  01_install_node:
    command: "curl -sL https://rpm.nodesource.com/setup_12.x | sudo bash - && sudo yum install nodejs"
    ignoreErrors: false

And then to create the build version of the react app on a Python Environment EB, I added the following command:然后为了在 Python 环境 EB 上创建 react 应用程序的构建版本,我添加了以下命令:

container_commands:
  02_react:
    command: "npm install && npm run build"
    ignoreErrors: false

So of course, after the build version is created, you should collect static files, so here is how my working config file looked at the end:所以当然,在构建版本创建之后,你应该收集 static 文件,所以这是我的工作配置文件最后的样子:

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: <project_name>/wsgi.py

  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: <project_name>.settings

  aws:elasticbeanstalk:container:python:staticfiles:
    /static/: staticfiles/

container_commands:
  01_install_node:
    command: "curl -sL https://rpm.nodesource.com/setup_12.x | sudo bash - && sudo yum install nodejs"
    ignoreErrors: false

  02_react:
    command: "npm install && npm run build"
    ignoreErrors: false

  03_collectstatic:
    command: "django-admin.py collectstatic --noinput"

Hope this helps anyone who encounters the same希望这对遇到相同问题的人有所帮助

I don't know exactly Python but I guess you can adapt for you case.我不确切知道 Python 但我想你可以适应你的情况。

Elastic Beanstalk for Node.js platform use by default app.js , then server.js , and then npm start (in that order) to start your application. Node.js 平台的 Elastic Beanstalk 默认使用app.js ,然后server.js ,然后npm start (按此顺序)来启动您的应用程序。

You can change this behavior with configuration files .您可以使用配置文件更改此行为。 Below the steps to accomplish with Node.js:下面是使用 Node.js 完成的步骤:

  1. Create the following file .ebextensions/<your-config-file-name>.config with the following content:使用以下内容创建以下文件.ebextensions/<your-config-file-name>.config
     option_settings: aws:elasticbeanstalk:container:nodejs: NodeCommand: "npm run eb:prod"
  2. Edit your package.json to create the eb:prod command.编辑package.json以创建eb:prod命令。 For instance:例如:
     "scripts": { "start": "razzle start", "build": "razzle build", "test": "razzle test --env=jsdom", "start:prod": "NODE_ENV=production node build/server.js", "eb:prod": "npm run build && npm run start:prod" }
  3. You may faced permission denied errors during your build.在构建过程中,您可能会遇到权限被拒绝错误。 To solve this problem you can create .npmrc file with the following content:要解决此问题,您可以创建具有以下内容的.npmrc文件:
     # Force npm to run node-gyp also as root unsafe-perm=true

If you need more details, I wrote a blogpost about it: I deployed a server-side React app with AWS Elastic Beanstalk.如果您需要更多详细信息,我写了一篇关于它的博文: 我使用 AWS Elastic Beanstalk 部署了一个服务器端 React 应用程序。 Here's what I learned. 这是我学到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM