在 Ubuntu 上设置 Node.JS 服务器

Setup Node.JS Server on Ubuntu

我正在使用 MEAN Stack,我已经可以 运行 我的前端了。在 ng build 之后,我将 dist 文件夹中的所有内容移动到 var/www/html 并且可以访问我的网站。我正在使用 Apache,我的前端现在可以在线使用。现在的问题是我的后端。我正在使用 Node.js 但我不知道如何让它对每个人都“在线”。使用 npm start server.jspm2 start server.js 我可以让我的 Node.js 服务器 运行 并且一切正常,但它只对我有用。当我的朋友访问我的网站时,后端不是运行ning(只有前端)。

所以我的问题是,如何让我的 node.js 服务器 public?有没有办法在 Apache 中 运行 node.js?

实际上我制作了一个 apache 代理,甚至使用了 nginx,但似乎还没有工作...

我是从这里开始的: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04

也来自这里: https://medium.com/@pierangelo1982/using-nodejs-app-with-apache-374b37c6140d

编辑:

我可以使用 nginx 为我的后端提供服务,但我必须停止我的 Apache 服务器,但我的 Apache 服务器正在为 angular 前端服务...

我该怎么办?

编辑 2:

感谢 Gouveia 的帮助,我可以用 NGINX 服务前后端

server {
    listen 80;

    server_name http://travelinked.vm.mi.hdm-stuttgart.de;

    location / {
        root /var/www/html;
    }

    location /api {
        proxy_pass http://141.62.65.110:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

如您所见,该站点可用,但后端仍未 运行 在我的内部网之外。对我来说,后端正在加载,但对你们来说,我认为不是

http://travelinked.vm.mi.hdm-stuttgart.de 是网站

当您 运行 npm start server.js 时,您正在 运行 自己安装服务器。

如果您希望所有请求都通过 Apache,那么您可以使用 simple reverse proxy 从 Apache 连接到您的 nodejs 后端(假设您的节点 js 服务器 运行s 在端口 8080 上):

ProxyPass "/api"  "http://localhost:8080/"

linux 中的 Apache 配置文件通常位于此处:/etc/apache2/sites-available/

如果你想改用 Nginx,你也可以serve your static files with it, while proxying the requests to the API

server {

    location / {
        # Path to your angular dist folder
        root /path/to/static/files;
    }

    location /api {
        # Address of your nodejs server
        proxy_pass http://localhost:8080/;
    }
}

linux 中的配置文件通常位于此处:/etc/nginx/nginx.conf

使用这两种方法,访问 /api/* 路径上的 Apache/Nginx 服务器将向 nodejs 服务器发出请求。

在 Nginx 的情况下,路径 /api 包含在到 nodejs 服务器的请求路径中。要删除那部分路径,您需要一个重写规则:

location /api {
    # Address of your nodejs server
    proxy_pass http://localhost:8080/;
    # This removes the /api part in the request to the backend
    rewrite /api/(.*) / break;
}

我在示例中使用了本地主机,因为我假设您运行将所有内容都放在同一台机器上。

有关更多详细信息和配置,我建议查看链接文档。