简体   繁体   English

在 Ubuntu 上设置 Node.JS 服务器

[英]Setup Node.JS Server on Ubuntu

I am using the MEAN Stack and I could run my frontend already.我正在使用 MEAN Stack,我已经可以运行我的前端了。 After ng build I moved all the content in the dist folder to var/www/html and could access my website.ng build之后,我将dist文件夹中的所有内容移动到var/www/html并且可以访问我的网站。 I'm using Apache and my frontend is now available online.我正在使用 Apache,我的前端现在可以在线使用。 The problem is now my backend.问题现在是我的后端。 I am using Node.js but I have no clue how I can make it "online" for everyone.我正在使用 Node.js 但我不知道如何让每个人都“在线”。 With npm start server.js or pm2 start server.js I can make my Node.js server run and everything is working fine, but it's only working for me.使用npm start server.js或 pm2 start pm2 start server.js我可以让我的 Node.js 服务器运行,一切正常,但它只对我有用。 When my friend accessed my website, the backend is not running (only frontend).当我的朋友访问我的网站时,后端没有运行(仅前端)。

So my question is, how can I make my node.js server public?所以我的问题是,我怎样才能让我的 node.js 服务器公开? Is there a way to run node.js in Apache also?有没有办法在 Apache 中运行 node.js ?

Actually I made a apache proxy and even with nginx but seems not to work yet...实际上我做了一个 apache 代理,甚至使用 nginx 但似乎还没有工作......

I followed this step from here: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04我从这里开始执行此步骤: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04

and also from here: https://medium.com/@pierangelo1982/using-nodejs-app-with-apache-374b37c6140d也从这里: https://medium.com/@pierangelo1982/using-nodejs-app-with-apache-374b37c6140d

EDIT:编辑:

I am able to server my backend with nginx but I have to stop my Apache server but my apache server is serving the angular frontend... I am able to server my backend with nginx but I have to stop my Apache server but my apache server is serving the angular frontend...

What should I do?我应该怎么办?

EDIT2:编辑2:

Thanks of the help from Gouveia, I am able to serve front and backend with NGINX感谢 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;
    }
}

As you can see the site is available but still the backend is not running outside of my intranet.如您所见,该站点可用,但后端仍未在我的 Intranet 之外运行。 For me the backend is loading but for you guys I think not对我来说,后端正在加载,但对你们来说,我认为不是

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

When you run npm start server.js , you are running a server on its own.当您运行npm start server.js时,您正在运行自己的服务器。

If you want all requests to go through Apache, then you can use simple reverse proxy to connect from Apache to your nodejs backend (assuming your node js server runs on port 8080):如果您希望通过 Apache 对 go 的所有请求,那么您可以使用 简单的反向代理从 Apache 连接到您的 nodejs 后端(假设您的节点 js 服务器在 port8008 上运行):

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

Apache config files in linux are usually located here: /etc/apache2/sites-available/ . linux 中的 Apache 配置文件通常位于: /etc/apache2/sites-available/

If you want to use Nginx instead, you can serve your static files with it, while proxying the requests to the API as well:如果您想使用 Nginx 代替,您可以使用它提供 static 文件,同时将请求代理到 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/;
    }
}

The config file in linux is usually located here: /etc/nginx/nginx.conf . linux 中的配置文件通常位于: /etc/nginx/nginx.conf

With both approaches, accessing your Apache/Nginx server on the /api/* path will make a request to the nodejs server.使用这两种方法,访问/api/*路径上的 Apache/Nginx 服务器将向 nodejs 服务器发出请求。

In Nginx case, the path /api is included in the request path to nodejs server.在 Nginx 情况下,路径 /api 包含在对 nodejs 服务器的请求路径中。 To remove that part of the path, you will need a rewrite rule:要删除该部分路径,您将需要一个重写规则:

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/(.*) /$1 break;
}

I've used localhost in the examples, as I'm assuming you are running everything in the same machine.我在示例中使用了 localhost,因为我假设您在同一台机器上运行所有内容。

For more details and configurations, I would advice to check the linked documentation.有关更多详细信息和配置,我建议查看链接文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM