1

I'm trying to learn next.js and I'm really struggling working out how to deploy it on my VPS as I'm not used to running a site where there is no index.html. I've been trying to research it for the best part of a week and it's driving me a bit nuts!!

So, my VPS is with Centos 7, cyberpanel and openlitespeed. For testing purposes I'm just using the initial next.js build (i.e. what's first created). I copied over the files to the public_html folder of my site and ran 'npm build'. If I run 'npm start' I can see the 'site' on my-domain-name:3000. But I can't figure out how to get this to run on just my-domain-name. All the information I find either seems to be about deploying on various services (Vercel, digital ocean etc) or in other ways unrelated to just setting it up on a VPS

The closest lead I have so far is that in OpenLiteSpeed I need to create an App Server 'context' for the virtual host for my site (CyberPanel auto-created this) and have entered the following settings:

  • URI: /
  • Location: /home/$VH_NAME/public_html
  • Binary Path: /usr/bin/node (not sure if this is right but there is this file on my server level)
  • Application Type: Node
  • Startup File: server.js

The startup file is the basic one shown at https://nextjs.org/docs/advanced-features/custom-server. I also tried updating the scripts with the following:

"scripts": {
  "dev": "node server.js",
  "build": "next build",
  "start": "NODE_ENV=production node server.js"
}

Additionally I tried opening the port up in my firewall (CFS)

I still can't seem to figure out how I'm supposed to be getting my VPS to run this site without using the port number or how to keep it running when I terminate my session in VS Code. I feel that I'm really misunderstanding how to set up OpenLiteSpeed as a reverse proxy and I'm also a little worried that if I keep testing random things out I'm likely to damage something.

This is probably modern web dev 101 but I'm really over my head here and not sure where to look for the answers. If someone has a moment would you be able to point me in the right direction? Happy to provide any other information about my set-up necessary...

Thanks in advance

4

1 回答 1

0

好的,我想我偶然发现了一个解决方案(通常是在提出问题后的第二天)。我不是 100% 确定这是正确的解决方案,所以如果有人看到问题,请告诉我。否则,对于处于相同情况的其他人来说,它就在这里

在我的 public_folder 中,我创建/修改了 .htaccess 文件:

RewriteEngine On
RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]

然后在 OpenLiteSpeed 中,我使用以下设置在为我创建的域虚拟主机 Cyber​​panel 下设置了一个“外部应用程序”:

  • 名称:127.0.0.1:3000
  • 地址: http: //127.0.0.1 :3000
  • 最大连接数:100
  • 项目清单
  • 连接保持活动超时:60
  • 初始请求超时(秒):60
  • 重试超时(秒):0
  • 响应缓冲:否

然后我重新启动了 OLS,现在当我运行 NPM start 时,我可以在域级别看到该站点!

当我关闭终端会话时,我仍然需要做一些研究以保持它运行(查看PM2systemd),但我感觉更加积极,因为似乎有很多关于配置下一步的信息。 js正确,我不再撞墙!

于 2021-05-25T15:36:43.850 回答