Skip to content
Advertisement

Setup Node.JS Server on Ubuntu

I am using the MEAN Stack and I could run my frontend already. After ng build I moved all the content in the dist folder to var/www/html and could access my website. I’m using Apache and my frontend is now available online. The problem is now my backend. I am using Node.js but I have no clue how I can make it “online” for everyone. 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. 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? Is there a way to run node.js in Apache also?

Actually I made a apache proxy and even with nginx but seems not to work yet…

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

and also from here: 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…

What should I do?

EDIT2:

Thanks of the help from Gouveia, I am able to serve front and backend with 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. For me the backend is loading but for you guys I think not

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

Advertisement

Answer

When you run npm start server.js, you are running a server on its own.

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):

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

Apache config files in linux are usually located here: /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:

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.

With both approaches, accessing your Apache/Nginx server on the /api/* path will make a request to the nodejs server.

In Nginx case, the path /api is included in the request path to nodejs server. 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.

For more details and configurations, I would advice to check the linked documentation.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement