我正在尝试在 cpanel 共享托管中运行一个简单的 next js 应用程序。我按照本教程进行操作: https://medium.com/@geevadon/how-to-deploy-a-next-js-app-on-cpanel-efficiently-c00c5eb860de
我尝试通过自定义服务器运行下一个应用程序。这是我的 server.js:
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const hostname = 'localhost'
const port = process.env.PORT || 3000
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port })
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer(async (req, res) => {
try {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
if (pathname === '/a') {
await app.render(req, res, '/a', query)
} else if (pathname === '/b') {
await app.render(req, res, '/b', query)
} else {
await handle(req, res, parsedUrl)
}
} catch (err) {
console.error('Error occurred handling', req.url, err)
res.statusCode = 500
res.end('internal server error')
}
})
.once('error', (err) => {
console.error(err)
process.exit(1)
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`)
})
})
这是我的 package.json 文件:
"name": "next-simple",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js",
"export": "next build && next export",
"lint": "next lint"
},
"dependencies": {
"autoprefixer": "10.4.15",
"next": "13.4.16",
"postcss": "8.4.28",
"react": "18.2.0",
"react-dom": "18.2.0",
"tailwindcss": "3.3.3"
}
}
这是我的
next.config.js
文件:
/** @type {import('next').NextConfig} */
const nextConfig = {}
module.exports = nextConfig
.htaccess
文件是这样的:
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/toirihoi/nextapp"
PassengerBaseURI "/"
PassengerNodejs "/home/toirihoi/nodevenv/nextapp/18/bin/node"
PassengerAppType node
PassengerStartupFile server.js
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php80” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php80 .php .php8 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php8_module>
php_flag display_errors Off
php_value max_execution_time 3000
php_value max_input_time 6000
php_value max_input_vars 1000
php_value memory_limit 2512M
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php80"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
php_flag display_errors Off
php_value max_execution_time 3000
php_value max_input_time 6000
php_value max_input_vars 1000
php_value memory_limit 2512M
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php80"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives, do not edit
然后我尝试通过 npm run start 命令从 cpanel 终端启动应用程序并收到以下错误:
> [email protected] dev
> node server.js
undefined:9898
return await WebAssembly.instantiate(mod, {
^
RangeError: WebAssembly.instantiate(): Out of memory: wasm memory
at lazyllhttp (eval at requireWithFakeGlobalScope (/home/toirihoi/nodevenv/nextapp/18/lib/node_modules/next/dist/compiled/@edge-runtime/primitives/load.js:64:3), <anonymous>:9898:32)
Node.js v18.16.1
请帮我解决这个问题。
我尝试从 ssh 终端 putty 通过 npm start 命令运行应用程序。
请从putty终端查看结果。
[nextapp (18)] [toirihoi@server3 nextapp]$ npm start
> [email protected] start
> NODE_ENV=production node server.js
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (node:net:1740:16)
at listenInCluster (node:net:1788:12)
at Server.listen (node:net:1876:7)
at /home/toirihoi/nextapp/server.js:37:10
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '::',
port: 3000
}
我将端口更改为 3001。它运行成功,但是当在 Chrome 浏览器上打开我的应用程序时,它说 404 未找到。
[nextapp (18)] [toirihoi@server3 nextapp]$ npm start
> [email protected] start
> NODE_ENV=production node server.js
> Ready on http://localhost:3001
http://localhost:3001
你已经找到答案了吗?遇到同样的问题...