如何在cpanel上部署nextjs应用程序?

3yhwsihp  于 2023-06-22  发布在  其他
关注(0)|答案(5)|浏览(154)

我按照这些步骤在cPanel上部署了我的nextjs。
1.转到package.json并添加这一行:"homepage": "http://afsanefadaei.ir"
1.运行next build.next文件夹作为我的构建文件夹
1.转到cpanel >> file manager >> public_html文件夹并将.next文件夹的内容上传到此目录
1.添加或编辑此文件:.htaccess至:

但是当我去网站的时候,我面对的是:

你知道这有什么问题吗?

qyzbxkaa

qyzbxkaa1#

我上传了out(它得到生成npm run build && npm run export)文件夹到public_html,并创建了.htaccess文件,如

<IfModule mod_rewrite.c>

  RewriteEngine On
  RewriteBase /
  RewriteRule ^index.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-L
  RewriteRule . /index.html [L]

</IfModule>

对我很有效😁

问题:当我在不同的路由上刷新页面时,比如说/about,它会带来index.js页面内容,但URL不会更改为/

j5fpnvbx

j5fpnvbx2#

1.您的.next没有index.html文件。
1.似乎你有服务器端(主要使用nodejs),但不幸的是你不能从cpanel运行服务器端。
1.据我所知,如果你倾向于只使用前端,你应该使用next export而不是next build
但最重要的是第1点,确保您的.next文件夹中有index.html

d5vmydt9

d5vmydt93#

我可以用上面的答案之一托管应用程序,但当我刷新页面时,应用程序将崩溃或转到初始路由。
这就是我解决这个问题的方法(解决方案取自next.js官方网站)。
1.使用npm run build构建next.js应用程序的生产版本
1.在根文件夹中创建一个启动文件app.js并复制以下代码

const {
  createServer
} = require("http");
const {
  parse
} = require("url");
const next = require("next");

const port = process.env.PORT || 3000;

// Create the Express-Next App
const app = next({
  dev:false
});
const handle = app.getRequestHandler();

app
  .prepare()
  .then(() => {
    createServer((req, res) => {
      const parsedUrl = parse(req.url, true);
      const {
        pathname,
        query
      } = parsedUrl;
      handle(req, res, parsedUrl);
      console.log("pathname", pathname);
    }).listen(port, (err) => {
      if (err) throw err;
      console.log(`> Ready on http://localhost:${port}`);
    });
  })
  .catch((ex) => {
    console.error(ex.stack);
    process.exit(1);
  });

1.在cPanel上创建一个Node.js应用,并添加应用启动文件名x1c 0d1x
1.并启动应用程序。你完了!
更多信息请查看Next.js Custom Server的官方文档

zzoitvuj

zzoitvuj4#

将其部署为NodeJS应用程序。

w41d8nur

w41d8nur5#

下面是一个完整的.htaccess,用于实现一个带有Apache重定向的Node.js应用程序。如果你的NextJS应用程序部署在next start服务器上(监听特定的端口),这在cPanel上运行得很好。

RewriteEngine On

# Need to disable DirectoryIndex to avoid rewriting directory URL to index.html
DirectoryIndex disabled

# Redirect home page request to the NextJS server
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://127.0.0.1:4002/ [P,L]

# Redirect all other requests to the NextJS server with the URI appended
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:4002/$1 [P,L]

相关问题