如何在AWS Elastic Beanstalk上部署Next.js?

7
我有一个基于next.js框架的小型网站,我想在AWS Elastic Beanstalk上部署它。
我按照这里https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/GettingStarted.html和这里https://nextjs.org/docs/deployment的入门文档进行操作。然而,next.js网站上的文档并没有深入探讨。
所以我做了以下步骤:
  1. 在我的项目文件夹中运行npm run build - 这将生成.next文件夹。
  2. 复制.next文件夹内的package.json文件。
  3. 打开.next文件夹并压缩其内容(使所有文件都在zip的根目录中)。
  4. 打开AWS管理控制台,选择创建Web应用程序并上传zip文件。
所有操作都无误,AWS Beanstalk已经创建了环境。 然后我收到了这个信息:Healthstatus: Severe - 100.0 %的请求返回HTTP 5xx错误。 于是我查看了日志,得到了以下信息:
Jan  2 19:44:38 ip-172-31-23-147 web: at /var/app/current/node_modules/next/dist/bin/next:27:115
Jan  2 19:44:38 ip-172-31-23-147 web: npm ERR! code ELIFECYCLE
Jan  2 19:44:38 ip-172-31-23-147 web: npm ERR! errno 1
Jan  2 19:44:38 ip-172-31-23-147 web: npm ERR! next-js-website@0.1.3 start: `next start -p $PORT`
Jan  2 19:44:38 ip-172-31-23-147 web: npm ERR! Exit status 1
Jan  2 19:44:38 ip-172-31-23-147 web: npm ERR!
Jan  2 19:44:38 ip-172-31-23-147 web: npm ERR! Failed at the next-js-website@0.1.3 start script.
Jan  2 19:44:38 ip-172-31-23-147 web: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Jan  2 19:44:38 ip-172-31-23-147 web: npm ERR! A complete log of this run can be found in:
Jan  2 19:44:38 ip-172-31-23-147 web: npm ERR!     /home/webapp/.npm/_logs/2021-01-02T19_44_38_214Z-debug.log
Jan  2 19:44:38 ip-172-31-23-147 web: > next-js-website@0.1.3 start /var/app/current
Jan  2 19:44:38 ip-172-31-23-147 web: > next start -p $PORT
Jan  2 19:44:38 ip-172-31-23-147 web: Error: Could not find a production build in the '/var/app/current/.next' directory. Try building your app with 'next build' before starting the production server. https://err.sh/vercel/next.js/production-start-no-build-id
Jan  2 19:44:38 ip-172-31-23-147 web: at Server.readBuildId (/var/app/current/node_modules/next/dist/next-server/server/next-server.js:138:355)
Jan  2 19:44:38 ip-172-31-23-147 web: at new Server (/var/app/current/node_modules/next/dist/next-server/server/next-server.js:3:120)
Jan  2 19:44:38 ip-172-31-23-147 web: at createServer (/var/app/current/node_modules/next/dist/server/next.js:2:638)
Jan  2 19:44:38 ip-172-31-23-147 web: at start (/var/app/current/node_modules/next/dist/server/lib/start-server.js:1:323)
Jan  2 19:44:38 ip-172-31-23-147 web: at nextStart (/var/app/current/node_modules/next/dist/cli/next-start.js:19:125)

我认为最重要的一行是:Could not find a production build in the '/var/app/current/.next' directory. Try building your app with 'next build' before starting the production server. https://err.sh/vercel/next.js/production-start-no-build-id 但正如您在上面看到的那样,我之前已经运行了npm run build并创建了构建文件夹。 经过一些研究,我发现许多人似乎遇到了类似的问题,但我还没有找到解决方法。
我该如何解决这个问题? 非常感谢。
一些额外的信息: 我的package.json看起来像这样:
{
    "name": "next-js-website",
    "version": "0.1.3",
    "private": true,
    "scripts": {
        "dev": "next dev",
        "build": "next build",
        "start": "next start -p $PORT"
    },
    "engines": {
        "node": "12.9.0"
    },
    "dependencies": {
        "aws-sdk": "^2.817.0",
        "chart.js": "^2.9.4",
        "moment": "^2.29.1",
        "moment-timezone": "^0.5.32",
        "next": "10.0.4",
        "react": "17.0.1",
        "react-chartjs-2": "^2.11.1",
        "react-device-detect": "^1.15.0",
        "react-dom": "17.0.1"
    }
}

我的压缩文件看起来像这样:

enter image description here

1个回答

11

经过一些阅读和测试,我想出了一个解决方案。

对于所有也在苦苦挣扎如何将node.js或特别是next.js应用程序部署到AWS Elastic Beanstalk的人:

  1. 确保你的.zip文件看起来像这样
.next
package.json

为了确保在zip文件中没有父目录。 AWS EB将首先运行npm install并安装所有依赖项。 然后它将运行npm start,因此构建文件夹需要与package.json文件在同一个文件夹中。

  1. 在您的package.json文件中添加以下内容
"scripts": {
        "dev": "next dev",
        "build": "next build",
        "start": "next start -- --port $PORT"
    },

nginx代理服务器正在监听8080端口,你也可以直接设置此端口。然而AWS EB将通过环境变量来设置端口。

如果你打开日志文件并看到类似以下内容:ready - started server on http://localhost:3000,那么你就知道你在端口方面搞砸了。


你知道是否需要上传其他文件吗?例如 .env.productionnext.config.js?或者这些文件会被打包到构建中吗? - Marnix.hoh
1
可能需要添加其他所需文件。.env.production通常位于src文件夹中,因此我猜它将与.next文件夹捆绑在一起。但是我不确定。例如,我的zip包含.ebextensions、.next、public和package.json。特别是public文件夹需要使图像可用。 - Mrob
想问一下你是否考虑在 S3 + CloudFront + Lambda@Edge 上部署 Next.js - https://serverless-stack.com/examples/how-to-create-a-nextjs-app-with-serverless.html 我想知道响应延迟与 BeanStalk 相比怎么样。 - Frank
如果您已经创建了 next.config.js,请确保将其包含在内。没有它,我会遇到错误。 - thetaPC
1
我似乎有同样的问题,但是你的建议对我没有解决问题。我正在使用“zip -r deploy.zip .next package.json”压缩文件-这看起来正确吗? - mort

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接