如何在一台服务器上部署Node.js API和Vue.js应用 [英] how to deploy nodejs api and vuejs app in one server

查看:105
本文介绍了如何在一台服务器上部署Node.js API和Vue.js应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了node rest api和vuejs Web应用程序,
Im试图将这两个项目部署到运行ubuntu的aws服务器中。
两个应用程序都有不同的端口,
域我尝试为api配置api.example.com和为vue应用配置example.com。
在SSH中运行命令后,我可以同时运行两个应用程序,但是我需要它们永久运行它。
我做了什么

I have developed node rest api and vuejs web applications, Im trying to deploy both project in to one aws server which run ubuntu. Both applications have different port, domain I try to configure api.example.com for api and example.com for vue app. I can run both applications once after running the command in SSH, but I need them to run it forever. What I did,


  1. 分别部署到应用程序

  2. 应用程序可以通过端口访问

我需要他们访问


  1. api .example.com

  2. example.com

要做的步骤,
任何更改主机文件。

what are the step to do, Any changes host file.

推荐答案

这是我们在同一服务器上运行VueJS UI和ExpressJS REST API的方式。

This is how we are running our VueJS UI and ExpressJS REST API from the same server.

我们正在使用 PM2 来管理我们的服务。

We are managing our services with PM2.

VueJS(开发环境,您可以在生产中添加相同的设置)

package.json 添加 start: HOST ='0.0.0.0'PORT = 80 npm run dev,,其中VueJS正在侦听的端口80是 scripts:{..} 数组。然后,在安装PM2之后(对于开发人员),我们可以使用 cd / location / of / vue / root启动VueJS; sudo pm2 start npm run dev --name Vue-开始。 (确保Apache未运行。)

In package.json add "start": "HOST='0.0.0.0' PORT=80 npm run dev",, where 80 is the port VueJS is listening on, to the "scripts": {..} array. Then, after installing PM2, (for dev) we can start VueJS with cd /location/of/vue/root; sudo pm2 start npm run dev --name Vue -- start. (Make sure that Apache is not running).

请注意,将HOST设置为0.0.0.0非常重要。不要将其设置为LocalHost或您的服务器IP地址,否则可能会遇到问题。

Please note that setting the HOST to 0.0.0.0 is important. Do not set it to LocalHost or your Servers IP address or you may run into issues.

ExpressJS

/location/of/express/app.js 中,将其与文件底部类似:
app.listen(process.env.PORT || 8081)
,其中8081是REST API应该监听的端口。然后我可以使用 sudo pm2 start /location/of/express/app.js --name Express

In the /location/of/express/app.js add this similar to the bottom of the file: app.listen(process.env.PORT || 8081), where 8081 is the port your REST API should be listening on. I can then start it with sudo pm2 start /location/of/express/app.js --name Express

此时,VueJS应该在www.example.com(暗示端口80)上可用,而REST API将在www.example.com:8081上可用。

At this point, the VueJS should be available at www.example.com (implied Port 80) and the REST API would be available at www.example.com:8081.

如果要使api.example.com/指向API,则需要确保DNS将 api子域指向所需的端口,否则可能需要将该端口添加到URL中,如下所示:

If you want to have api.example.com/ point to the API, you need to make sure that your DNS is pointing the "api" subdomain to the desired port, or you may have to add the port into the URL as above.

此外,您还可以通过 pm2日志APPNAME-行100 轻松跟踪PM2日志

Additionally, you can easily follow the logs through PM2 as well with pm2 logs APPNAME --lines 100.

这篇关于如何在一台服务器上部署Node.js API和Vue.js应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆